boagz57
Meaning the main goal of trying to manage memory is to limit the need for manually deleting memory as much as you can.
I wouldn't try to generalize it. Each case has separate constraints and it's a matter of advantages / disadvantages.
A 1 frame stack allocator is nice because you don't really worry about freeing memory, but you can't persist data stored into it across frames, or free memory in the middle of the stack.
A pool allocator is usefull when you allocate objects of the same size that don't have a know lifetime. But it's a bit slower to get or free memory from it.
To summarize, you need to figure out the memory usage for different parts of your code and choose how to manage it accordingly. If you want the security that memory doesn't persist between levels, than design your allocator for that. You can have the memory block used from a pool allocator come from a "level lifetime" arena so it's cleared when you "free" the level memory block.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | int main( void ) {
arena_t frame_arena = ...;
arena_t level_arena = ...;
pool_t level_pool;
while ( ... ) {
reset( &frame_arena );
if ( load_level ) {
reset( &level_arena );
pool_init( &level_arena );
}
pool_alloc( &level_pool, ... );
...
pool_free( &level_pool, ... );
}
return 0;
}
|