commit 09a5ba60cedbb8e5c1438565904f0248c28c3238 parent 236f8ef1d2c0febf3d8c5740c1815ea540ff1d9f Author: Michael Savage <mikejsavage@gmail.com> Date: Fri Oct 14 20:45:48 +0300 Fix another stack overflow Diffstat:
array.h | | | 6 | ++++-- |
pp.cc | | | 24 | ++++++++++-------------- |
diff --git a/array.h b/array.h @@ -6,7 +6,7 @@ template< typename T > class array { public: - array( T * memory, size_t size ) : n( size ) { + array( T * memory, size_t count ) : n( count ), bytes( sizeof( T ) * count ) { assert( memory != NULL ); elems = memory; } @@ -40,6 +40,7 @@ public: } const size_t n; + const size_t bytes; private: T * elems; @@ -48,7 +49,7 @@ private: template< typename T > class array2d { public: - array2d( T * memory, size_t width, size_t height ) : w( width ), h( height ) { + array2d( T * memory, size_t width, size_t height ) : w( width ), h( height ), bytes( sizeof( T ) * width * height ) { assert( memory != NULL ); elems = memory; } @@ -72,6 +73,7 @@ public: } const size_t w, h; + const size_t bytes; private: T * elems; diff --git a/pp.cc b/pp.cc @@ -175,18 +175,14 @@ static void write_compressed_file( const void * data, size_t len, const char * f } static void write_tile( - const std::string & dir, + MemoryArena * arena, const std::string & dir, const array2d< u8 > heightmap, const array2d< float > horizons, - const array2d< v3 > normals, - int tx, int ty + const array2d< v3 > normals, int tx, int ty ) { - u8 heightmap_memory[ OUT_SIZE * OUT_SIZE ]; - float horizons_memory[ OUT_SIZE * OUT_SIZE ]; - v3 normals_memory[ OUT_SIZE * OUT_SIZE ]; - - array2d< u8 > tile_heightmap( heightmap_memory, OUT_SIZE, OUT_SIZE ); - array2d< float > tile_horizons( horizons_memory, OUT_SIZE, OUT_SIZE ); - array2d< v3 > tile_normals( normals_memory, OUT_SIZE, OUT_SIZE ); + MEMARENA_SCOPED_CHECKPOINT( arena ); + array2d< u8 > tile_heightmap = memarena_push_array2d( arena, u8, OUT_SIZE, OUT_SIZE ); + array2d< float > tile_horizons = memarena_push_array2d( arena, float, OUT_SIZE, OUT_SIZE ); + array2d< v3 > tile_normals = memarena_push_array2d( arena, v3, OUT_SIZE, OUT_SIZE ); const u32 left = tx * TILE_SIZE; const u32 top = ty * TILE_SIZE; @@ -210,15 +206,15 @@ static void write_tile( } printf( "writing heightmap for (%d, %d)\n", tx, ty ); - write_compressed_file( heightmap_memory, array_count( heightmap_memory ), + write_compressed_file( tile_heightmap.ptr(), tile_heightmap.bytes, "%s/%d_%d_heightmap.lz4", dir.c_str(), tx, ty ); printf( "writing horizons for (%d, %d)\n", tx, ty ); - write_compressed_file( horizons_memory, sizeof( horizons_memory ), + write_compressed_file( tile_horizons.ptr(), tile_horizons.bytes, "%s/%d_%d_horizons.lz4", dir.c_str(), tx, ty ); printf( "writing normals for (%d, %d)\n", tx, ty ); - write_compressed_file( normals_memory, sizeof( normals_memory ), + write_compressed_file( tile_normals.ptr(), tile_normals.bytes, "%s/%d_%d_normals.lz4", dir.c_str(), tx, ty ); } @@ -256,7 +252,7 @@ int main( int argc, char ** argv ) { for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { - write_tile( dir, heightmap, horizons, normals, tx, ty ); + write_tile( &arena, dir, heightmap, horizons, normals, tx, ty ); } }