updated container_of / CONSTRUCTOR / DESTRUCTOR macros to compile under msvc

This commit is contained in:
Anthony Pesch 2016-07-12 23:28:21 -07:00
parent 6fdfd17903
commit d98b6cb47d
2 changed files with 43 additions and 22 deletions

View File

@ -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

View File

@ -2,23 +2,41 @@
#define REDREAM_CORE_H
#include <stddef.h>
#include <stdint.h>
#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