OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ |
12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ | 12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ |
13 | 13 |
14 #include <assert.h> | 14 #include <assert.h> |
15 #include <list> | 15 #include <list> |
16 | 16 |
17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 17 #include "webrtc/base/criticalsection.h" |
18 #include "webrtc/typedefs.h" | 18 #include "webrtc/typedefs.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 template<class MemoryType> | 21 template<class MemoryType> |
22 class MemoryPoolImpl | 22 class MemoryPoolImpl |
23 { | 23 { |
24 public: | 24 public: |
25 // MemoryPool functions. | 25 // MemoryPool functions. |
26 int32_t PopMemory(MemoryType*& memory); | 26 int32_t PopMemory(MemoryType*& memory); |
27 int32_t PushMemory(MemoryType*& memory); | 27 int32_t PushMemory(MemoryType*& memory); |
28 | 28 |
29 MemoryPoolImpl(int32_t initialPoolSize); | 29 MemoryPoolImpl(int32_t initialPoolSize); |
30 ~MemoryPoolImpl(); | 30 ~MemoryPoolImpl(); |
31 | 31 |
32 // Atomic functions | 32 // Atomic functions |
33 int32_t Terminate(); | 33 int32_t Terminate(); |
34 bool Initialize(); | 34 bool Initialize(); |
35 private: | 35 private: |
36 // Non-atomic function. | 36 // Non-atomic function. |
37 int32_t CreateMemory(uint32_t amountToCreate); | 37 int32_t CreateMemory(uint32_t amountToCreate); |
38 | 38 |
39 CriticalSectionWrapper* _crit; | 39 rtc::CriticalSection _crit; |
40 | 40 |
41 bool _terminate; | 41 bool _terminate; |
42 | 42 |
43 std::list<MemoryType*> _memoryPool; | 43 std::list<MemoryType*> _memoryPool; |
44 | 44 |
45 uint32_t _initialPoolSize; | 45 uint32_t _initialPoolSize; |
46 uint32_t _createdMemory; | 46 uint32_t _createdMemory; |
47 uint32_t _outstandingMemory; | 47 uint32_t _outstandingMemory; |
48 }; | 48 }; |
49 | 49 |
50 template<class MemoryType> | 50 template<class MemoryType> |
51 MemoryPoolImpl<MemoryType>::MemoryPoolImpl(int32_t initialPoolSize) | 51 MemoryPoolImpl<MemoryType>::MemoryPoolImpl(int32_t initialPoolSize) |
52 : _crit(CriticalSectionWrapper::CreateCriticalSection()), | 52 : _terminate(false), |
53 _terminate(false), | |
54 _initialPoolSize(initialPoolSize), | 53 _initialPoolSize(initialPoolSize), |
55 _createdMemory(0), | 54 _createdMemory(0), |
56 _outstandingMemory(0) | 55 _outstandingMemory(0) |
57 { | 56 { |
58 } | 57 } |
59 | 58 |
60 template<class MemoryType> | 59 template<class MemoryType> |
61 MemoryPoolImpl<MemoryType>::~MemoryPoolImpl() | 60 MemoryPoolImpl<MemoryType>::~MemoryPoolImpl() |
62 { | 61 { |
63 // Trigger assert if there is outstanding memory. | 62 // Trigger assert if there is outstanding memory. |
64 assert(_createdMemory == 0); | 63 assert(_createdMemory == 0); |
65 assert(_outstandingMemory == 0); | 64 assert(_outstandingMemory == 0); |
66 delete _crit; | |
67 } | 65 } |
68 | 66 |
69 template<class MemoryType> | 67 template<class MemoryType> |
70 int32_t MemoryPoolImpl<MemoryType>::PopMemory(MemoryType*& memory) | 68 int32_t MemoryPoolImpl<MemoryType>::PopMemory(MemoryType*& memory) |
71 { | 69 { |
72 CriticalSectionScoped cs(_crit); | 70 rtc::CritScope cs(&_crit); |
73 if(_terminate) | 71 if(_terminate) |
74 { | 72 { |
75 memory = NULL; | 73 memory = NULL; |
76 return -1; | 74 return -1; |
77 } | 75 } |
78 if (_memoryPool.empty()) { | 76 if (_memoryPool.empty()) { |
79 // _memoryPool empty create new memory. | 77 // _memoryPool empty create new memory. |
80 CreateMemory(_initialPoolSize); | 78 CreateMemory(_initialPoolSize); |
81 if(_memoryPool.empty()) | 79 if(_memoryPool.empty()) |
82 { | 80 { |
83 memory = NULL; | 81 memory = NULL; |
84 return -1; | 82 return -1; |
85 } | 83 } |
86 } | 84 } |
87 memory = _memoryPool.front(); | 85 memory = _memoryPool.front(); |
88 _memoryPool.pop_front(); | 86 _memoryPool.pop_front(); |
89 _outstandingMemory++; | 87 _outstandingMemory++; |
90 return 0; | 88 return 0; |
91 } | 89 } |
92 | 90 |
93 template<class MemoryType> | 91 template<class MemoryType> |
94 int32_t MemoryPoolImpl<MemoryType>::PushMemory(MemoryType*& memory) | 92 int32_t MemoryPoolImpl<MemoryType>::PushMemory(MemoryType*& memory) |
95 { | 93 { |
96 if(memory == NULL) | 94 if(memory == NULL) |
97 { | 95 { |
98 return -1; | 96 return -1; |
99 } | 97 } |
100 CriticalSectionScoped cs(_crit); | 98 rtc::CritScope cs(&_crit); |
101 _outstandingMemory--; | 99 _outstandingMemory--; |
102 if(_memoryPool.size() > (_initialPoolSize << 1)) | 100 if(_memoryPool.size() > (_initialPoolSize << 1)) |
103 { | 101 { |
104 // Reclaim memory if less than half of the pool is unused. | 102 // Reclaim memory if less than half of the pool is unused. |
105 _createdMemory--; | 103 _createdMemory--; |
106 delete memory; | 104 delete memory; |
107 memory = NULL; | 105 memory = NULL; |
108 return 0; | 106 return 0; |
109 } | 107 } |
110 _memoryPool.push_back(memory); | 108 _memoryPool.push_back(memory); |
111 memory = NULL; | 109 memory = NULL; |
112 return 0; | 110 return 0; |
113 } | 111 } |
114 | 112 |
115 template<class MemoryType> | 113 template<class MemoryType> |
116 bool MemoryPoolImpl<MemoryType>::Initialize() | 114 bool MemoryPoolImpl<MemoryType>::Initialize() |
117 { | 115 { |
118 CriticalSectionScoped cs(_crit); | 116 rtc::CritScope cs(&_crit); |
119 return CreateMemory(_initialPoolSize) == 0; | 117 return CreateMemory(_initialPoolSize) == 0; |
120 } | 118 } |
121 | 119 |
122 template<class MemoryType> | 120 template<class MemoryType> |
123 int32_t MemoryPoolImpl<MemoryType>::Terminate() | 121 int32_t MemoryPoolImpl<MemoryType>::Terminate() |
124 { | 122 { |
125 CriticalSectionScoped cs(_crit); | 123 rtc::CritScope cs(&_crit); |
126 assert(_createdMemory == _outstandingMemory + _memoryPool.size()); | 124 assert(_createdMemory == _outstandingMemory + _memoryPool.size()); |
127 | 125 |
128 _terminate = true; | 126 _terminate = true; |
129 // Reclaim all memory. | 127 // Reclaim all memory. |
130 while(_createdMemory > 0) | 128 while(_createdMemory > 0) |
131 { | 129 { |
132 MemoryType* memory = _memoryPool.front(); | 130 MemoryType* memory = _memoryPool.front(); |
133 _memoryPool.pop_front(); | 131 _memoryPool.pop_front(); |
134 delete memory; | 132 delete memory; |
135 _createdMemory--; | 133 _createdMemory--; |
(...skipping 13 matching lines...) Expand all Loading... |
149 return -1; | 147 return -1; |
150 } | 148 } |
151 _memoryPool.push_back(memory); | 149 _memoryPool.push_back(memory); |
152 _createdMemory++; | 150 _createdMemory++; |
153 } | 151 } |
154 return 0; | 152 return 0; |
155 } | 153 } |
156 } // namespace webrtc | 154 } // namespace webrtc |
157 | 155 |
158 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ | 156 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_GENERIC_H_ |
OLD | NEW |