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_GENERIC_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ |
| 13 |
| 14 #include <assert.h> |
| 15 #include <list> |
| 16 |
| 17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 18 #include "webrtc/typedefs.h" |
| 19 |
| 20 namespace webrtc { |
| 21 template <class MemoryType> |
| 22 class MemoryPoolImpl { |
| 23 public: |
| 24 // MemoryPool functions. |
| 25 int32_t PopMemory(MemoryType*& memory); |
| 26 int32_t PushMemory(MemoryType*& memory); |
| 27 |
| 28 MemoryPoolImpl(int32_t initialPoolSize); |
| 29 ~MemoryPoolImpl(); |
| 30 |
| 31 // Atomic functions |
| 32 int32_t Terminate(); |
| 33 bool Initialize(); |
| 34 |
| 35 private: |
| 36 // Non-atomic function. |
| 37 int32_t CreateMemory(uint32_t amountToCreate); |
| 38 |
| 39 CriticalSectionWrapper* _crit; |
| 40 |
| 41 bool _terminate; |
| 42 |
| 43 std::list<MemoryType*> _memoryPool; |
| 44 |
| 45 uint32_t _initialPoolSize; |
| 46 uint32_t _createdMemory; |
| 47 uint32_t _outstandingMemory; |
| 48 }; |
| 49 |
| 50 template <class MemoryType> |
| 51 MemoryPoolImpl<MemoryType>::MemoryPoolImpl(int32_t initialPoolSize) |
| 52 : _crit(CriticalSectionWrapper::CreateCriticalSection()), |
| 53 _terminate(false), |
| 54 _initialPoolSize(initialPoolSize), |
| 55 _createdMemory(0), |
| 56 _outstandingMemory(0) {} |
| 57 |
| 58 template <class MemoryType> |
| 59 MemoryPoolImpl<MemoryType>::~MemoryPoolImpl() { |
| 60 // Trigger assert if there is outstanding memory. |
| 61 assert(_createdMemory == 0); |
| 62 assert(_outstandingMemory == 0); |
| 63 delete _crit; |
| 64 } |
| 65 |
| 66 template <class MemoryType> |
| 67 int32_t MemoryPoolImpl<MemoryType>::PopMemory(MemoryType*& memory) { |
| 68 CriticalSectionScoped cs(_crit); |
| 69 if (_terminate) { |
| 70 memory = NULL; |
| 71 return -1; |
| 72 } |
| 73 if (_memoryPool.empty()) { |
| 74 // _memoryPool empty create new memory. |
| 75 CreateMemory(_initialPoolSize); |
| 76 if (_memoryPool.empty()) { |
| 77 memory = NULL; |
| 78 return -1; |
| 79 } |
| 80 } |
| 81 memory = _memoryPool.front(); |
| 82 _memoryPool.pop_front(); |
| 83 _outstandingMemory++; |
| 84 return 0; |
| 85 } |
| 86 |
| 87 template <class MemoryType> |
| 88 int32_t MemoryPoolImpl<MemoryType>::PushMemory(MemoryType*& memory) { |
| 89 if (memory == NULL) { |
| 90 return -1; |
| 91 } |
| 92 CriticalSectionScoped cs(_crit); |
| 93 _outstandingMemory--; |
| 94 if (_memoryPool.size() > (_initialPoolSize << 1)) { |
| 95 // Reclaim memory if less than half of the pool is unused. |
| 96 _createdMemory--; |
| 97 delete memory; |
| 98 memory = NULL; |
| 99 return 0; |
| 100 } |
| 101 _memoryPool.push_back(memory); |
| 102 memory = NULL; |
| 103 return 0; |
| 104 } |
| 105 |
| 106 template <class MemoryType> |
| 107 bool MemoryPoolImpl<MemoryType>::Initialize() { |
| 108 CriticalSectionScoped cs(_crit); |
| 109 return CreateMemory(_initialPoolSize) == 0; |
| 110 } |
| 111 |
| 112 template <class MemoryType> |
| 113 int32_t MemoryPoolImpl<MemoryType>::Terminate() { |
| 114 CriticalSectionScoped cs(_crit); |
| 115 assert(_createdMemory == _outstandingMemory + _memoryPool.size()); |
| 116 |
| 117 _terminate = true; |
| 118 // Reclaim all memory. |
| 119 while (_createdMemory > 0) { |
| 120 MemoryType* memory = _memoryPool.front(); |
| 121 _memoryPool.pop_front(); |
| 122 delete memory; |
| 123 _createdMemory--; |
| 124 } |
| 125 return 0; |
| 126 } |
| 127 |
| 128 template <class MemoryType> |
| 129 int32_t MemoryPoolImpl<MemoryType>::CreateMemory(uint32_t amountToCreate) { |
| 130 for (uint32_t i = 0; i < amountToCreate; i++) { |
| 131 MemoryType* memory = new MemoryType(); |
| 132 if (memory == NULL) { |
| 133 return -1; |
| 134 } |
| 135 _memoryPool.push_back(memory); |
| 136 _createdMemory++; |
| 137 } |
| 138 return 0; |
| 139 } |
| 140 } // namespace webrtc |
| 141 |
| 142 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ |
OLD | NEW |