1
0
Fork 0

(svn r10745) -Codechange: generalize the pool cleanup/initialize functions for stations (in such a manner that they can be used for other pools too).

release/0.6
rubidium 2007-08-01 23:49:06 +00:00
parent 4e96ce3dfe
commit aa78685c99
4 changed files with 59 additions and 40 deletions

View File

@ -92,6 +92,40 @@ static inline bool AddBlockToPool(OldMemoryPoolBase *array) { return array->AddB
static inline bool AddBlockIfNeeded(OldMemoryPoolBase *array, uint index) { return array->AddBlockIfNeeded(index); } static inline bool AddBlockIfNeeded(OldMemoryPoolBase *array, uint index) { return array->AddBlockIfNeeded(index); }
/**
* Generic function to initialize a new block in a pool.
* @param start_item the first item that needs to be initialized
*/
template <typename T, OldMemoryPool<T> *Tpool>
static void PoolNewBlock(uint start_item)
{
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (T *t = Tpool->Get(start_item); t != NULL; t = (t->index + 1U < Tpool->GetSize()) ? Tpool->Get(t->index + 1U) : NULL) {
t->index = start_item++;
t->PreInit();
}
}
/**
* Generic function to free a new block in a pool.
* This function uses QuickFree that is intended to only free memory that would be lost if the pool is freed.
* @param start_item the first item that needs to be cleaned
* @param end_item the last item that needs to be cleaned
*/
template <typename T, OldMemoryPool<T> *Tpool>
static void PoolCleanBlock(uint start_item, uint end_item)
{
for (uint i = start_item; i <= end_item; i++) {
T *t = Tpool->Get(i);
if (t->IsValid()) {
t->QuickFree();
}
}
}
#define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ #define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
enum { \ enum { \
name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \ name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \
@ -115,6 +149,11 @@ static inline bool AddBlockIfNeeded(OldMemoryPoolBase *array, uint index) { retu
#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \ #name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
new_block_proc, clean_block_proc); new_block_proc, clean_block_proc);
#define DEFINE_OLD_POOL_GENERIC(name, type) \
OldMemoryPool<type> _##name##_pool( \
#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
PoolNewBlock<type, &_##name##_pool>, PoolCleanBlock<type, &_##name##_pool>);
#define STATIC_OLD_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \ #define STATIC_OLD_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \
OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \ OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \

View File

@ -64,7 +64,6 @@ Station::~Station()
{ {
DEBUG(station, cDebugCtorLevel, "I-%3d", index); DEBUG(station, cDebugCtorLevel, "I-%3d", index);
DeleteName(string_id);
MarkDirty(); MarkDirty();
RebuildStationLists(); RebuildStationLists();
InvalidateWindowClasses(WC_STATION_LIST); InvalidateWindowClasses(WC_STATION_LIST);
@ -77,21 +76,28 @@ Station::~Station()
/* Subsidies need removal as well */ /* Subsidies need removal as well */
DeleteSubsidyWithStation(index); DeleteSubsidyWithStation(index);
free(speclist);
xy = 0; xy = 0;
for (CargoID c = 0; c < NUM_CARGO; c++) { for (CargoID c = 0; c < NUM_CARGO; c++) {
goods[c].cargo.Truncate(0); goods[c].cargo.Truncate(0);
} }
this->QuickFree();
} }
void* Station::operator new(size_t size) void Station::QuickFree()
{
DeleteName(this->string_id);
free(this->speclist);
}
void *Station::operator new(size_t size)
{ {
Station *st = AllocateRaw(); Station *st = AllocateRaw();
return st; return st;
} }
void* Station::operator new(size_t size, int st_idx) void *Station::operator new(size_t size, int st_idx)
{ {
if (!AddBlockIfNeeded(&_Station_pool, st_idx)) if (!AddBlockIfNeeded(&_Station_pool, st_idx))
error("Stations: failed loading savegame: too many stations"); error("Stations: failed loading savegame: too many stations");
@ -480,7 +486,6 @@ RoadStop::~RoadStop()
xy = INVALID_TILE; xy = INVALID_TILE;
} }
/** Low-level function for allocating a RoadStop on the pool */ /** Low-level function for allocating a RoadStop on the pool */
RoadStop *RoadStop::AllocateRaw() RoadStop *RoadStop::AllocateRaw()
{ {

View File

@ -55,6 +55,9 @@ struct RoadStop {
RoadStop(TileIndex tile); RoadStop(TileIndex tile);
~RoadStop(); ~RoadStop();
void PreInit() { this->xy = INVALID_TILE; }
void QuickFree() {}
void *operator new (size_t size); void *operator new (size_t size);
void operator delete(void *rs); void operator delete(void *rs);
@ -164,12 +167,15 @@ struct Station {
Station(TileIndex tile = 0); Station(TileIndex tile = 0);
~Station(); ~Station();
void PreInit() {}
void QuickFree();
/* normal new/delete operators. Used when building/removing station */ /* normal new/delete operators. Used when building/removing station */
void* operator new (size_t size); void *operator new (size_t size);
void operator delete(void *p); void operator delete(void *p);
/* new/delete operators accepting station index. Used when loading station from savegame. */ /* new/delete operators accepting station index. Used when loading station from savegame. */
void* operator new (size_t size, int st_idx); void *operator new (size_t size, int st_idx);
void operator delete(void *p, int st_idx); void operator delete(void *p, int st_idx);
void AddFacility(byte new_facility_bit, TileIndex facil_xy); void AddFacility(byte new_facility_bit, TileIndex facil_xy);

View File

@ -43,39 +43,8 @@
#include "cargotype.h" #include "cargotype.h"
#include "strings.h" #include "strings.h"
/** DEFINE_OLD_POOL_GENERIC(Station, Station)
* Called if a new block is added to the station-pool DEFINE_OLD_POOL_GENERIC(RoadStop, RoadStop)
*/
static void StationPoolNewBlock(uint start_item)
{
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (Station *st = GetStation(start_item); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) st->index = start_item++;
}
static void StationPoolCleanBlock(uint start_item, uint end_item)
{
for (uint i = start_item; i <= end_item; i++) {
Station *st = GetStation(i);
if (st->IsValid()) st->~Station();
}
}
/**
* Called if a new block is added to the roadstop-pool
*/
static void RoadStopPoolNewBlock(uint start_item)
{
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (RoadStop *rs = GetRoadStop(start_item); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) {
rs->xy = INVALID_TILE;
rs->index = start_item++;
}
}
DEFINE_OLD_POOL(Station, Station, StationPoolNewBlock, StationPoolCleanBlock)
DEFINE_OLD_POOL(RoadStop, RoadStop, RoadStopPoolNewBlock, NULL)
/** /**