update test files and overriding

This commit is contained in:
daan 2019-07-22 01:36:16 -07:00
parent 6d9fab5af4
commit 219d46ff0c
9 changed files with 67 additions and 41 deletions

View file

@ -24,14 +24,21 @@ target_link_libraries(dynamic-override PUBLIC mimalloc)
add_executable(dynamic-override-cxx main-override.cpp)
target_link_libraries(dynamic-override-cxx PUBLIC mimalloc)
# with a static library
# overriding with a static object file works reliable as the symbols in the
# object file have priority over those in library files
add_executable(static-override-obj main-override.c ${MIMALLOC_TARGET_DIR}/mimalloc.o)
target_include_directories(static-override-obj PUBLIC ${MIMALLOC_TARGET_DIR}/include)
target_link_libraries(static-override-obj PUBLIC pthread)
# overriding with a static library works too if using the `mimalloc-override.h`
# header to redefine malloc/free.
add_executable(static-override-static main-override-static.c)
target_link_libraries(static-override-static PUBLIC mimalloc-static)
# overriding with a static library: this may not work if the library is linked too late
add_executable(static-override main-override.c)
target_link_libraries(static-override PUBLIC mimalloc-static)
add_executable(static-override-cxx main-override.cpp)
target_link_libraries(static-override-cxx PUBLIC mimalloc-static)
# and with a static object file
add_executable(static-override-obj main-override.c ${MIMALLOC_TARGET_DIR}/mimalloc.o)
target_include_directories(static-override-obj PUBLIC ${MIMALLOC_TARGET_DIR}/include)
target_link_libraries(static-override-obj PUBLIC pthread)

View file

@ -0,0 +1,31 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <mimalloc.h>
#include <mimalloc-override.h> // redefines malloc etc.
int main() {
mi_version();
void* p1 = malloc(78);
void* p2 = malloc(24);
free(p1);
p1 = malloc(8);
//char* s = strdup("hello\n");
free(p2);
p2 = malloc(16);
p1 = realloc(p1, 32);
free(p1);
free(p2);
//free(s);
//mi_collect(true);
/* now test if override worked by allocating/freeing across the api's*/
//p1 = mi_malloc(32);
//free(p1);
//p2 = malloc(32);
//mi_free(p2);
mi_stats_print(NULL);
return 0;
}

View file

@ -3,11 +3,10 @@
#include <assert.h>
#include <string.h>
//#include <mimalloc.h>
#include <mimalloc.h>
int main() {
//mi_stats_reset();
mi_version(); // ensure mimalloc library is linked
void* p1 = malloc(78);
void* p2 = malloc(24);
free(p1);
@ -26,6 +25,6 @@ int main() {
//free(p1);
//p2 = malloc(32);
//mi_free(p2);
mi_stats_print(NULL);
return 0;
}

View file

@ -4,8 +4,6 @@
#include <string.h>
#include <mimalloc.h>
#include <mimalloc-override.h>
#include <new>
static void* p = malloc(8);
@ -24,16 +22,15 @@ public:
};
int main() {
//mi_malloc_override();
mi_stats_reset();
int main() {
mi_version();
atexit(free_p);
void* p1 = malloc(78);
void* p2 = _aligned_malloc(24,16);
void* p2 = mi_malloc_aligned(16,24);
free(p1);
p1 = malloc(8);
char* s = _strdup("hello\n");
_aligned_free(p2);
char* s = mi_strdup("hello\n");
mi_free(p2);
p2 = malloc(16);
p1 = realloc(p1, 32);
free(p1);