diff --git a/src/core/constructor.h b/src/core/constructor.h index 289ebc34..6f0ec693 100644 --- a/src/core/constructor.h +++ b/src/core/constructor.h @@ -15,16 +15,19 @@ #define CONSTRUCTOR(f) \ static void __cdecl f(void); \ + __pragma(section(".CRT$XCU", read)); \ __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = f; \ static void __cdecl f(void) -#define DESTRUCTOR(f) \ - static void __cdecl f(void); \ - static int _f##_wrapper(void) { \ - atexit(f); \ - return 0; \ - } \ - __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = _f##_wrapper; \ +#define DESTRUCTOR(f) \ + static void __cdecl f(void); \ + static int _##f##_wrapper(void) { \ + atexit(f); \ + return 0; \ + } \ + __pragma(section(".CRT$XCU", read)); \ + __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = \ + _##f##_wrapper; \ static void __cdecl f(void) #else diff --git a/src/core/core.h b/src/core/core.h index 8372373b..8462072f 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -2,23 +2,41 @@ #define REDREAM_CORE_H #include +#include #include "core/math.h" -#define TYPEOF(n) __typeof__(n) - -#define SWAP(a, b) \ - do { \ - TYPEOF(a) tmp = (a); \ - (a) = (b); \ - (b) = tmp; \ - } while (0) - -#define container_of(ptr, type, member) \ - ({ \ - const TYPEOF(((type *)0)->member) *__mptr = (ptr); \ - (type *)((char *)__mptr - offsetof(type, member)); \ - }) - #define array_size(arr) (int)(sizeof(arr) / sizeof((arr)[0])) +#if PLATFORM_WINDOWS + +static inline void *container_of_(void *ptr, ptrdiff_t offset) { + return (char *)ptr - offset; +} + +static inline void *container_of_safe_(void *ptr, ptrdiff_t offset) { + return ptr ? (char *)ptr - offset : NULL; +} + +#define container_of(ptr, type, member) \ + ((type *)container_of_((void *)ptr, offsetof(type, member))) + +#define container_of_safe(ptr, type, member) \ + ((type *)container_of_safe_((void *)ptr, offsetof(type, member))) + +#else + +#define container_of(ptr, type, member) \ + ({ \ + const typeof(((type *)0)->member) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type, member)); \ + }) + +#define container_of_safe(ptr, type, member) \ + ({ \ + const typeof(((type *)0)->member) *__mptr = (ptr); \ + ptr ? (type *)((char *)__mptr - offsetof(type, member)) : NULL; \ + }) + +#endif + #endif