Skip to content

Commit

Permalink
Don't use GCC's __builtin_alloca_with_align(), fix #572
Browse files Browse the repository at this point in the history
turns out that __builtin_alloca_with_align() might releases the
allocated memory at the end of the block it was allocated in, instead
of the end of the function (which is the behavior of regular alloca()
and __builtin_alloca()): "The lifetime of the allocated object ends at
 the end of the block in which the function was called. The allocated
 storage is released no later than just before the calling function
 returns to its caller, but may be released at the end of the block in
 which the function was called."
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005falloca_005fwith_005falign

Clang also supports __builtin_alloca_with_align(), but always releases
the memory at the end of the function.

And it seems that newer GCC versions also tend to release it at the
end of the function, but GCC 4.7.2 (that I use for the official Linux
release binaries) didn't, and that caused weird graphical glitches.
But as I don't want to rely on newer GCC versions behaving like this
(and the documentation explicitly says that it *may* be released at
 the end of the block, but will definitely be released at the end of
 the function), I removed all usage of __builtin_alloca_with_align().

(Side-Note: GCC only started documenting the behavior of
 __builtin_alloca and __builtin_alloca_with_align at all with GCC 6.1.0)
  • Loading branch information
DanielGibson committed Apr 19, 2024
1 parent b565c6c commit 6daf1e9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions neo/sys/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ If you have questions concerning this license or the applicable additional terms

#ifdef __MINGW32__
#undef _alloca // in mingw _alloca is a #define
#define _alloca16( x ) ( (assert((x)<ID_MAX_ALLOCA_SIZE)), __builtin_alloca_with_align( (x), 16*8 ) )
// NOTE: Do *not* use __builtin_alloca_with_align(), unlike regular alloca it frees at end of block instead of end of function !
#define _alloca16( x ) ( (void *) ( (assert((x)<ID_MAX_ALLOCA_SIZE)), ((((uintptr_t)__builtin_alloca( (x)+15 )) + 15) & ~15) ) )
#define _alloca( x ) ( (assert((x)<ID_MAX_ALLOCA_SIZE)), __builtin_alloca( (x) ) )
#else
#define _alloca16( x ) ( (void *) ( (assert((x)<ID_MAX_ALLOCA_SIZE)), ((((uintptr_t)_alloca( (x)+15 )) + 15) & ~15) ) )
Expand Down Expand Up @@ -185,14 +186,13 @@ If you have questions concerning this license or the applicable additional terms
// Unix
#ifdef __unix__

#if !defined(__GNUC__) || (defined(__MCST__) && __LCC__ < 128)
// MCST-LCC < 1.28 does not support __builtin_alloca_with_align()
#ifdef __GNUC__
// NOTE: Do *not* use __builtin_alloca_with_align(), unlike regular alloca it frees at end of block instead of end of function !
#define _alloca16( x ) (({assert( (x)<ID_MAX_ALLOCA_SIZE );}),((void *)((((uintptr_t)__builtin_alloca( (x)+15 )) + 15) & ~15)))
#define _alloca( x ) ( ({assert((x)<ID_MAX_ALLOCA_SIZE);}), __builtin_alloca( (x) ) )
#else
#define _alloca( x ) (({assert( (x)<ID_MAX_ALLOCA_SIZE );}), alloca( (x) ))
#define _alloca16( x ) (({assert( (x)<ID_MAX_ALLOCA_SIZE );}),((void *)((((uintptr_t)alloca( (x)+15 )) + 15) & ~15)))
#else
// GCC, CLANG, MCST-LCC >= 1.28
#define _alloca16( x ) ( ({assert((x)<ID_MAX_ALLOCA_SIZE);}), __builtin_alloca_with_align( (x), 16*8 ) )
#define _alloca( x ) ( ({assert((x)<ID_MAX_ALLOCA_SIZE);}), __builtin_alloca( (x) ) )
#endif

#ifdef GAME_DLL
Expand Down

0 comments on commit 6daf1e9

Please sign in to comment.