comparison extra_rwops.c @ 485:137c0b00ea4c

Added Sound_NewSampleFromMem(), and implementation of RWops pooling.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 27 Feb 2005 19:53:25 +0000
parents 47cc2de2ae36
children c7ab783f05d3
comparison
equal deleted inserted replaced
484:1c09756bc038 485:137c0b00ea4c
128 128
129 return(retval); 129 return(retval);
130 } /* RWops_RWRefCounter_new */ 130 } /* RWops_RWRefCounter_new */
131 131
132 132
133
134 /*
135 * RWops pooling...
136 */
137
138 static SDL_RWops *rwops_pool = NULL;
139 static SDL_mutex *rwops_pool_mutex = NULL;
140
141 int RWops_pooled_init(void)
142 {
143 const int preallocate = 50;
144 int i;
145
146 rwops_pool_mutex = SDL_CreateMutex();
147 if (rwops_pool_mutex == NULL)
148 return(0);
149
150 for (i = 0; i < preallocate; i++)
151 free_pooled_rwops(alloc_pooled_rwops());
152
153 return(1);
154 } /* RWops_pooled_init */
155
156
157 void RWops_pooled_deinit(void)
158 {
159 SDL_RWops *cur;
160 SDL_RWops *next;
161
162 if (rwops_pool_mutex == NULL)
163 return; /* never initialized. */
164
165 SDL_LockMutex(rwops_pool_mutex);
166 /* all allocated rwops must be in the pool now, or the memory leaks. */
167 cur = rwops_pool;
168 rwops_pool = NULL;
169 SDL_UnlockMutex(rwops_pool_mutex);
170 SDL_DestroyMutex(rwops_pool_mutex);
171 rwops_pool_mutex = NULL;
172
173 while (cur)
174 {
175 next = (SDL_RWops *) (cur->hidden.unknown.data1);
176 free(cur);
177 cur = next;
178 } /* while */
179 } /* RWops_pooled_deinit */
180
181
182 SDL_RWops *RWops_pooled_alloc(void)
183 {
184 SDL_RWops *rw;
185 if (rwops_pool_mutex == NULL)
186 return(NULL); /* never initialized. */
187
188 SDL_LockMutex(rwops_pool_mutex);
189 rw = rwops_pool;
190 if (rw)
191 rwops_pool = (SDL_RWops *) (rw->hidden.unknown.data1);
192 SDL_UnlockMutex(rwops_pool_mutex);
193
194 if (!rw)
195 rw = (SDL_RWops *) malloc(sizeof (SDL_RWops));
196
197 return(rw);
198 } /* RWops_pooled_alloc */
199
200
201 void RWops_pooled_free(SDL_RWops *rw)
202 {
203 if (rwops_pool_mutex == NULL)
204 return; /* never initialized...why are we here? */
205
206 if (rw == NULL)
207 return;
208
209 SDL_LockMutex(rwops_pool_mutex);
210 rw->hidden.unknown.data1 = rwops_pool;
211 rwops_pool = rw;
212 SDL_UnlockMutex(rwops_pool_mutex);
213 } /* RWops_pooled_free */
214
133 /* end of extra_rwops.c ... */ 215 /* end of extra_rwops.c ... */
134 216
135 217