Mercurial > SDL_sound_CoreAudio
diff 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 |
line wrap: on
line diff
--- a/extra_rwops.c Sun Feb 27 19:50:54 2005 +0000 +++ b/extra_rwops.c Sun Feb 27 19:53:25 2005 +0000 @@ -130,6 +130,88 @@ } /* RWops_RWRefCounter_new */ + + /* + * RWops pooling... + */ + +static SDL_RWops *rwops_pool = NULL; +static SDL_mutex *rwops_pool_mutex = NULL; + +int RWops_pooled_init(void) +{ + const int preallocate = 50; + int i; + + rwops_pool_mutex = SDL_CreateMutex(); + if (rwops_pool_mutex == NULL) + return(0); + + for (i = 0; i < preallocate; i++) + free_pooled_rwops(alloc_pooled_rwops()); + + return(1); +} /* RWops_pooled_init */ + + +void RWops_pooled_deinit(void) +{ + SDL_RWops *cur; + SDL_RWops *next; + + if (rwops_pool_mutex == NULL) + return; /* never initialized. */ + + SDL_LockMutex(rwops_pool_mutex); + /* all allocated rwops must be in the pool now, or the memory leaks. */ + cur = rwops_pool; + rwops_pool = NULL; + SDL_UnlockMutex(rwops_pool_mutex); + SDL_DestroyMutex(rwops_pool_mutex); + rwops_pool_mutex = NULL; + + while (cur) + { + next = (SDL_RWops *) (cur->hidden.unknown.data1); + free(cur); + cur = next; + } /* while */ +} /* RWops_pooled_deinit */ + + +SDL_RWops *RWops_pooled_alloc(void) +{ + SDL_RWops *rw; + if (rwops_pool_mutex == NULL) + return(NULL); /* never initialized. */ + + SDL_LockMutex(rwops_pool_mutex); + rw = rwops_pool; + if (rw) + rwops_pool = (SDL_RWops *) (rw->hidden.unknown.data1); + SDL_UnlockMutex(rwops_pool_mutex); + + if (!rw) + rw = (SDL_RWops *) malloc(sizeof (SDL_RWops)); + + return(rw); +} /* RWops_pooled_alloc */ + + +void RWops_pooled_free(SDL_RWops *rw) +{ + if (rwops_pool_mutex == NULL) + return; /* never initialized...why are we here? */ + + if (rw == NULL) + return; + + SDL_LockMutex(rwops_pool_mutex); + rw->hidden.unknown.data1 = rwops_pool; + rwops_pool = rw; + SDL_UnlockMutex(rwops_pool_mutex); +} /* RWops_pooled_free */ + /* end of extra_rwops.c ... */