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