Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/src/common/pa_allocation.c

raw symbols SCIP
1 /*
2 * $Id: pa_allocation.c,v 1.1 2006/06/10 21:30:55 dmazzoni Exp $
3 * Portable Audio I/O Library allocation group implementation
4 * memory allocation group for tracking allocation groups
5 *
6 * Based on the Open Source API proposed by Ross Bencina
7 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files
11 * (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * Any person wishing to distribute modifications to the Software is
21 * requested to send the modifications to the original developer so that
22 * they can be incorporated into the canonical version.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 /** @file
34 @brief Allocation Group implementation.
35 */
36
37
38 #include "pa_allocation.h"
39 #include "pa_util.h"
40
41
42 /*
43 Maintain 3 singly linked lists...
44 linkBlocks: the buffers used to allocate the links
45 spareLinks: links available for use in the allocations list
46 allocations: the buffers currently allocated using PaUtil_ContextAllocateMemory()
47
48 Link block size is doubled every time new links are allocated.
49 */
50
51
52 #define PA_INITIAL_LINK_COUNT_ 16
53
54 struct PaUtilAllocationGroupLink
55 {
56 struct PaUtilAllocationGroupLink *next;
57 void *buffer;
58 };
59
60 /*
61 Allocate a block of links. The first link will have it's buffer member
62 pointing to the block, and it's next member set to <nextBlock>. The remaining
63 links will have NULL buffer members, and each link will point to
64 the next link except the last, which will point to <nextSpare>
65 */
66 static struct PaUtilAllocationGroupLink *AllocateLinks( long count,
67 struct PaUtilAllocationGroupLink *nextBlock,
68 struct PaUtilAllocationGroupLink *nextSpare )
69 {
70 struct PaUtilAllocationGroupLink *result;
71 int i;
72
73 result = (struct PaUtilAllocationGroupLink *)PaUtil_AllocateMemory(
74 sizeof(struct PaUtilAllocationGroupLink) * count );
75 if( result )
76 {
77 /* the block link */
78 result[0].buffer = result;
79 result[0].next = nextBlock;
80
81 /* the spare links */
82 for( i=1; i<count; ++i )
83 {
84 result[i].buffer = 0;
85 result[i].next = &result[i+1];
86 }
87 result[count-1].next = nextSpare;
88 }
89
90 return result;
91 }
92
93
94 PaUtilAllocationGroup* PaUtil_CreateAllocationGroup( void )
95 {
96 PaUtilAllocationGroup* result = 0;
97 struct PaUtilAllocationGroupLink *links;
98
99
100 links = AllocateLinks( PA_INITIAL_LINK_COUNT_, 0, 0 );
101 if( links != 0 )
102 {
103 result = (PaUtilAllocationGroup*)PaUtil_AllocateMemory( sizeof(PaUtilAllocationGroup) );
104 if( result )
105 {
106 result->linkCount = PA_INITIAL_LINK_COUNT_;
107 result->linkBlocks = &links[0];
108 result->spareLinks = &links[1];
109 result->allocations = 0;
110 }
111 else
112 {
113 PaUtil_FreeMemory( links );
114 }
115 }
116
117 return result;
118 }
119
120
121 void PaUtil_DestroyAllocationGroup( PaUtilAllocationGroup* group )
122 {
123 struct PaUtilAllocationGroupLink *current = group->linkBlocks;
124 struct PaUtilAllocationGroupLink *next;
125
126 while( current )
127 {
128 next = current->next;
129 PaUtil_FreeMemory( current->buffer );
130 current = next;
131 }
132
133 PaUtil_FreeMemory( group );
134 }
135
136
137 void* PaUtil_GroupAllocateMemory( PaUtilAllocationGroup* group, long size )
138 {
139 struct PaUtilAllocationGroupLink *links, *link;
140 void *result = 0;
141
142 /* allocate more links if necessary */
143 if( !group->spareLinks )
144 {
145 /* double the link count on each block allocation */
146 links = AllocateLinks( group->linkCount, group->linkBlocks, group->spareLinks );
147 if( links )
148 {
149 group->linkCount += group->linkCount;
150 group->linkBlocks = &links[0];
151 group->spareLinks = &links[1];
152 }
153 }
154
155 if( group->spareLinks )
156 {
157 result = PaUtil_AllocateMemory( size );
158 if( result )
159 {
160 link = group->spareLinks;
161 group->spareLinks = link->next;
162
163 link->buffer = result;
164 link->next = group->allocations;
165
166 group->allocations = link;
167 }
168 }
169
170 return result;
171 }
172
173
174 void PaUtil_GroupFreeMemory( PaUtilAllocationGroup* group, void *buffer )
175 {
176 struct PaUtilAllocationGroupLink *current = group->allocations;
177 struct PaUtilAllocationGroupLink *previous = 0;
178
179 if( buffer == 0 )
180 return;
181
182 /* find the right link and remove it */
183 while( current )
184 {
185 if( current->buffer == buffer )
186 {
187 if( previous )
188 {
189 previous->next = current->next;
190 }
191 else
192 {
193 group->allocations = current->next;
194 }
195
196 current->buffer = 0;
197 current->next = group->spareLinks;
198 group->spareLinks = current;
199
200 break;
201 }
202
203 previous = current;
204 current = current->next;
205 }
206
207 PaUtil_FreeMemory( buffer ); /* free the memory whether we found it in the list or not */
208 }
209
210
211 void PaUtil_FreeAllAllocations( PaUtilAllocationGroup* group )
212 {
213 struct PaUtilAllocationGroupLink *current = group->allocations;
214 struct PaUtilAllocationGroupLink *previous = 0;
215
216 /* free all buffers in the allocations list */
217 while( current )
218 {
219 PaUtil_FreeMemory( current->buffer );
220 current->buffer = 0;
221
222 previous = current;
223 current = current->next;
224 }
225
226 /* link the former allocations list onto the front of the spareLinks list */
227 if( previous )
228 {
229 previous->next = group->spareLinks;
230 group->spareLinks = group->allocations;
231 group->allocations = 0;
232 }
233 }
234