2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-21 15:19:33 +00:00
|
|
|
/** @file oldpool.cpp */
|
|
|
|
|
2005-02-01 18:30:11 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2006-12-03 17:27:43 +00:00
|
|
|
#include "oldpool.h"
|
2007-01-10 18:56:51 +00:00
|
|
|
#include "helpers.hpp"
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean a pool in a safe way (does free all blocks)
|
|
|
|
*/
|
2006-12-03 17:27:43 +00:00
|
|
|
void CleanPool(OldMemoryPool *pool)
|
2005-02-01 18:30:11 +00:00
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", pool->name);
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/* Free all blocks */
|
2006-04-18 18:48:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2005-02-01 18:30:11 +00:00
|
|
|
free(pool->blocks[i]);
|
2006-04-18 18:48:50 +00:00
|
|
|
}
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2006-12-03 17:27:43 +00:00
|
|
|
bool AddBlockToPool(OldMemoryPool *pool)
|
2005-02-01 18:30:11 +00:00
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
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);
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/* Increase the poolsize */
|
2007-01-11 17:29:39 +00:00
|
|
|
pool->blocks = ReallocT(pool->blocks, pool->current_blocks + 1);
|
2006-12-26 17:36:18 +00:00
|
|
|
if (pool->blocks == NULL) error("Pool: (%s) could not allocate memory for blocks", pool->name);
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/* Allocate memory to the new block item */
|
2007-01-11 17:29:39 +00:00
|
|
|
pool->blocks[pool->current_blocks] = MallocT<byte>(pool->item_size * (1 << pool->block_size_bits));
|
2005-02-01 18:30:11 +00:00
|
|
|
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
|
|
|
|
*/
|
2006-12-03 17:27:43 +00:00
|
|
|
bool AddBlockIfNeeded(OldMemoryPool *pool, uint index)
|
2005-02-01 18:30:11 +00:00
|
|
|
{
|
|
|
|
while (index >= pool->total_items) {
|
|
|
|
if (!AddBlockToPool(pool))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|