2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_WINDOWS_H_
12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_WINDOWS_H_
17 #include "webrtc/system_wrappers/interface/aligned_malloc.h"
18 #include "webrtc/system_wrappers/interface/atomic32.h"
19 #include "webrtc/typedefs.h"
22 template<class MemoryType> struct MemoryPoolItem;
24 template<class MemoryType>
25 struct MemoryPoolItemPayload
27 MemoryPoolItemPayload()
32 MemoryType memoryType;
33 MemoryPoolItem<MemoryType>* base;
36 template<class MemoryType>
39 // Atomic single linked list entry header.
40 SLIST_ENTRY itemEntry;
41 // Atomic single linked list payload.
42 MemoryPoolItemPayload<MemoryType>* payload;
45 template<class MemoryType>
49 // MemoryPool functions.
50 int32_t PopMemory(MemoryType*& memory);
51 int32_t PushMemory(MemoryType*& memory);
53 MemoryPoolImpl(int32_t /*initialPoolSize*/);
60 // Non-atomic function.
61 MemoryPoolItem<MemoryType>* CreateMemory();
63 // Windows implementation of single linked atomic list, documented here:
64 // http://msdn.microsoft.com/en-us/library/ms686962(VS.85).aspx
66 // Atomic single linked list head.
67 PSLIST_HEADER _pListHead;
69 Atomic32 _createdMemory;
70 Atomic32 _outstandingMemory;
73 template<class MemoryType>
74 MemoryPoolImpl<MemoryType>::MemoryPoolImpl(
75 int32_t /*initialPoolSize*/)
82 template<class MemoryType>
83 MemoryPoolImpl<MemoryType>::~MemoryPoolImpl()
86 if(_pListHead != NULL)
88 AlignedFree(reinterpret_cast<void*>(_pListHead));
91 // Trigger assert if there is outstanding memory.
92 assert(_createdMemory.Value() == 0);
93 assert(_outstandingMemory.Value() == 0);
96 template<class MemoryType>
97 int32_t MemoryPoolImpl<MemoryType>::PopMemory(MemoryType*& memory)
99 PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(_pListHead);
100 if(pListEntry == NULL)
102 MemoryPoolItem<MemoryType>* item = CreateMemory();
107 pListEntry = &(item->itemEntry);
109 ++_outstandingMemory;
110 memory = &((MemoryPoolItem<MemoryType>*)pListEntry)->payload->memoryType;
114 template<class MemoryType>
115 int32_t MemoryPoolImpl<MemoryType>::PushMemory(MemoryType*& memory)
122 MemoryPoolItem<MemoryType>* item =
123 ((MemoryPoolItemPayload<MemoryType>*)memory)->base;
125 const int32_t usedItems = --_outstandingMemory;
126 const int32_t totalItems = _createdMemory.Value();
127 const int32_t freeItems = totalItems - usedItems;
131 delete item->payload;
135 if(freeItems >= totalItems>>1)
137 delete item->payload;
142 InterlockedPushEntrySList(_pListHead,&(item->itemEntry));
146 template<class MemoryType>
147 bool MemoryPoolImpl<MemoryType>::Initialize()
149 _pListHead = (PSLIST_HEADER)AlignedMalloc(sizeof(SLIST_HEADER),
150 MEMORY_ALLOCATION_ALIGNMENT);
151 if(_pListHead == NULL)
155 InitializeSListHead(_pListHead);
159 template<class MemoryType>
160 int32_t MemoryPoolImpl<MemoryType>::Terminate()
162 int32_t itemsFreed = 0;
163 PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(_pListHead);
164 while(pListEntry != NULL)
166 MemoryPoolItem<MemoryType>* item = ((MemoryPoolItem<MemoryType>*)pListEntry);
167 delete item->payload;
171 pListEntry = InterlockedPopEntrySList(_pListHead);
176 template<class MemoryType>
177 MemoryPoolItem<MemoryType>* MemoryPoolImpl<MemoryType>::CreateMemory()
179 MemoryPoolItem<MemoryType>* returnValue = (MemoryPoolItem<MemoryType>*)
180 AlignedMalloc(sizeof(MemoryPoolItem<MemoryType>),
181 MEMORY_ALLOCATION_ALIGNMENT);
182 if(returnValue == NULL)
187 returnValue->payload = new MemoryPoolItemPayload<MemoryType>();
188 if(returnValue->payload == NULL)
193 returnValue->payload->base = returnValue;
197 } // namespace webrtc
199 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_WINDOWS_H_