From 103a2aa1164fe92552cfe64c46d99b4b7dd5e82a Mon Sep 17 00:00:00 2001 From: peter1138 Date: Thu, 20 Apr 2006 20:51:57 +0000 Subject: (svn r4486) - NewGRF: Create and use a memory pool to manage sprite groups. This reduces the amount of house keeping we do and the chance of memory leaks. --- newgrf_spritegroup.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 newgrf_spritegroup.c (limited to 'newgrf_spritegroup.c') diff --git a/newgrf_spritegroup.c b/newgrf_spritegroup.c new file mode 100644 index 000000000..9fd43efca --- /dev/null +++ b/newgrf_spritegroup.c @@ -0,0 +1,68 @@ +/* $Id$ */ + +#include "stdafx.h" +#include "openttd.h" +#include "pool.h" +#include "sprite.h" + +enum { + SPRITEGROUP_POOL_BLOCK_SIZE_BITS = 4, /* (1 << 4) == 16 items */ + SPRITEGROUP_POOL_MAX_BLOCKS = 8000, +}; + +static uint _spritegroup_count = 0; +static MemoryPool _spritegroup_pool; + + +static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item) +{ + uint i; + + for (i = start_item; i <= end_item; i++) { + SpriteGroup *group = (SpriteGroup*)GetItemFromPool(&_spritegroup_pool, i); + + /* Free dynamically allocated memory */ + switch (group->type) { + case SGT_REAL: + free(group->g.real.loaded); + free(group->g.real.loading); + break; + + case SGT_DETERMINISTIC: + free(group->g.determ.ranges); + break; + + case SGT_RANDOMIZED: + free(group->g.random.groups); + break; + + default: + break; + } + } +} + + +/* Initialize the SpriteGroup pool */ +static MemoryPool _spritegroup_pool = { "SpriteGr", SPRITEGROUP_POOL_MAX_BLOCKS, SPRITEGROUP_POOL_BLOCK_SIZE_BITS, sizeof(SpriteGroup), NULL, &SpriteGroupPoolCleanBlock, 0, 0, NULL }; + + +/* Allocate a new SpriteGroup */ +SpriteGroup *AllocateSpriteGroup(void) +{ + /* This is totally different to the other pool allocators, as we never remove an item from the pool. */ + if (_spritegroup_count == _spritegroup_pool.total_items) { + if (!AddBlockToPool(&_spritegroup_pool)) return NULL; + } + + return (SpriteGroup*)GetItemFromPool(&_spritegroup_pool, _spritegroup_count++); +} + + +void InitializeSpriteGroupPool(void) +{ + CleanPool(&_spritegroup_pool); + AddBlockToPool(&_spritegroup_pool); + + _spritegroup_count = 0; +} -- cgit v1.2.3-54-g00ecf