OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_H_ |
| 13 |
| 14 #include <assert.h> |
| 15 |
| 16 #include "webrtc/typedefs.h" |
| 17 |
| 18 #if _WIN32 |
| 19 #include "webrtc/modules/audio_conference_mixer/source/memory_pool_win.h" |
| 20 #else |
| 21 #include "webrtc/modules/audio_conference_mixer/source/memory_pool_posix.h" |
| 22 #endif |
| 23 |
| 24 namespace webrtc { |
| 25 |
| 26 template <class MemoryType> |
| 27 class MemoryPool { |
| 28 public: |
| 29 // Factory method, constructor disabled. |
| 30 static int32_t CreateMemoryPool(MemoryPool*& memoryPool, |
| 31 uint32_t initialPoolSize); |
| 32 |
| 33 // Try to delete the memory pool. Fail with return value -1 if there is |
| 34 // outstanding memory. |
| 35 static int32_t DeleteMemoryPool(MemoryPool*& memoryPool); |
| 36 |
| 37 // Get/return unused memory. |
| 38 int32_t PopMemory(MemoryType*& memory); |
| 39 int32_t PushMemory(MemoryType*& memory); |
| 40 |
| 41 private: |
| 42 MemoryPool(int32_t initialPoolSize); |
| 43 ~MemoryPool(); |
| 44 |
| 45 MemoryPoolImpl<MemoryType>* _ptrImpl; |
| 46 }; |
| 47 |
| 48 template <class MemoryType> |
| 49 MemoryPool<MemoryType>::MemoryPool(int32_t initialPoolSize) { |
| 50 _ptrImpl = new MemoryPoolImpl<MemoryType>(initialPoolSize); |
| 51 } |
| 52 |
| 53 template <class MemoryType> |
| 54 MemoryPool<MemoryType>::~MemoryPool() { |
| 55 delete _ptrImpl; |
| 56 } |
| 57 |
| 58 template <class MemoryType> |
| 59 int32_t MemoryPool<MemoryType>::CreateMemoryPool(MemoryPool*& memoryPool, |
| 60 uint32_t initialPoolSize) { |
| 61 memoryPool = new MemoryPool(initialPoolSize); |
| 62 if (memoryPool == NULL) { |
| 63 return -1; |
| 64 } |
| 65 if (memoryPool->_ptrImpl == NULL) { |
| 66 delete memoryPool; |
| 67 memoryPool = NULL; |
| 68 return -1; |
| 69 } |
| 70 if (!memoryPool->_ptrImpl->Initialize()) { |
| 71 delete memoryPool; |
| 72 memoryPool = NULL; |
| 73 return -1; |
| 74 } |
| 75 return 0; |
| 76 } |
| 77 |
| 78 template <class MemoryType> |
| 79 int32_t MemoryPool<MemoryType>::DeleteMemoryPool(MemoryPool*& memoryPool) { |
| 80 if (memoryPool == NULL) { |
| 81 return -1; |
| 82 } |
| 83 if (memoryPool->_ptrImpl == NULL) { |
| 84 return -1; |
| 85 } |
| 86 if (memoryPool->_ptrImpl->Terminate() == -1) { |
| 87 return -1; |
| 88 } |
| 89 delete memoryPool; |
| 90 memoryPool = NULL; |
| 91 return 0; |
| 92 } |
| 93 |
| 94 template <class MemoryType> |
| 95 int32_t MemoryPool<MemoryType>::PopMemory(MemoryType*& memory) { |
| 96 return _ptrImpl->PopMemory(memory); |
| 97 } |
| 98 |
| 99 template <class MemoryType> |
| 100 int32_t MemoryPool<MemoryType>::PushMemory(MemoryType*& memory) { |
| 101 if (memory == NULL) { |
| 102 return -1; |
| 103 } |
| 104 return _ptrImpl->PushMemory(memory); |
| 105 } |
| 106 } // namespace webrtc |
| 107 |
| 108 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_H_ |
OLD | NEW |