Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: webrtc/modules/audio_mixer/source/memory_pool_win.h

Issue 2109133003: Added empty directory with myself as owner for new mixer. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Renamed to avoid compilation crashes and added build file. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 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_WINDOWS_H_
12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_WINDOWS_H_
13
14 #include <assert.h>
15 #include <windows.h>
16
17 #include "webrtc/system_wrappers/include/aligned_malloc.h"
18 #include "webrtc/system_wrappers/include/atomic32.h"
19 #include "webrtc/typedefs.h"
20
21 namespace webrtc {
22 template <class MemoryType>
23 struct MemoryPoolItem;
24
25 template <class MemoryType>
26 struct MemoryPoolItemPayload {
27 MemoryPoolItemPayload() : memoryType(), base(NULL) {}
28 MemoryType memoryType;
29 MemoryPoolItem<MemoryType>* base;
30 };
31
32 template <class MemoryType>
33 struct MemoryPoolItem {
34 // Atomic single linked list entry header.
35 SLIST_ENTRY itemEntry;
36 // Atomic single linked list payload.
37 MemoryPoolItemPayload<MemoryType>* payload;
38 };
39
40 template <class MemoryType>
41 class MemoryPoolImpl {
42 public:
43 // MemoryPool functions.
44 int32_t PopMemory(MemoryType*& memory);
45 int32_t PushMemory(MemoryType*& memory);
46
47 MemoryPoolImpl(int32_t /*initialPoolSize*/);
48 ~MemoryPoolImpl();
49
50 // Atomic functions.
51 int32_t Terminate();
52 bool Initialize();
53
54 private:
55 // Non-atomic function.
56 MemoryPoolItem<MemoryType>* CreateMemory();
57
58 // Windows implementation of single linked atomic list, documented here:
59 // http://msdn.microsoft.com/en-us/library/ms686962(VS.85).aspx
60
61 // Atomic single linked list head.
62 PSLIST_HEADER _pListHead;
63
64 Atomic32 _createdMemory;
65 Atomic32 _outstandingMemory;
66 };
67
68 template <class MemoryType>
69 MemoryPoolImpl<MemoryType>::MemoryPoolImpl(int32_t /*initialPoolSize*/)
70 : _pListHead(NULL), _createdMemory(0), _outstandingMemory(0) {}
71
72 template <class MemoryType>
73 MemoryPoolImpl<MemoryType>::~MemoryPoolImpl() {
74 Terminate();
75 if (_pListHead != NULL) {
76 AlignedFree(reinterpret_cast<void*>(_pListHead));
77 _pListHead = NULL;
78 }
79 // Trigger assert if there is outstanding memory.
80 assert(_createdMemory.Value() == 0);
81 assert(_outstandingMemory.Value() == 0);
82 }
83
84 template <class MemoryType>
85 int32_t MemoryPoolImpl<MemoryType>::PopMemory(MemoryType*& memory) {
86 PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(_pListHead);
87 if (pListEntry == NULL) {
88 MemoryPoolItem<MemoryType>* item = CreateMemory();
89 if (item == NULL) {
90 return -1;
91 }
92 pListEntry = &(item->itemEntry);
93 }
94 ++_outstandingMemory;
95 memory = &((MemoryPoolItem<MemoryType>*)pListEntry)->payload->memoryType;
96 return 0;
97 }
98
99 template <class MemoryType>
100 int32_t MemoryPoolImpl<MemoryType>::PushMemory(MemoryType*& memory) {
101 if (memory == NULL) {
102 return -1;
103 }
104
105 MemoryPoolItem<MemoryType>* item =
106 ((MemoryPoolItemPayload<MemoryType>*)memory)->base;
107
108 const int32_t usedItems = --_outstandingMemory;
109 const int32_t totalItems = _createdMemory.Value();
110 const int32_t freeItems = totalItems - usedItems;
111 if (freeItems < 0) {
112 assert(false);
113 delete item->payload;
114 AlignedFree(item);
115 return -1;
116 }
117 if (freeItems >= totalItems >> 1) {
118 delete item->payload;
119 AlignedFree(item);
120 --_createdMemory;
121 return 0;
122 }
123 InterlockedPushEntrySList(_pListHead, &(item->itemEntry));
124 return 0;
125 }
126
127 template <class MemoryType>
128 bool MemoryPoolImpl<MemoryType>::Initialize() {
129 _pListHead = (PSLIST_HEADER)AlignedMalloc(sizeof(SLIST_HEADER),
130 MEMORY_ALLOCATION_ALIGNMENT);
131 if (_pListHead == NULL) {
132 return false;
133 }
134 InitializeSListHead(_pListHead);
135 return true;
136 }
137
138 template <class MemoryType>
139 int32_t MemoryPoolImpl<MemoryType>::Terminate() {
140 int32_t itemsFreed = 0;
141 PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(_pListHead);
142 while (pListEntry != NULL) {
143 MemoryPoolItem<MemoryType>* item =
144 ((MemoryPoolItem<MemoryType>*)pListEntry);
145 delete item->payload;
146 AlignedFree(item);
147 --_createdMemory;
148 itemsFreed++;
149 pListEntry = InterlockedPopEntrySList(_pListHead);
150 }
151 return itemsFreed;
152 }
153
154 template <class MemoryType>
155 MemoryPoolItem<MemoryType>* MemoryPoolImpl<MemoryType>::CreateMemory() {
156 MemoryPoolItem<MemoryType>* returnValue =
157 (MemoryPoolItem<MemoryType>*)AlignedMalloc(
158 sizeof(MemoryPoolItem<MemoryType>), MEMORY_ALLOCATION_ALIGNMENT);
159 if (returnValue == NULL) {
160 return NULL;
161 }
162
163 returnValue->payload = new MemoryPoolItemPayload<MemoryType>();
164 if (returnValue->payload == NULL) {
165 delete returnValue;
166 return NULL;
167 }
168 returnValue->payload->base = returnValue;
169 ++_createdMemory;
170 return returnValue;
171 }
172 } // namespace webrtc
173
174 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_MEMORY_POOL_WINDOWS_H_
OLDNEW
« no previous file with comments | « webrtc/modules/audio_mixer/source/memory_pool_posix.h ('k') | webrtc/modules/audio_mixer/source/time_scheduler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698