1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
---|
2 | * vi:set ts=8: |
---|
3 | * |
---|
4 | * al_mspool.c |
---|
5 | * |
---|
6 | * Prototypes, macros and definitions related to the management of mspool |
---|
7 | * objects. mspool objects are objects which ease the slab allocation of |
---|
8 | * _alMixSource objects. |
---|
9 | */ |
---|
10 | #include "al_debug.h" |
---|
11 | #include "al_mspool.h" |
---|
12 | |
---|
13 | #include <stdlib.h> |
---|
14 | |
---|
15 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
---|
16 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
---|
17 | |
---|
18 | /* |
---|
19 | * _alMixPoolResize( _alMixPool *mspool, size_t newsize ) |
---|
20 | * |
---|
21 | * Initializes an already allocated _alMixPool object. Returns AL_TRUE, |
---|
22 | * inless initialization failed for some reason, in which case AL_FALSE is |
---|
23 | * returned. |
---|
24 | * |
---|
25 | * After a successful initialization, mspool will have the capacity for at |
---|
26 | * least newsize _alMixPoolNodes. |
---|
27 | */ |
---|
28 | ALboolean _alMixPoolResize(_alMixPool *spool, size_t newsize) |
---|
29 | { |
---|
30 | _alMixPoolNode *temp; |
---|
31 | unsigned int i; |
---|
32 | |
---|
33 | newsize = MAX( newsize, 1 ); |
---|
34 | |
---|
35 | if(spool->size >= newsize) |
---|
36 | { |
---|
37 | return AL_TRUE; /* no resize needed */ |
---|
38 | } |
---|
39 | |
---|
40 | if(spool->pool == NULL) |
---|
41 | { |
---|
42 | spool->pool = malloc(newsize * sizeof *spool->pool); |
---|
43 | } |
---|
44 | else |
---|
45 | { |
---|
46 | temp = realloc(spool->pool, newsize * sizeof(_alMixPoolNode)); |
---|
47 | if(temp == NULL) |
---|
48 | { |
---|
49 | return AL_FALSE; /* could not realloc */ |
---|
50 | } |
---|
51 | |
---|
52 | spool->pool = temp; |
---|
53 | } |
---|
54 | |
---|
55 | for(i = spool->size; i < newsize; i++) |
---|
56 | { |
---|
57 | spool->pool[i].inuse = AL_FALSE; |
---|
58 | } |
---|
59 | |
---|
60 | spool->size = newsize; |
---|
61 | |
---|
62 | return AL_TRUE; |
---|
63 | } |
---|
64 | |
---|
65 | /* |
---|
66 | * _alMixPoolAlloc( _alMixPool *mspool ) |
---|
67 | * |
---|
68 | * Initializes an already allocated _alMixPool object. Returns index suitable |
---|
69 | * for calls which expect an index, or -1 on error. |
---|
70 | */ |
---|
71 | int _alMixPoolAlloc( _alMixPool *spool ) |
---|
72 | { |
---|
73 | int msindex; |
---|
74 | |
---|
75 | msindex = _alMixPoolFirstFreeIndex(spool); |
---|
76 | if(msindex == -1) |
---|
77 | { |
---|
78 | if(_alMixPoolResize(spool, spool->size * 2) == AL_FALSE) |
---|
79 | { |
---|
80 | return -1; |
---|
81 | } |
---|
82 | |
---|
83 | msindex = _alMixPoolFirstFreeIndex(spool); |
---|
84 | } |
---|
85 | |
---|
86 | spool->pool[msindex].inuse = AL_TRUE; |
---|
87 | |
---|
88 | return msindex; |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * _alMixPoolIndex( _alMixPool *mspool, int msindex ) |
---|
93 | * |
---|
94 | * Return _alMixSource from mspool using simple index, or NULL if msindex is |
---|
95 | * not a valid index or has not been flagged for use. |
---|
96 | */ |
---|
97 | _alMixSource *_alMixPoolIndex(_alMixPool *spool, int msindex) |
---|
98 | { |
---|
99 | if( msindex > (int) spool->size ) |
---|
100 | { |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | |
---|
104 | if(spool->pool[msindex].inuse == AL_FALSE) |
---|
105 | { |
---|
106 | return NULL; |
---|
107 | } |
---|
108 | |
---|
109 | return &spool->pool[msindex].data; |
---|
110 | } |
---|
111 | |
---|
112 | /* |
---|
113 | * _alMixPoolFirstFreeIndex( _alMixPool *mspool ) |
---|
114 | * |
---|
115 | * Returns first available index in mspool, or -1 if nothing is available. |
---|
116 | */ |
---|
117 | int _alMixPoolFirstFreeIndex(_alMixPool *spool) |
---|
118 | { |
---|
119 | ALuint i; |
---|
120 | |
---|
121 | for(i = 0; i < spool->size; i++) |
---|
122 | { |
---|
123 | if(spool->pool[i].inuse == AL_FALSE) |
---|
124 | { |
---|
125 | return i; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * _alMixPoolDealloc( _alMixPool *mspool, int msindex, |
---|
134 | * void (*freer_func)(void *)) |
---|
135 | * |
---|
136 | * Finalize a _alMixSource, indexed by msindex, using freer_func, |
---|
137 | * from mspool, marking is as not in use. |
---|
138 | */ |
---|
139 | ALboolean _alMixPoolDealloc( _alMixPool *spool, int msindex, |
---|
140 | void (*freer_func)(void *) ) { |
---|
141 | _alMixSource *src; |
---|
142 | |
---|
143 | if( msindex < 0 ) { |
---|
144 | return AL_FALSE; |
---|
145 | } |
---|
146 | |
---|
147 | src = _alMixPoolIndex( spool, msindex ); |
---|
148 | if(src == NULL) { |
---|
149 | _alDebug(ALD_MIXER, __FILE__, __LINE__, |
---|
150 | "%d is a bad index", msindex); |
---|
151 | |
---|
152 | return AL_FALSE; |
---|
153 | } |
---|
154 | |
---|
155 | spool->pool[msindex].inuse = AL_FALSE; |
---|
156 | |
---|
157 | freer_func(src); |
---|
158 | |
---|
159 | return AL_TRUE; |
---|
160 | } |
---|
161 | |
---|
162 | /* |
---|
163 | * _alMixPoolFree( _alMixPool *mspool, void (*freer_func)(void *) ) |
---|
164 | * |
---|
165 | * Finalizes each _alMixSource in the _alMixPool object, using freer_func. |
---|
166 | */ |
---|
167 | void _alMixPoolFree(_alMixPool *spool, void (*freer_func)(void *)) |
---|
168 | { |
---|
169 | unsigned int i; |
---|
170 | |
---|
171 | for(i = 0; i < spool->size; i++) |
---|
172 | { |
---|
173 | if(spool->pool[i].inuse == AL_TRUE) |
---|
174 | { |
---|
175 | _alMixPoolDealloc( spool, i, freer_func ); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | free( spool->pool ); |
---|
180 | spool->pool = NULL; |
---|
181 | |
---|
182 | spool->size = 0; |
---|
183 | |
---|
184 | /* let the caller free spool itself */ |
---|
185 | |
---|
186 | return; |
---|
187 | } |
---|