mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 09:04:13 +00:00
Added exception-safe version of the GS_BEGINITEMBUF() macro. As a side effect, this should silence the zero-sized VLA warnings in the static analyser.
Note: This macro is currently only used on GCC 4+ and clang. It depends on __attribute__((cleanup)), which was introduced some time in the 3.x series, but I'm not sure exactly when (3.2 / 3.3 I think), so someone who cares about gcc 3.x can tweak this if they want (not sure if anyone does - I think most people use 2.95, 4.x, or clang). git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32059 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
d81ac8a945
commit
fa8afdc28a
1 changed files with 22 additions and 2 deletions
|
@ -93,21 +93,41 @@ typedef struct objc_category* Category;
|
|||
* arrays are allocated on the stack (for speed), but large arrays are
|
||||
* allocated from the heap (to avoid stack overflow).
|
||||
*/
|
||||
#define GS_BEGINITEMBUF(P, S, T) { \
|
||||
#if __GNUC__ > 3
|
||||
__attribute__((unused)) static void GSFreeTempBuffer(void **b)
|
||||
{
|
||||
if (NULL != *b) free(*b);
|
||||
}
|
||||
# define GS_BEGINITEMBUF(P, S, T) { \
|
||||
T _ibuf[GS_MAX_OBJECTS_FROM_STACK];\
|
||||
T *P = _ibuf;\
|
||||
__attribute__((cleanup(GSFreeTempBuffer))) void *_base = 0;\
|
||||
if (S > GS_MAX_OBJECTS_FROM_STACK)\
|
||||
{\
|
||||
_base = malloc(S);\
|
||||
P = _base;\
|
||||
}
|
||||
#else
|
||||
# define GS_BEGINITEMBUF(P, S, T) { \
|
||||
T _ibuf[(S) <= GS_MAX_OBJECTS_FROM_STACK ? (S) : 0]; \
|
||||
T *_base = ((S) <= GS_MAX_OBJECTS_FROM_STACK) ? _ibuf \
|
||||
: (T*)NSZoneMalloc(NSDefaultMallocZone(), (S) * sizeof(T)); \
|
||||
T *(P) = _base;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Macro to manage memory for chunks of code that need to work with
|
||||
* arrays of items. Use GS_BEGINITEMBUF() to start the block of code using
|
||||
* the array and this macro to end it.
|
||||
*/
|
||||
#define GS_ENDITEMBUF() \
|
||||
#if __GNUC__ > 3
|
||||
# define GS_ENDITEMBUF() }
|
||||
#else
|
||||
# define GS_ENDITEMBUF() \
|
||||
if (_base != _ibuf) \
|
||||
NSZoneFree(NSDefaultMallocZone(), _base); \
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Macro to manage memory for chunks of code that need to work with
|
||||
|
|
Loading…
Reference in a new issue