d61df7dc771a130c58e59709121e41b9ec85b4c8 galt Sat Jan 4 21:53:30 2025 -0800 Fixes for the many compiler issues with ArraySize called on NULL. Now compiles without any errors, warnings, or pramga. diff --git src/inc/common.h src/inc/common.h index 34b0451..09764f2 100644 --- src/inc/common.h +++ src/inc/common.h @@ -157,35 +157,64 @@ * and create our own. Our implementation is the same as used on Solaris. */ #if defined(__va_copy) && !defined(va_copy) # define va_copy __va_copy #endif #if !defined(va_copy) # define va_copy(to, from) ((to) = (from)) #endif /* Cast a pointer to a long long. Use to printf format points as long-longs * in a 32/64bit portable manner. Format should use %llx for the result. * Needed because casting a pointer to a different sized number cause a * warning with gcc */ #define ptrToLL(p) ((long long)((size_t)p)) -// TODO GALT This is a temporary work around. -#pragma GCC diagnostic ignored "-Wsizeof-pointer-div" +#define _CAT(a, b) a ## b + +#define _CHECK_N(x, n, ...) n +#define _CHECK(...) _CHECK_N(__VA_ARGS__, 0,) +#define _PROBE() ~, 1, + +#define _NOT(x) _CHECK(_CAT(_NOT_, x)) +#define _NOT_0 _PROBE() + +#define _BOOL(x) _NOT(_NOT(x)) + +#define _IIF(c) _CAT(_IIF_, c) +#define _IIF_0(t, ...) __VA_ARGS__ +#define _IIF_1(t, ...) t + +#define _IF(c) _IIF(_BOOL(c)) + +/* +_IF(0)(it was not 0, it was 0) + Should be "it was 0" + +_IF(1)(it was 1, it was not 1) + Should be "it was 1" + +_IF(123)(it was 123)(it was not 123) + Should be "it was 123" + +// Could not get NULL to work as it gets replaced by ((void *)0) and then ## makes a invalid token created error. +_IF(NULL)(it was not null,it was null) + Should be "it was null" but does not work +*/ /* How big is this array? */ -#define ArraySize(a) (sizeof(a)/sizeof((a)[0])) +#define ArraySize(a) (_IF(a)((sizeof(a) / sizeof((a)[0])),(size_t)0)) #define uglyf printf /* debugging printf */ #define uglyAbort errAbort /* debugging error abort. */ #define uglyOut stdout /* debugging fprintf target. */ size_t memCheckPoint(); /* Return the amount of memory allocated since last called. */ void *needMem(size_t size); /* Need mem calls abort if the memory allocation fails. The memory * is initialized to zero. */ void *needLargeMem(size_t size); /* This calls abort if the memory allocation fails. The memory is * not initialized to zero. */