commit fac84d570608f5a23083947ae81c3ed38092a5ca
parent 400454f5963c78743120a031237ed0aec2e05995
Author: Michael Savage <mikejsavage@gmail.com>
Date: Fri, 20 Oct 2017 21:54:32 +0300
alloc templates for MemoryArena
Diffstat:
2 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/btt.cc b/btt.cc
@@ -15,8 +15,8 @@
*/
static void btt_link_diamond( MemoryArena * arena, BTT * node ) {
- node->left = memarena_push_type( arena, BTT );
- node->right = memarena_push_type( arena, BTT );
+ node->left = alloc< BTT >( arena );
+ node->right = alloc< BTT >( arena );
*node->left = { };
*node->right = { };
@@ -104,8 +104,8 @@ static void btt_build(
BTTs btt_from_heightmap( const Heightmap * hm, MemoryArena * arena ) {
BTTs roots;
- roots.left_root = memarena_push_type( arena, BTT );
- roots.right_root = memarena_push_type( arena, BTT );
+ roots.left_root = alloc< BTT >( arena );
+ roots.right_root = alloc< BTT >( arena );
*roots.left_root = { };
*roots.right_root = { };
diff --git a/memory_arena.h b/memory_arena.h
@@ -27,7 +27,22 @@ struct MemoryArenaAutoCheckpoint {
void memarena_init( MemoryArena * arena, u8 * memory, size_t size );
u8 * memarena_push_size( MemoryArena * arena, size_t size, uptr alignment = 16 );
-#define memarena_push_type( arena, type, ... ) ( ( type * ) memarena_push_size( arena, sizeof( type ), ##__VA_ARGS__ ) )
+
+template< typename T >
+T * alloc( MemoryArena * arena, uptr alignment = alignof( T ) ) {
+ return ( T * ) memarena_push_size( arena, sizeof( T ), alignment );
+}
+
+template< typename T >
+array< T > alloc_array( MemoryArena * arena, size_t n, uptr alignment = alignof( T ) ) {
+ return array< T >( ( T * ) memarena_push_size( arena, sizeof( T ) * n, alignment ), n );
+}
+
+template< typename T >
+array< T > alloc_array2d( MemoryArena * arena, size_t w, size_t h, uptr alignment = alignof( T ) ) {
+ return array2d< T >( ( T * ) memarena_push_size( arena, sizeof( T ) * w * h, alignment ), w, h );
+}
+
#define memarena_push_many( arena, type, count, ... ) ( ( type * ) memarena_push_size( arena, sizeof( type ) * count, ##__VA_ARGS__ ) )
#define memarena_push_array( arena, type, count, ... ) \