diff options
Diffstat (limited to 'src/extmalloc.c')
-rw-r--r-- | src/extmalloc.c | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/src/extmalloc.c b/src/extmalloc.c index 3e14e0d..a23dc2a 100644 --- a/src/extmalloc.c +++ b/src/extmalloc.c @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Michael Smith <mikesmiffy128@gmail.com> + * Copyright © 2023 Michael Smith <mikesmiffy128@gmail.com> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,39 +14,49 @@ * PERFORMANCE OF THIS SOFTWARE. */ +#ifndef _WIN32 +#include <stdlib.h> +#endif + #include "intdefs.h" #include "vcall.h" -// XXX: this is duped from os.h because I don't want to pull in Windows.h, -// consider splitting out the IMPORT/EXPORT defs to some other thing? -#ifdef _WIN32 -#define IMPORT __declspec(dllimport) // only needed for variables -#else -#define IMPORT -#endif - // XXX: not sure if "ext" is the best naming convention? use brain later -IMPORT void *g_pMemAlloc; +#ifdef _WIN32 + +__declspec(dllimport) void *g_pMemAlloc; // this interface has changed a bit between versions but thankfully the basic // functions we care about have always been at the start - nice and easy. -// unfortunately though, because the debug and non-debug versions are overloads -// and Microsoft are a bunch of crazies who decided vtable order should be -// affected by naming (overloads are grouped, and *reversed* inside of a -// group!?), we get this amusing ABI difference between platforms: -#ifdef _WIN32 +// note that since Microsoft are a bunch of crazies, overloads are grouped and +// reversed so the vtable order here is maybe not what you'd expect otherwise. DECL_VFUNC(void *, Alloc, 1, usize) DECL_VFUNC(void *, Realloc, 3, void *, usize) DECL_VFUNC(void, Free, 5, void *) -#else -DECL_VFUNC(void *, Alloc, 0, usize) -DECL_VFUNC(void *, Realloc, 1, void *, usize) -DECL_VFUNC(void, Free, 2, void *) -#endif void *extmalloc(usize sz) { return Alloc(g_pMemAlloc, sz); } void *extrealloc(void *mem, usize sz) { return Realloc(g_pMemAlloc, mem, sz); } void extfree(void *mem) { Free(g_pMemAlloc, mem); } +#else + +void Error(const char *fmt, ...); // stub left out of con_.h (not that useful) + +// Linux Source doesn't seem to bother with the custom allocator stuff at all. +// We still want to check for OoM though, because turning off overcommit is a +// right, not a privilege. Like func_vehicle. +void *extmalloc(usize sz) { + void *ret = malloc(sz); + if (!ret) Error("sst: out of memory"); + return ret; +} +void *extrealloc(void *mem, usize sz) { + void *ret = realloc(mem, sz); + if (!ret) Error("sst: out of memory"); + return ret; +} +// note: extfree is #defined to free in the header +#endif + // vi: sw=4 ts=4 noet tw=80 cc=80 |