microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/allocator/mimalloc-sys/mimalloc/src/prim/prim.c
76lines · modecode
| 1 | /* ---------------------------------------------------------------------------- |
| 2 | Copyright (c) 2018-2023, Microsoft Research, Daan Leijen |
| 3 | This is free software; you can redistribute it and/or modify it under the |
| 4 | terms of the MIT license. A copy of the license can be found in the file |
| 5 | "LICENSE" at the root of this distribution. |
| 6 | -----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | // Select the implementation of the primitives |
| 9 | // depending on the OS. |
| 10 | |
| 11 | #if defined(_WIN32) |
| 12 | #include "windows/prim.c" // VirtualAlloc (Windows) |
| 13 | |
| 14 | #elif defined(__APPLE__) |
| 15 | #include "osx/prim.c" // macOSX (actually defers to mmap in unix/prim.c) |
| 16 | |
| 17 | #elif defined(__wasi__) |
| 18 | #define MI_USE_SBRK |
| 19 | #include "wasi/prim.c" // memory-grow or sbrk (Wasm) |
| 20 | |
| 21 | #elif defined(__EMSCRIPTEN__) |
| 22 | #include "emscripten/prim.c" // emmalloc_*, + pthread support |
| 23 | |
| 24 | #else |
| 25 | #include "unix/prim.c" // mmap() (Linux, macOSX, BSD, Illumnos, Haiku, DragonFly, etc.) |
| 26 | |
| 27 | #endif |
| 28 | |
| 29 | // Generic process initialization |
| 30 | #ifndef MI_PRIM_HAS_PROCESS_ATTACH |
| 31 | #if defined(__GNUC__) || defined(__clang__) |
| 32 | // gcc,clang: use the constructor/destructor attribute |
| 33 | // which for both seem to run before regular constructors/destructors |
| 34 | #if defined(__clang__) |
| 35 | #define mi_attr_constructor __attribute__((constructor(101))) |
| 36 | #define mi_attr_destructor __attribute__((destructor(101))) |
| 37 | #else |
| 38 | #define mi_attr_constructor __attribute__((constructor)) |
| 39 | #define mi_attr_destructor __attribute__((destructor)) |
| 40 | #endif |
| 41 | static void mi_attr_constructor mi_process_attach(void) { |
| 42 | _mi_auto_process_init(); |
| 43 | } |
| 44 | static void mi_attr_destructor mi_process_detach(void) { |
| 45 | _mi_auto_process_done(); |
| 46 | } |
| 47 | #elif defined(__cplusplus) |
| 48 | // C++: use static initialization to detect process start/end |
| 49 | // This is not guaranteed to be first/last but the best we can generally do? |
| 50 | struct mi_init_done_t { |
| 51 | mi_init_done_t() { |
| 52 | _mi_auto_process_init(); |
| 53 | } |
| 54 | ~mi_init_done_t() { |
| 55 | _mi_auto_process_done(); |
| 56 | } |
| 57 | }; |
| 58 | static mi_init_done_t mi_init_done; |
| 59 | #else |
| 60 | #pragma message("define a way to call _mi_auto_process_init/done on your platform") |
| 61 | #endif |
| 62 | #endif |
| 63 | |
| 64 | // Generic allocator init/done callback |
| 65 | #ifndef MI_PRIM_HAS_ALLOCATOR_INIT |
| 66 | bool _mi_is_redirected(void) { |
| 67 | return false; |
| 68 | } |
| 69 | bool _mi_allocator_init(const char** message) { |
| 70 | if (message != NULL) { *message = NULL; } |
| 71 | return true; |
| 72 | } |
| 73 | void _mi_allocator_done(void) { |
| 74 | // nothing to do |
| 75 | } |
| 76 | #endif |
| 77 | |