medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 68ec670f387a0b5883215a9b8626fd23ce1095f1
parent b68100b124d2d5ae745f4090607910589ef33809
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue Nov 22 20:45:06 +0200

Add scripts/gen_visitors.lua

Diffstat:
Makefile | 12++++++++----
analysis/leakcheck.lua | 68--------------------------------------------------------------------
linear_algebra.h | 11+++++++++++
rules.mk | 5+++++
scripts/gen_visitors.lua | 46++++++++++++++++++++++++++++++++++++++++++++++
scripts/leak_check.lua | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
visitors/linear_algebra.h | 130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 268 insertions(+), 72 deletions(-)
diff --git a/Makefile b/Makefile @@ -10,10 +10,6 @@ pp: pp.o log.o memory_arena.o stb_image.o lz4.o lz4hc.o strlcpy.o sound: audio.o mixer.o log.o memory_arena.o wave.o platform_audio_output.o srv: server/main.o rng/well512.o -lz4.o: CXXFLAGS += -O3 -lz4hc.o: CXXFLAGS += -O3 -xxhash.o: CXXFLAGS += -O3 - # Module dependencies bsp.so: bsp.o bsp_renderer.o hm.so: hm.o heightmap.o terrain_manager.o lz4.o btt.o gpubtt.o skybox.o @@ -23,6 +19,14 @@ sm.so: shadow_map.o test_lockfree: relacy.cc $(CXX) relacy.cc -o test_lockfree $(CXXFLAGS) $(LDFLAGS) -std=c++98 -fexceptions -frtti -Wno-missing-field-initializers +# Special CXXFLAGS +lz4.o: CXXFLAGS += -O3 +lz4hc.o: CXXFLAGS += -O3 +xxhash.o: CXXFLAGS += -O3 + +# Visitor headers +visitors/linear_algebra.h: linear_algebra.h + # Common dependencies COMMON_OBJS := log.o memory_arena.o work_queue.o immediate.o benchmark.o stb_truetype.o renderer.o glsl.o strlcpy.o diff --git a/analysis/leakcheck.lua b/analysis/leakcheck.lua @@ -1,68 +0,0 @@ -#! /usr/bin/lua - -local lpeg = require( "lpeg" ) -local json = require( "cjson.safe" ) - --- b_ = begin --- e_ = end --- ne_ = not end --- slc = single line comment --- mlc = multiline comment -local b_slc = lpeg.P( "//" ) -local e_slc = lpeg.P( "\n" ) -local ne_slc = ( 1 - e_slc ) ^ 0 -local slc = b_slc * ne_slc - -local b_mlc = lpeg.P( "/*" ) -local e_mlc = lpeg.P( "*/" ) -local ne_mlc = ( 1 - e_mlc ) ^ 0 -local mlc = b_mlc * ne_mlc * e_mlc - -local quote = lpeg.P( "\"" ) -local escaped = lpeg.P( "\\" ) * 1 -local string = quote * ( escaped + ( 1 - quote ) ) ^ 0 * quote - -local comments = slc + mlc -local none = 1 - ( string + comments ) - -local just_code = ( comments + string + lpeg.C( none ^ 1 ) ) ^ 0 - -local f = io.open( arg[ 1 ] ) -local contents = f:read( "*all" ) -local stripped = table.concat( { just_code:match( contents ) } ) - -local function check_leaks( leaks, levels, depth, body ) - for code, block in ( body .. "{}" ):gmatch( "(.-)(%b{})" ) do - if code == "" and block == "{}" then - break - end - - for line in code:gmatch( "([^\n]+)" ) do - if line:find( "memarena_push" ) then - if not levels[ depth ] then - table.insert( leaks, line ) - end - end - - if line:find( "MEMARENA_SCOPED_CHECKPOINT" ) then - levels[ depth ] = true - end - end - - if not cp then - levels[ depth + 1 ] = false - check_leaks( leaks, levels, depth + 1, block:sub( 2, -2 ) ) - levels[ depth + 1 ] = nil - end - end -end - -for ret, name, args, body in stripped:gmatch( "([^\n]-)%s*([^%s]+)(%b())\n? *(%b{})" ) do - local leaks = { } - check_leaks( leaks, { false }, 1, body ) - - if #leaks > 0 then - print( name ) - print( table.concat( leaks, "\n" ) ) - end -end diff --git a/linear_algebra.h b/linear_algebra.h @@ -14,6 +14,7 @@ * data definitions */ +// AUTOVISITOR struct v2 { float x, y; @@ -35,6 +36,7 @@ struct v2 { v2 yy() const { return v2( y, y ); } }; +// AUTOVISITOR struct v2s32 { s32 x, y; @@ -46,6 +48,7 @@ struct v2s32 { } }; +// AUTOVISITOR struct m2 { v2 row0, row1; @@ -60,6 +63,7 @@ struct m2 { v2 col1() const { return v2( row0.y, row1.y ); } }; +// AUTOVISITOR struct v3 { float x, y, z; @@ -90,6 +94,7 @@ struct v3 { } }; +// AUTOVISITOR struct v3u32 { u32 x, y, z; @@ -102,6 +107,7 @@ struct v3u32 { } }; +// AUTOVISITOR struct m3 { v3 row0, row1, row2; @@ -128,6 +134,7 @@ struct m3 { v3 col2() const { return v3( row0.z, row1.z, row2.z ); } }; +// AUTOVISITOR struct v4 { float x, y, z, w; @@ -171,6 +178,7 @@ struct v4 { } }; +// AUTOVISITOR struct m4 { v4 row0, row1, row2, row3; @@ -220,6 +228,7 @@ struct m4 { } }; +// AUTOVISITOR struct quat { float x, y, z, w; }; @@ -736,4 +745,6 @@ forceinline v3u32 mean( v3u32 mins, v3u32 maxs ) { return mins + ( maxs - mins ) / 2; } +#include "visitors/linear_algebra.h" + #endif // _LINEAR_ALGEBRA_H_ diff --git a/rules.mk b/rules.mk @@ -26,6 +26,11 @@ $(MODULES): %.so: $(COMMON_OBJS) @echo -e "\e[1;32mbuilding $<\e[0m" $(CXX) -c -o $@ $< -MMD -MP -MF .deps/$(subst /,_,$*).d $(CXXFLAGS) $(WARNINGS) +# Generate visitor headers +visitors/%.h: + @mkdir -p visitors + scripts/gen_visitors.lua $^ > $@ + # Make sure .deps exists .deps: mkdir -p .deps diff --git a/scripts/gen_visitors.lua b/scripts/gen_visitors.lua @@ -0,0 +1,46 @@ +#! /usr/bin/lua + +assert( arg[ 1 ], "need a file name" ) +local f = assert( io.open( arg[ 1 ] ) ) +local contents = assert( f:read( "*all" ) ) +f:close() + +local output = { + "/*", + " * This file was autogenerated by " .. arg[ 0 ] .. " " .. arg[ 1 ], + " * at " .. os.date() .. ". Please do not edit it by hand!", + " */", +} + +for struct, members in contents:gmatch( "// AUTOVISITOR%s+struct%s+(%w+)%s+(%b{})" ) do + members = members:sub( 2, -2 ) -- trim { } + members = members:gsub( "%b{}", "" ) + + local visitor = { + "template< typename T >", + "void visit_members( " .. struct .. " & obj, T f ) {", + } + local named_visitor = { + "template< typename T >", + "void visit_named_members( " .. struct .. " & obj, T f ) {", + } + + for names in members:gmatch( "\n\t*%w+%s+([%w, ]+)%s*;" ) do + for name in names:gmatch( "(%w+)" ) do + table.insert( visitor, "\tf( obj." .. name .. " );" ) + table.insert( named_visitor, "\tf( \"" .. name .. "\", obj." .. name .. " );" ) + end + end + + table.insert( visitor, "}" ) + table.insert( named_visitor, "}" ) + table.insert( output, "\n" .. table.concat( visitor, "\n" ) ) + table.insert( output, "\n" .. table.concat( named_visitor, "\n" ) ) +end + +print( table.concat( output, "\n" ) ) + +local filename = arg[ 1 ]:gsub( "^(.-)%.[^%.]+$", "visitors/%1.h" ) +if not contents:find( "#include \"" .. filename .. "\"" ) then + io.stderr:write( arg[ 1 ] .. " should include " .. filename .. "\n" ) +end diff --git a/scripts/leak_check.lua b/scripts/leak_check.lua @@ -0,0 +1,68 @@ +#! /usr/bin/lua + +local lpeg = require( "lpeg" ) +local json = require( "cjson.safe" ) + +-- b_ = begin +-- e_ = end +-- ne_ = not end +-- slc = single line comment +-- mlc = multiline comment +local b_slc = lpeg.P( "//" ) +local e_slc = lpeg.P( "\n" ) +local ne_slc = ( 1 - e_slc ) ^ 0 +local slc = b_slc * ne_slc + +local b_mlc = lpeg.P( "/*" ) +local e_mlc = lpeg.P( "*/" ) +local ne_mlc = ( 1 - e_mlc ) ^ 0 +local mlc = b_mlc * ne_mlc * e_mlc + +local quote = lpeg.P( "\"" ) +local escaped = lpeg.P( "\\" ) * 1 +local string = quote * ( escaped + ( 1 - quote ) ) ^ 0 * quote + +local comments = slc + mlc +local none = 1 - ( string + comments ) + +local just_code = ( comments + string + lpeg.C( none ^ 1 ) ) ^ 0 + +local f = io.open( arg[ 1 ] ) +local contents = f:read( "*all" ) +local stripped = table.concat( { just_code:match( contents ) } ) + +local function check_leaks( leaks, levels, depth, body ) + for code, block in ( body .. "{}" ):gmatch( "(.-)(%b{})" ) do + if code == "" and block == "{}" then + break + end + + for line in code:gmatch( "([^\n]+)" ) do + if line:find( "memarena_push" ) then + if not levels[ depth ] then + table.insert( leaks, line ) + end + end + + if line:find( "MEMARENA_SCOPED_CHECKPOINT" ) then + levels[ depth ] = true + end + end + + if not cp then + levels[ depth + 1 ] = false + check_leaks( leaks, levels, depth + 1, block:sub( 2, -2 ) ) + levels[ depth + 1 ] = nil + end + end +end + +for ret, name, args, body in stripped:gmatch( "([^\n]-)%s*([^%s]+)(%b())\n? *(%b{})" ) do + local leaks = { } + check_leaks( leaks, { false }, 1, body ) + + if #leaks > 0 then + print( name ) + print( table.concat( leaks, "\n" ) ) + end +end diff --git a/visitors/linear_algebra.h b/visitors/linear_algebra.h @@ -0,0 +1,130 @@ +/* + * This file was autogenerated by scripts/gen_visitors.lua linear_algebra.h + * at Tue Nov 22 20:43:18 2016. Please do not edit it by hand! + */ + +template< typename T > +void visit_members( v2 & obj, T f ) { + f( obj.x ); + f( obj.y ); +} + +template< typename T > +void visit_named_members( v2 & obj, T f ) { + f( "x", obj.x ); + f( "y", obj.y ); +} + +template< typename T > +void visit_members( v2s32 & obj, T f ) { + f( obj.x ); + f( obj.y ); +} + +template< typename T > +void visit_named_members( v2s32 & obj, T f ) { + f( "x", obj.x ); + f( "y", obj.y ); +} + +template< typename T > +void visit_members( m2 & obj, T f ) { + f( obj.row0 ); + f( obj.row1 ); +} + +template< typename T > +void visit_named_members( m2 & obj, T f ) { + f( "row0", obj.row0 ); + f( "row1", obj.row1 ); +} + +template< typename T > +void visit_members( v3 & obj, T f ) { + f( obj.x ); + f( obj.y ); + f( obj.z ); +} + +template< typename T > +void visit_named_members( v3 & obj, T f ) { + f( "x", obj.x ); + f( "y", obj.y ); + f( "z", obj.z ); +} + +template< typename T > +void visit_members( v3u32 & obj, T f ) { + f( obj.x ); + f( obj.y ); + f( obj.z ); +} + +template< typename T > +void visit_named_members( v3u32 & obj, T f ) { + f( "x", obj.x ); + f( "y", obj.y ); + f( "z", obj.z ); +} + +template< typename T > +void visit_members( m3 & obj, T f ) { + f( obj.row0 ); + f( obj.row1 ); + f( obj.row2 ); +} + +template< typename T > +void visit_named_members( m3 & obj, T f ) { + f( "row0", obj.row0 ); + f( "row1", obj.row1 ); + f( "row2", obj.row2 ); +} + +template< typename T > +void visit_members( v4 & obj, T f ) { + f( obj.x ); + f( obj.y ); + f( obj.z ); + f( obj.w ); +} + +template< typename T > +void visit_named_members( v4 & obj, T f ) { + f( "x", obj.x ); + f( "y", obj.y ); + f( "z", obj.z ); + f( "w", obj.w ); +} + +template< typename T > +void visit_members( m4 & obj, T f ) { + f( obj.row0 ); + f( obj.row1 ); + f( obj.row2 ); + f( obj.row3 ); +} + +template< typename T > +void visit_named_members( m4 & obj, T f ) { + f( "row0", obj.row0 ); + f( "row1", obj.row1 ); + f( "row2", obj.row2 ); + f( "row3", obj.row3 ); +} + +template< typename T > +void visit_members( quat & obj, T f ) { + f( obj.x ); + f( obj.y ); + f( obj.z ); + f( obj.w ); +} + +template< typename T > +void visit_named_members( quat & obj, T f ) { + f( "x", obj.x ); + f( "y", obj.y ); + f( "z", obj.z ); + f( "w", obj.w ); +}