| // test allocation in a DLL that is statically linked to mimalloc | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include "main-static-dep.h" | |
| #include <mimalloc.h> | |
| class Static { | |
| private: | |
| void* p; | |
| public: | |
| Static() { | |
| printf("static-dep: static constructor\n"); | |
| p = mi_malloc(64); | |
| return; | |
| } | |
| ~Static() { | |
| mi_free(p); | |
| printf("static-dep: static destructor\n"); | |
| return; | |
| } | |
| }; | |
| static Static s = Static(); | |
| #ifdef __cplusplus | |
| extern "C" | |
| #endif | |
| void __cdecl Test(void) { | |
| char* s = mi_mallocn_tp(char, 128); | |
| #ifdef _WIN32 | |
| strcpy_s(s, 128, "hello world!"); | |
| #else | |
| strlcpy(s, "hello world!", 128); | |
| #endif | |
| printf("message from static dll: %s\n", s); | |
| mi_free(s); | |
| } | |
| #ifdef _WIN32 | |
| #include <windows.h> | |
| BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID reserved) { | |
| (void)(reserved); | |
| (void)(module); | |
| if (reason==DLL_PROCESS_ATTACH) { | |
| printf("static-dep: dll attach\n"); | |
| } | |
| else if (reason==DLL_PROCESS_DETACH) { | |
| mi_option_enable(mi_option_destroy_on_exit); | |
| printf("static-dep: dll detach\n"); | |
| } | |
| return TRUE; | |
| } | |
| #endif |