commit 05f9f7f05b540711edc27cc762b586b4d8ea50c7 parent 9a923e4e00e1f93a748fc9a3c78e1e13a6b65dfe Author: Michael Savage <mikejsavage@gmail.com> Date: Thu Oct 8 19:46:21 +0100 Add MEMARENA_SCOPED_CHECKPOINT Diffstat:
gpubtt.cc | | | 4 | +--- |
heightmap.cc | | | 4 | +--- |
memory_arena.cc | | | 9 | +++++++++ |
memory_arena.h | | | 11 | +++++++++++ |
work_queue.cc | | | 5 | ++--- |
diff --git a/gpubtt.cc b/gpubtt.cc @@ -47,7 +47,7 @@ void gpubtt_init( const OffsetHeightmap * const ohm, const BTTs btts, const GLuint at_position ) { - MemoryArenaCheckpoint cp = memarena_checkpoint( mem ); + MEMARENA_SCOPED_CHECKPOINT( mem ); const u32 num_leaves = btt_count_leaves( btts.left_root ) + btt_count_leaves( btts.right_root ); @@ -70,8 +70,6 @@ void gpubtt_init( glBindVertexArray( 0 ); gpubtt->num_verts = i; - - memarena_restore( mem, &cp ); } void gpubtt_destroy( GPUBTT * const gpubtt ) { diff --git a/heightmap.cc b/heightmap.cc @@ -54,7 +54,7 @@ void heightmap_init( hm->width = width; hm->height = height; - MemoryArenaCheckpoint cp = memarena_checkpoint( mem ); + MEMARENA_SCOPED_CHECKPOINT( mem ); GLfloat * const vertices = memarena_push_many( mem, GLfloat, width * height * 3 ); GLfloat * const normals = memarena_push_many( mem, GLfloat, width * height * 3 ); @@ -153,8 +153,6 @@ void heightmap_init( glBufferData( GL_ELEMENT_ARRAY_BUFFER, width * height * sizeof( GLuint ) * 6, indices, GL_STATIC_DRAW ); glBindVertexArray( 0 ); - - memarena_restore( mem, &cp ); } void heightmap_destroy( Heightmap * const hm ) { diff --git a/memory_arena.cc b/memory_arena.cc @@ -50,3 +50,12 @@ void memarena_restore( MemoryArena * arena, MemoryArenaCheckpoint * const cp ) { arena->num_checkpoints--; cp->restored = true; } + +MemoryArenaAutoCheckpoint::MemoryArenaAutoCheckpoint( MemoryArena * arena, MemoryArenaCheckpoint cp ) { + this->arena = arena; + this->cp = cp; +} + +MemoryArenaAutoCheckpoint::~MemoryArenaAutoCheckpoint() { + memarena_restore( arena, &cp ); +} diff --git a/memory_arena.h b/memory_arena.h @@ -16,6 +16,14 @@ struct MemoryArenaCheckpoint { bool restored; }; +struct MemoryArenaAutoCheckpoint { + MemoryArena * arena; + MemoryArenaCheckpoint cp; + + MemoryArenaAutoCheckpoint( MemoryArena * arena, MemoryArenaCheckpoint cp ); + ~MemoryArenaAutoCheckpoint(); +}; + void memarena_init( MemoryArena * const arena, u8 * const memory, const size_t size ); u8 * memarena_push_size( MemoryArena * const arena, const size_t size, const size_t alignment = sizeof( void * ) ); @@ -29,4 +37,7 @@ void memarena_clear( MemoryArena * const arena ); MemoryArenaCheckpoint memarena_checkpoint( MemoryArena * const arena ); void memarena_restore( MemoryArena * arena, MemoryArenaCheckpoint * const cp ); +#define MEMARENA_SCOPED_CHECKPOINT( arena ) MemoryArenaAutoCheckpoint mem_cp##__COUNTER__( arena, memarena_checkpoint( arena ) ); + + #endif // _MEMORY_ARENA_H_ diff --git a/work_queue.cc b/work_queue.cc @@ -58,7 +58,8 @@ void workqueue_init( WorkQueue * const queue, MemoryArena * const arena, const u queue->arenas[ i ] = memarena_push_arena( arena, megabytes( 1 ) ); } - MemoryArenaCheckpoint cp = memarena_checkpoint( arena ); + MEMARENA_SCOPED_CHECKPOINT( arena ); + ThreadInfo * infos = memarena_push_many( arena, ThreadInfo, num_threads ); volatile u32 started_threads = 0; @@ -71,8 +72,6 @@ void workqueue_init( WorkQueue * const queue, MemoryArena * const arena, const u // wait until all threads have a local copy of ThreadInfo while( started_threads < num_threads ); - - memarena_restore( arena, &cp ); } void workqueue_enqueue( WorkQueue * const queue, WorkQueueCallback * const callback, void * const data ) {