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 { 29 public: 30 // Factory method, constructor disabled. 31 static int32_t CreateMemoryPool(MemoryPool*& memoryPool, 32 uint32_t initialPoolSize); 33 34 // Try to delete the memory pool. Fail with return value -1 if there is 35 // outstanding memory. 36 static int32_t DeleteMemoryPool( 37 MemoryPool*& memoryPool); 38 39 // Get/return unused memory. 40 int32_t PopMemory(MemoryType*& memory); 41 int32_t PushMemory(MemoryType*& memory); 42 private: 43 MemoryPool(int32_t initialPoolSize); 44 ~MemoryPool(); 45 46 MemoryPoolImpl<MemoryType>* _ptrImpl; 47 }; 48 49 template<class MemoryType> MemoryPool(int32_t initialPoolSize)50MemoryPool<MemoryType>::MemoryPool(int32_t initialPoolSize) 51 { 52 _ptrImpl = new MemoryPoolImpl<MemoryType>(initialPoolSize); 53 } 54 55 template<class MemoryType> ~MemoryPool()56MemoryPool<MemoryType>::~MemoryPool() 57 { 58 delete _ptrImpl; 59 } 60 61 template<class MemoryType> int32_t CreateMemoryPool(MemoryPool * & memoryPool,uint32_t initialPoolSize)62MemoryPool<MemoryType>::CreateMemoryPool(MemoryPool*& memoryPool, 63 uint32_t initialPoolSize) 64 { 65 memoryPool = new MemoryPool(initialPoolSize); 66 if(memoryPool == NULL) 67 { 68 return -1; 69 } 70 if(memoryPool->_ptrImpl == NULL) 71 { 72 delete memoryPool; 73 memoryPool = NULL; 74 return -1; 75 } 76 if(!memoryPool->_ptrImpl->Initialize()) 77 { 78 delete memoryPool; 79 memoryPool = NULL; 80 return -1; 81 } 82 return 0; 83 } 84 85 template<class MemoryType> DeleteMemoryPool(MemoryPool * & memoryPool)86int32_t MemoryPool<MemoryType>::DeleteMemoryPool(MemoryPool*& memoryPool) 87 { 88 if(memoryPool == NULL) 89 { 90 return -1; 91 } 92 if(memoryPool->_ptrImpl == NULL) 93 { 94 return -1; 95 } 96 if(memoryPool->_ptrImpl->Terminate() == -1) 97 { 98 return -1; 99 } 100 delete memoryPool; 101 memoryPool = NULL; 102 return 0; 103 } 104 105 template<class MemoryType> PopMemory(MemoryType * & memory)106int32_t MemoryPool<MemoryType>::PopMemory(MemoryType*& memory) 107 { 108 return _ptrImpl->PopMemory(memory); 109 } 110 111 template<class MemoryType> PushMemory(MemoryType * & memory)112int32_t MemoryPool<MemoryType>::PushMemory(MemoryType*& memory) 113 { 114 if(memory == NULL) 115 { 116 return -1; 117 } 118 return _ptrImpl->PushMemory(memory); 119 } 120 } // namespace webrtc 121 122 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_H_ 123