pool.c
changeset 1259 8d8515e3da29
child 1299 39c06aba09aa
equal deleted inserted replaced
1258:220b6e3b4d10 1259:8d8515e3da29
       
     1 #include "stdafx.h"
       
     2 #include "ttd.h"
       
     3 #include "pool.h"
       
     4 
       
     5 /**
       
     6  * Clean a pool in a safe way (does free all blocks)
       
     7  */
       
     8 void CleanPool(MemoryPool *pool)
       
     9 {
       
    10 	uint i;
       
    11 
       
    12 	DEBUG(misc, 4)("[Pool] (%s) Cleaing pool..", pool->name);
       
    13 
       
    14 	/* Free all blocks */
       
    15 	for (i = 0; i < pool->current_blocks; i++)
       
    16 		free(pool->blocks[i]);
       
    17 
       
    18 	/* Free the block itself */
       
    19 	free(pool->blocks);
       
    20 
       
    21 	/* Clear up some critical data */
       
    22 	pool->total_items = 0;
       
    23 	pool->current_blocks = 0;
       
    24 	pool->blocks = NULL;
       
    25 }
       
    26 
       
    27 /**
       
    28  * This function tries to increase the size of array by adding
       
    29  *  1 block too it
       
    30  *
       
    31  * @return Returns false if the pool could not be increased
       
    32  */
       
    33 bool AddBlockToPool(MemoryPool *pool)
       
    34 {
       
    35 	/* Is the pool at his max? */
       
    36 	if (pool->max_blocks == pool->current_blocks)
       
    37 		return false;
       
    38 
       
    39 	pool->total_items = (pool->current_blocks + 1) * (1 << pool->block_size_bits);
       
    40 
       
    41 	DEBUG(misc, 4)("[Pool] (%s) Increasing size of pool to %d items (%d bytes)", pool->name, pool->total_items, pool->total_items * pool->item_size);
       
    42 
       
    43 	/* Increase the poolsize */
       
    44 	pool->blocks = realloc(pool->blocks, sizeof(pool->blocks[0]) * (pool->current_blocks + 1));
       
    45 	if (pool->blocks == NULL)
       
    46 		error("Pool: (%s) could not allocate memory for blocks", pool->name);
       
    47 
       
    48 	/* Allocate memory to the new block item */
       
    49 	pool->blocks[pool->current_blocks] = malloc(pool->item_size * (1 << pool->block_size_bits));
       
    50 	if (pool->blocks[pool->current_blocks] == NULL)
       
    51 		error("Pool: (%s) could not allocate memory for blocks", pool->name);
       
    52 
       
    53 	/* Clean the content of the new block */
       
    54 	memset(pool->blocks[pool->current_blocks], 0, pool->item_size * (1 << pool->block_size_bits));
       
    55 
       
    56 	/* Call a custom function if defined (e.g. to fill indexes) */
       
    57 	if (pool->new_block_proc != NULL)
       
    58 		pool->new_block_proc(pool->current_blocks * (1 << pool->block_size_bits));
       
    59 
       
    60 	/* We have a new block */
       
    61 	pool->current_blocks++;
       
    62 
       
    63 	return true;
       
    64 }
       
    65 
       
    66 /**
       
    67  * Adds blocks to the pool if needed (and possible) till index fits inside the pool
       
    68  *
       
    69  * @return Returns false if adding failed
       
    70  */
       
    71 bool AddBlockIfNeeded(MemoryPool *pool, uint index)
       
    72 {
       
    73 	while (index >= pool->total_items) {
       
    74 		if (!AddBlockToPool(pool))
       
    75 			return false;
       
    76 	}
       
    77 
       
    78 	return true;
       
    79 }