Line data Source code
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 : #ifdef _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>
50 0 : MemoryPool<MemoryType>::MemoryPool(int32_t initialPoolSize)
51 : {
52 0 : _ptrImpl = new MemoryPoolImpl<MemoryType>(initialPoolSize);
53 0 : }
54 :
55 : template<class MemoryType>
56 0 : MemoryPool<MemoryType>::~MemoryPool()
57 : {
58 0 : delete _ptrImpl;
59 0 : }
60 :
61 : template<class MemoryType> int32_t
62 0 : MemoryPool<MemoryType>::CreateMemoryPool(MemoryPool*& memoryPool,
63 : uint32_t initialPoolSize)
64 : {
65 0 : memoryPool = new MemoryPool(initialPoolSize);
66 0 : if(memoryPool == NULL)
67 : {
68 0 : return -1;
69 : }
70 0 : if(memoryPool->_ptrImpl == NULL)
71 : {
72 0 : delete memoryPool;
73 0 : memoryPool = NULL;
74 0 : return -1;
75 : }
76 0 : if(!memoryPool->_ptrImpl->Initialize())
77 : {
78 0 : delete memoryPool;
79 0 : memoryPool = NULL;
80 0 : return -1;
81 : }
82 0 : return 0;
83 : }
84 :
85 : template<class MemoryType>
86 0 : int32_t MemoryPool<MemoryType>::DeleteMemoryPool(MemoryPool*& memoryPool)
87 : {
88 0 : if(memoryPool == NULL)
89 : {
90 0 : return -1;
91 : }
92 0 : if(memoryPool->_ptrImpl == NULL)
93 : {
94 0 : return -1;
95 : }
96 0 : if(memoryPool->_ptrImpl->Terminate() == -1)
97 : {
98 0 : return -1;
99 : }
100 0 : delete memoryPool;
101 0 : memoryPool = NULL;
102 0 : return 0;
103 : }
104 :
105 : template<class MemoryType>
106 0 : int32_t MemoryPool<MemoryType>::PopMemory(MemoryType*& memory)
107 : {
108 0 : return _ptrImpl->PopMemory(memory);
109 : }
110 :
111 : template<class MemoryType>
112 0 : int32_t MemoryPool<MemoryType>::PushMemory(MemoryType*& memory)
113 : {
114 0 : if(memory == NULL)
115 : {
116 0 : return -1;
117 : }
118 0 : return _ptrImpl->PushMemory(memory);
119 : }
120 : } // namespace webrtc
121 :
122 : #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_H_
|