diff options
Diffstat (limited to 'lib/Utils.Memory')
-rw-r--r-- | lib/Utils.Memory/NativeHeapApi/src/NativeHeapApi.h | 7 | ||||
-rw-r--r-- | lib/Utils.Memory/vnlib_rpmalloc/Taskfile.yaml | 2 | ||||
-rw-r--r-- | lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.c | 76 | ||||
-rw-r--r-- | lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.h | 14 |
4 files changed, 95 insertions, 4 deletions
diff --git a/lib/Utils.Memory/NativeHeapApi/src/NativeHeapApi.h b/lib/Utils.Memory/NativeHeapApi/src/NativeHeapApi.h index d70745b..6f9ad63 100644 --- a/lib/Utils.Memory/NativeHeapApi/src/NativeHeapApi.h +++ b/lib/Utils.Memory/NativeHeapApi/src/NativeHeapApi.h @@ -43,10 +43,15 @@ #ifdef WIN32 #define HEAP_METHOD_EXPORT __declspec(dllexport) #else - #define METHOD_EXPORT + #define HEAP_METHOD_EXPORT __attribute__((visibility("default"))) #endif #endif // HEAP_METHOD_EXPORT! +#ifndef WIN32 +typedef unsigned long DWORD; +typedef void* LPVOID; +#endif // !WIN32 + /// <summary> /// Internal heap creation flags passed to the creation method by the library loader /// </summary> diff --git a/lib/Utils.Memory/vnlib_rpmalloc/Taskfile.yaml b/lib/Utils.Memory/vnlib_rpmalloc/Taskfile.yaml index d7d921a..635c006 100644 --- a/lib/Utils.Memory/vnlib_rpmalloc/Taskfile.yaml +++ b/lib/Utils.Memory/vnlib_rpmalloc/Taskfile.yaml @@ -55,7 +55,6 @@ tasks: #release dll - cd build/Release && tar -czf '../../bin/win-x64-release-{{.PROJECT_NAME}}.tgz' {{.PROJECT_NAME}}.dll {{.TAR_FILES}} - licenses: cmds: #add rpmalloc license to binary output @@ -65,7 +64,6 @@ tasks: #add readme file - powershell -Command "Copy-Item -Path ./build.readme.txt -Destination '{{.TARGET}}/readme.txt'" - clean: ignore_error: true cmds: diff --git a/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.c b/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.c index 8a3e65f..20e1554 100644 --- a/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.c +++ b/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.c @@ -22,12 +22,13 @@ * along with vnlib_rpmalloc. If not, see http://www.gnu.org/licenses/. */ - #include "vnlib_rpmalloc.h" #include <NativeHeapApi.h> #include <rpmalloc.h> #include <stdint.h> +#if defined(_WIN32) + BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { (void)hModule; @@ -54,6 +55,79 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv return TRUE; } +#else + +/* +* Nuts and bolts from rpmalloc/malloc.c for pthread hooks +* for thread and process heap initializations +*/ + +#include <pthread.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> + +//! Set main thread ID (from rpmalloc.c) +extern void rpmalloc_set_main_thread(void); + +static pthread_key_t destructor_key; + +static void thread_destructor(void*); + +static void __attribute__((constructor)) initializer(void) { + rpmalloc_set_main_thread(); + rpmalloc_initialize(); + pthread_key_create(&destructor_key, thread_destructor); +} + +static void __attribute__((destructor)) finalizer(void) { + rpmalloc_finalize(); +} + +typedef struct { + void* (*real_start)(void*); + void* real_arg; +} thread_starter_arg; + +static void* thread_starter(void* argptr) { + thread_starter_arg* arg = argptr; + void* (*real_start)(void*) = arg->real_start; + void* real_arg = arg->real_arg; + rpmalloc_thread_initialize(); + rpfree(argptr); + pthread_setspecific(destructor_key, (void*)1); + return (*real_start)(real_arg); +} + +static void thread_destructor(void* value) { + (void)sizeof(value); + rpmalloc_thread_finalize(1); +} + + +#include <dlfcn.h> + +int pthread_create(pthread_t* thread, + const pthread_attr_t* attr, + void* (*start_routine)(void*), + void* arg) { +#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \ + defined(__APPLE__) || defined(__HAIKU__) + char fname[] = "pthread_create"; +#else + char fname[] = "_pthread_create"; +#endif + void* real_pthread_create = dlsym(RTLD_NEXT, fname); + + rpmalloc_thread_initialize(); + thread_starter_arg* starter_arg = rpmalloc(sizeof(thread_starter_arg)); + starter_arg->real_start = start_routine; + starter_arg->real_arg = arg; + return (*(int (*)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*))real_pthread_create)(thread, attr, thread_starter, starter_arg); +} + +#endif + #define GLOBAL_HEAP_HANDLE_VALUE -10 #define GLOBAL_HEAP_INIT_CHECK if (!rpmalloc_is_thread_initialized()) { rpmalloc_thread_initialize(); } diff --git a/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.h b/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.h index 0b89580..dd46c91 100644 --- a/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.h +++ b/lib/Utils.Memory/vnlib_rpmalloc/vnlib_rpmalloc.h @@ -3,9 +3,23 @@ #ifndef VNLIB_RPMALLOC_H #if defined(_WIN32) || defined(_WIN64) + #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files #include <windows.h> + +#else + +#define _GNU_SOURCE // for RTLD_NEXT + +#include <stddef.h> + +#define TRUE 1 +#define FALSE 0 + +//Windows type aliases for non-win +typedef int BOOL; + #endif #endif // !VNLIB_RPMALLOC_H
\ No newline at end of file |