From ae7ec5cd0d9eb0a137c8a3cf9e7ca99679768a87 Mon Sep 17 00:00:00 2001 From: peter1138 Date: Tue, 18 Apr 2006 18:48:50 +0000 Subject: [PATCH] (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up. --- depot.c | 2 +- engine.c | 2 +- industry_cmd.c | 2 +- order_cmd.c | 2 +- pool.c | 6 +++++- pool.h | 4 ++++ saveload.c | 2 +- signs.c | 2 +- station_cmd.c | 4 ++-- town_cmd.c | 2 +- vehicle.c | 2 +- waypoint.c | 2 +- 12 files changed, 20 insertions(+), 12 deletions(-) diff --git a/depot.c b/depot.c index 2809d5d9f4..8faf3b8bb3 100644 --- a/depot.c +++ b/depot.c @@ -28,7 +28,7 @@ static void DepotPoolNewBlock(uint start_item) } /* Initialize the town-pool */ -MemoryPool _depot_pool = { "Depots", DEPOT_POOL_MAX_BLOCKS, DEPOT_POOL_BLOCK_SIZE_BITS, sizeof(Depot), &DepotPoolNewBlock, 0, 0, NULL }; +MemoryPool _depot_pool = { "Depots", DEPOT_POOL_MAX_BLOCKS, DEPOT_POOL_BLOCK_SIZE_BITS, sizeof(Depot), &DepotPoolNewBlock, NULL, 0, 0, NULL }; /** diff --git a/engine.c b/engine.c index 78f895bb14..34cb60f501 100644 --- a/engine.c +++ b/engine.c @@ -507,7 +507,7 @@ enum { ENGINE_RENEW_POOL_MAX_BLOCKS = 8000, }; -MemoryPool _engine_renew_pool = { "EngineRe", ENGINE_RENEW_POOL_MAX_BLOCKS, ENGINE_RENEW_POOL_BLOCK_SIZE_BITS, sizeof(EngineRenew), &EngineRenewPoolNewBlock, 0, 0, NULL }; +MemoryPool _engine_renew_pool = { "EngineRe", ENGINE_RENEW_POOL_MAX_BLOCKS, ENGINE_RENEW_POOL_BLOCK_SIZE_BITS, sizeof(EngineRenew), &EngineRenewPoolNewBlock, NULL, 0, 0, NULL }; static inline uint16 GetEngineRenewPoolSize(void) { diff --git a/industry_cmd.c b/industry_cmd.c index dd53f5e26d..e0cf69ebe4 100644 --- a/industry_cmd.c +++ b/industry_cmd.c @@ -38,7 +38,7 @@ static void IndustryPoolNewBlock(uint start_item) } /* Initialize the industry-pool */ -MemoryPool _industry_pool = { "Industry", INDUSTRY_POOL_MAX_BLOCKS, INDUSTRY_POOL_BLOCK_SIZE_BITS, sizeof(Industry), &IndustryPoolNewBlock, 0, 0, NULL }; +MemoryPool _industry_pool = { "Industry", INDUSTRY_POOL_MAX_BLOCKS, INDUSTRY_POOL_BLOCK_SIZE_BITS, sizeof(Industry), &IndustryPoolNewBlock, NULL, 0, 0, NULL }; static byte _industry_sound_ctr; static TileIndex _industry_sound_tile; diff --git a/order_cmd.c b/order_cmd.c index 109eae836d..70a1539a39 100644 --- a/order_cmd.c +++ b/order_cmd.c @@ -33,7 +33,7 @@ static void OrderPoolNewBlock(uint start_item) } /* Initialize the order-pool */ -MemoryPool _order_pool = { "Orders", ORDER_POOL_MAX_BLOCKS, ORDER_POOL_BLOCK_SIZE_BITS, sizeof(Order), &OrderPoolNewBlock, 0, 0, NULL }; +MemoryPool _order_pool = { "Orders", ORDER_POOL_MAX_BLOCKS, ORDER_POOL_BLOCK_SIZE_BITS, sizeof(Order), &OrderPoolNewBlock, NULL, 0, 0, NULL }; /** * diff --git a/pool.c b/pool.c index 9b340e3f2e..828aa87d9d 100644 --- a/pool.c +++ b/pool.c @@ -16,8 +16,12 @@ void CleanPool(MemoryPool *pool) DEBUG(misc, 4)("[Pool] (%s) Cleaning pool..", pool->name); /* Free all blocks */ - for (i = 0; i < pool->current_blocks; i++) + 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); diff --git a/pool.h b/pool.h index 179ec7dc8e..24c1717fc3 100644 --- a/pool.h +++ b/pool.h @@ -8,6 +8,8 @@ typedef struct MemoryPool MemoryPool; /* The function that is called after a new block is added start_item is the first item of the new made block */ typedef void MemoryPoolNewBlock(uint start_item); +/* The function that is called before a block is cleaned up */ +typedef void MemoryPoolCleanBlock(uint start_item, uint end_item); /** * Stuff for dynamic vehicles. Use the wrappers to access the MemoryPool @@ -22,6 +24,8 @@ struct MemoryPool { /// Pointer to a function that is called after a new block is added MemoryPoolNewBlock *new_block_proc; + /// Pointer to a function that is called to clean a block + MemoryPoolCleanBlock *clean_block_proc; uint current_blocks; ///< How many blocks we have in our pool uint total_items; ///< How many items we now have in this pool diff --git a/saveload.c b/saveload.c index 4a5470cb95..a29c3990fc 100644 --- a/saveload.c +++ b/saveload.c @@ -1021,7 +1021,7 @@ typedef struct ThreadedSave { } ThreadedSave; /* A maximum size of of 128K * 500 = 64.000KB savegames */ -static MemoryPool _save_pool = {"Savegame", SAVE_POOL_MAX_BLOCKS, SAVE_POOL_BLOCK_SIZE_BITS, sizeof(byte), NULL, 0, 0, NULL}; +static MemoryPool _save_pool = {"Savegame", SAVE_POOL_MAX_BLOCKS, SAVE_POOL_BLOCK_SIZE_BITS, sizeof(byte), NULL, NULL, 0, 0, NULL}; static ThreadedSave _ts; static bool InitMem(void) diff --git a/signs.c b/signs.c index eb108a5962..ed1e39bc8e 100644 --- a/signs.c +++ b/signs.c @@ -30,7 +30,7 @@ static void SignPoolNewBlock(uint start_item) } /* Initialize the sign-pool */ -MemoryPool _sign_pool = { "Signs", SIGN_POOL_MAX_BLOCKS, SIGN_POOL_BLOCK_SIZE_BITS, sizeof(SignStruct), &SignPoolNewBlock, 0, 0, NULL }; +MemoryPool _sign_pool = { "Signs", SIGN_POOL_MAX_BLOCKS, SIGN_POOL_BLOCK_SIZE_BITS, sizeof(SignStruct), &SignPoolNewBlock, NULL, 0, 0, NULL }; /** * diff --git a/station_cmd.c b/station_cmd.c index 62d71ccb83..ed448d3aec 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -61,8 +61,8 @@ static void RoadStopPoolNewBlock(uint start_item) } /* Initialize the station-pool and roadstop-pool */ -MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, 0, 0, NULL }; -MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, 0, 0, NULL }; +MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, NULL, 0, 0, NULL }; +MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, NULL, 0, 0, NULL }; // FIXME -- need to be embedded into Airport variable. Is dynamically diff --git a/town_cmd.c b/town_cmd.c index b78096253b..b6afb74a51 100644 --- a/town_cmd.c +++ b/town_cmd.c @@ -45,7 +45,7 @@ static void TownPoolNewBlock(uint start_item) } /* Initialize the town-pool */ -MemoryPool _town_pool = { "Towns", TOWN_POOL_MAX_BLOCKS, TOWN_POOL_BLOCK_SIZE_BITS, sizeof(Town), &TownPoolNewBlock, 0, 0, NULL }; +MemoryPool _town_pool = { "Towns", TOWN_POOL_MAX_BLOCKS, TOWN_POOL_BLOCK_SIZE_BITS, sizeof(Town), &TownPoolNewBlock, NULL, 0, 0, NULL }; // Local static int _grow_town_result; diff --git a/vehicle.c b/vehicle.c index 0007b991d2..582c0b1947 100644 --- a/vehicle.c +++ b/vehicle.c @@ -80,7 +80,7 @@ static void VehiclePoolNewBlock(uint start_item) } /* Initialize the vehicle-pool */ -MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, 0, 0, NULL }; +MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, NULL, 0, 0, NULL }; void VehicleServiceInDepot(Vehicle *v) { diff --git a/waypoint.c b/waypoint.c index 0239513cc2..9570768112 100644 --- a/waypoint.c +++ b/waypoint.c @@ -39,7 +39,7 @@ static void WaypointPoolNewBlock(uint start_item) } /* Initialize the town-pool */ -MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, 0, 0, NULL }; +MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, NULL, 0, 0, NULL }; /* Create a new waypoint */ static Waypoint* AllocateWaypoint(void)