From 013df98f79866a75f367853c9e436f3c5c79f645 Mon Sep 17 00:00:00 2001 From: rubidium Date: Tue, 2 Jan 2007 19:19:48 +0000 Subject: (svn r7759) -Merge: makefile rewrite. This merge features: - A proper ./configure, so everything needs to be configured only once, not for every make. - Usage of makedepend when available. This greatly reduces the time needed for generating the dependencies. - A generator for all project files. There is a single file with sources, which is used to generate Makefiles and the project files for MSVC. - Proper support for OSX universal binaries. - Object files for non-MSVC compiles are also placed in separate directories, making is faster to switch between debug and release compiles and it does not touch the directory with the source files. - Functionality to make a bundle of all needed files for for example a nightly or distribution of a binary with all needed GRFs and language files. Note: as this merge moves almost all files, it is recommended to make a backup of your working copy before updating your working copy. --- src/oldpool.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/oldpool.c (limited to 'src/oldpool.c') diff --git a/src/oldpool.c b/src/oldpool.c new file mode 100644 index 000000000..2b2d188e8 --- /dev/null +++ b/src/oldpool.c @@ -0,0 +1,86 @@ +/* $Id$ */ + +#include "stdafx.h" +#include "openttd.h" +#include "debug.h" +#include "functions.h" +#include "oldpool.h" + +/** + * Clean a pool in a safe way (does free all blocks) + */ +void CleanPool(OldMemoryPool *pool) +{ + uint i; + + DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", pool->name); + + /* Free all blocks */ + for (i = 0; i < pool->current_blocks; i++) { + if (pool->clean_block_proc != NULL) { + pool->clean_block_proc(i * (1 << pool->block_size_bits), (i + 1) * (1 << pool->block_size_bits) - 1); + } + free(pool->blocks[i]); + } + + /* Free the block itself */ + free(pool->blocks); + + /* Clear up some critical data */ + pool->total_items = 0; + pool->current_blocks = 0; + pool->blocks = NULL; +} + +/** + * This function tries to increase the size of array by adding + * 1 block too it + * + * @return Returns false if the pool could not be increased + */ +bool AddBlockToPool(OldMemoryPool *pool) +{ + /* Is the pool at his max? */ + if (pool->max_blocks == pool->current_blocks) + return false; + + pool->total_items = (pool->current_blocks + 1) * (1 << pool->block_size_bits); + + 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); + + /* Increase the poolsize */ + pool->blocks = realloc(pool->blocks, sizeof(pool->blocks[0]) * (pool->current_blocks + 1)); + if (pool->blocks == NULL) error("Pool: (%s) could not allocate memory for blocks", pool->name); + + /* Allocate memory to the new block item */ + pool->blocks[pool->current_blocks] = malloc(pool->item_size * (1 << pool->block_size_bits)); + if (pool->blocks[pool->current_blocks] == NULL) + error("Pool: (%s) could not allocate memory for blocks", pool->name); + + /* Clean the content of the new block */ + memset(pool->blocks[pool->current_blocks], 0, pool->item_size * (1 << pool->block_size_bits)); + + /* Call a custom function if defined (e.g. to fill indexes) */ + if (pool->new_block_proc != NULL) + pool->new_block_proc(pool->current_blocks * (1 << pool->block_size_bits)); + + /* We have a new block */ + pool->current_blocks++; + + return true; +} + +/** + * Adds blocks to the pool if needed (and possible) till index fits inside the pool + * + * @return Returns false if adding failed + */ +bool AddBlockIfNeeded(OldMemoryPool *pool, uint index) +{ + while (index >= pool->total_items) { + if (!AddBlockToPool(pool)) + return false; + } + + return true; +} -- cgit v1.2.3-70-g09d2