temp-mempool.c revision a3b6849ec9a7e7cb209d0e330211aed6395daabd
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen temp-mempool.c : Memory pool for temporary memory allocations
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen Copyright (c) 2001-2002 Timo Sirainen
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen Permission is hereby granted, free of charge, to any person obtaining
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen a copy of this software and associated documentation files (the
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen "Software"), to deal in the Software without restriction, including
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen without limitation the rights to use, copy, modify, merge, publish,
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen permit persons to whom the Software is furnished to do so, subject to
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen the following conditions:
decdff03c32cb5d0e99d71c5678fd008714de70bTimo Sirainen The above copyright notice and this permission notice shall be
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen included in all copies or substantial portions of the Software.
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
038c2831447440bf0bef89b43dd0968afc298abcStephan Bosch/* #define TEMP_POOL_DISABLE */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen/* max. number of bytes to even try to allocate. This is done just to avoid
038c2831447440bf0bef89b43dd0968afc298abcStephan Bosch allocating less memory than was actually requested because of integer
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen overflows. */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen#define MAX_ALLOC_SIZE (UINT_MAX - (MEM_ALIGN_SIZE-1))
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen/* Initial pool size - this should be kept in a size that doesn't exceed
038c2831447440bf0bef89b43dd0968afc298abcStephan Bosch in a normal use to keep it fast. */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen/* current_stack contains last t_push()ed blocks. After that new
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen MemBlockStack is created and it's ->prev is set to current_stack. */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainenstatic int stack_pos; /* next free position in current_stack->block[] */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainenstatic MemBlockStack *current_stack; /* current stack position */
9766afd8dc3976d238e1c71712d6bafa316f2e06Timo Sirainenstatic MemBlockStack *unused_stack_list; /* unused stack blocks */
9766afd8dc3976d238e1c71712d6bafa316f2e06Timo Sirainenstatic MemBlock *current_block; /* block currently used for allocation */
9766afd8dc3976d238e1c71712d6bafa316f2e06Timo Sirainenstatic MemBlock *unused_block; /* largest unused block is kept here */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainenstatic unsigned int last_buffer_size;
29cda2d6eb57b7fb65a55115ea8bcb6ab6938477Timo Sirainen /* stack list full */
29cda2d6eb57b7fb65a55115ea8bcb6ab6938477Timo Sirainen /* allocate new stack */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen /* use existing unused stack */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen /* mark our current position */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen current_stack->block[stack_pos] = current_block;
decdff03c32cb5d0e99d71c5678fd008714de70bTimo Sirainen current_stack->block_space_used[stack_pos] = current_block->left;
8e0eb6a0cfc2cb66385bdf05a70a38d753bc1e24Timo Sirainen /* free all the blocks, except if any of them is bigger than
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen unused_block, replace it */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen if (unused_block == NULL || block->size > unused_block->size) {
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen /* update the current block */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen current_block = current_stack->block[stack_pos];
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen current_block->left = current_stack->block_space_used[stack_pos];
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen /* free unused blocks */
854b4074ac77a138b3983d72510b4d8779d15040Timo Sirainen /* stack block is now unused, add it to unused list */
return stack_pos;
return block;
void *ret;
if (size == 0)
return NULL;
if (permanent)
return ret;
if (permanent)
void *mem;
return mem;
return TRUE;
return FALSE;
void *ret;
return ret;
unsigned int old_size;
void *new_buffer;
return buffer;
return new_buffer;
void temp_mempool_init(void)
t_push();
last_alloc_size = 0;
last_buffer_size = 0;
void temp_mempool_deinit(void)
t_pop();
struct _Stack {
struct _Alloc {
void *mem;
static int stack_counter;
static void *buffer_mem;
int t_push(void)
return stack_counter++;
int t_pop(void)
return --stack_counter;
void *mem;
return mem;
void *mem;
return mem;
void *new_mem;
return TRUE;
return FALSE;
return buffer_mem;
return buffer_mem;
void *mem;
void temp_mempool_init(void)
stack_counter = 0;
t_push();
void temp_mempool_deinit(void)
t_pop();
if (stack_counter != 0)