/* Allocates one element from the pool specified. */ void * pool_alloc (alloc_pool pool) { alloc_pool_list header; char *block; #ifdef GATHER_STATISTICS struct alloc_pool_descriptor *desc = alloc_pool_descriptor (pool->name); desc->allocated+=pool->elt_size; #endif gcc_assert (pool); /* If there are no more free elements, make some more!. */ if (!pool->free_list) { size_t i; alloc_pool_list block_header; /* Make the block. */ block = XNEWVEC (char, pool->block_size); block_header = (alloc_pool_list) block; block += align_eight (sizeof (struct alloc_pool_list_def)); #ifdef GATHER_STATISTICS desc->current += pool->block_size; if (desc->peak < desc->current) desc->peak = desc->current; #endif /* Throw it on the block list. */ block_header->next = pool->block_list; pool->block_list = block_header; /* Now put the actual block pieces onto the free list. */ for (i = 0; i < pool->elts_per_block; i++, block += pool->elt_size) { #ifdef ENABLE_CHECKING /* Mark the element to be free. */ ((allocation_object *) block)->id = 0; #endif header = (alloc_pool_list) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block); header->next = pool->free_list; pool->free_list = header; } /* Also update the number of elements we have free/allocated, and increment the allocated block count. */ pool->elts_allocated += pool->elts_per_block; pool->elts_free += pool->elts_per_block; pool->blocks_allocated += 1; }
/* Allocates one element from the pool specified. */ void * pool_alloc (alloc_pool pool) { alloc_pool_list header; #ifdef ENABLE_VALGRIND_CHECKING int size; #endif if (GATHER_STATISTICS) { struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name); desc->allocated += pool->elt_size; desc->current += pool->elt_size; if (desc->peak < desc->current) desc->peak = desc->current; } gcc_checking_assert (pool); #ifdef ENABLE_VALGRIND_CHECKING size = pool->elt_size - offsetof (allocation_object, u.data); #endif /* If there are no more free elements, make some more!. */ if (!pool->returned_free_list) { char *block; if (!pool->virgin_elts_remaining) { alloc_pool_list block_header; /* Make the block. */ block = XNEWVEC (char, pool->block_size); block_header = (alloc_pool_list) block; block += align_eight (sizeof (struct alloc_pool_list_def)); /* Throw it on the block list. */ block_header->next = pool->block_list; pool->block_list = block_header; /* Make the block available for allocation. */ pool->virgin_free_list = block; pool->virgin_elts_remaining = pool->elts_per_block; /* Also update the number of elements we have free/allocated, and increment the allocated block count. */ pool->elts_allocated += pool->elts_per_block; pool->elts_free += pool->elts_per_block; pool->blocks_allocated += 1; }
alloc_pool create_alloc_pool (const char *name, size_t size, size_t num) { alloc_pool pool; size_t pool_size, header_size; #ifdef GATHER_STATISTICS struct alloc_pool_descriptor *desc; #endif gcc_assert (name); /* Make size large enough to store the list header. */ if (size < sizeof (alloc_pool_list)) size = sizeof (alloc_pool_list); /* Now align the size to a multiple of 4. */ size = align_eight (size); #ifdef ENABLE_CHECKING /* Add the aligned size of ID. */ size += offsetof (allocation_object, u.data); #endif /* Um, we can't really allocate 0 elements per block. */ gcc_assert (num); /* Find the size of the pool structure, and the name. */ pool_size = sizeof (struct alloc_pool_def); /* and allocate that much memory. */ pool = xmalloc (pool_size); /* Now init the various pieces of our pool structure. */ pool->name = /*xstrdup (name)*/name; #ifdef GATHER_STATISTICS desc = alloc_pool_descriptor (name); desc->created++; #endif pool->elt_size = size; pool->elts_per_block = num; /* List header size should be a multiple of 8. */ header_size = align_eight (sizeof (struct alloc_pool_list_def)); pool->block_size = (size * num) + header_size; pool->free_list = NULL; pool->elts_allocated = 0; pool->elts_free = 0; pool->blocks_allocated = 0; pool->block_list = NULL; #ifdef ENABLE_CHECKING /* Increase the last used ID and use it for this pool. ID == 0 is used for free elements of pool so skip it. */ last_id++; if (last_id == 0) last_id++; pool->id = last_id; #endif return (pool); }