From 98c30dfac17e751fd97b9cdc356fc5d372e8e7c5 Mon Sep 17 00:00:00 2001 From: John Sully Date: Tue, 16 Apr 2019 13:34:22 -0400 Subject: [PATCH] Fix module linking and crashing issues Former-commit-id: 1c953a4d5b332a0758d77d378c0caffa4d9bcfe4 --- src/Makefile | 4 ++-- src/module.cpp | 2 +- src/redismodule.h | 9 +++++++++ src/{zmalloc.c => zmalloc.cpp} | 31 +++++++++++++++++-------------- 4 files changed, 29 insertions(+), 17 deletions(-) rename src/{zmalloc.c => zmalloc.cpp} (94%) diff --git a/src/Makefile b/src/Makefile index c787de7f9..555d0c629 100644 --- a/src/Makefile +++ b/src/Makefile @@ -270,7 +270,7 @@ $(REDIS_CLI_NAME): $(REDIS_CLI_OBJ) $(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ) $(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS) -dict-benchmark: dict.cpp zmalloc.c sds.c siphash.c +dict-benchmark: dict.cpp zmalloc.cpp sds.c siphash.c $(REDIS_CC) $(FINAL_CFLAGS) $^ -D DICT_BENCHMARK_MAIN -o $@ $(FINAL_LIBS) # Because the jemalloc.h header is generated as a part of the jemalloc build, @@ -312,7 +312,7 @@ lcov: @genhtml --legend -o lcov-html redis.info test-sds: sds.c sds.h - $(REDIS_CC) sds.c zmalloc.c -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test + $(REDIS_CC) sds.c zmalloc.cpp -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test /tmp/sds_test .PHONY: lcov diff --git a/src/module.cpp b/src/module.cpp index 3a109c1d5..c3448e89f 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -5077,7 +5077,7 @@ dictType moduleAPIDictType = { NULL /* val destructor */ }; -int moduleRegisterApi(const char *funcname, void *funcptr) { +extern "C" int moduleRegisterApi(const char *funcname, void *funcptr) { return dictAdd(server.moduleapi, (char*)funcname, funcptr); } diff --git a/src/redismodule.h b/src/redismodule.h index 259a5f1db..217025319 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -5,6 +5,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + /* ---------------- Defines common between core and modules --------------- */ /* Error status return values. */ @@ -538,4 +542,9 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int #define RedisModuleString robj #endif /* REDISMODULE_CORE */ + +#ifdef __cplusplus +} +#endif + #endif /* REDISMOUDLE_H */ diff --git a/src/zmalloc.c b/src/zmalloc.cpp similarity index 94% rename from src/zmalloc.c rename to src/zmalloc.cpp index 3847ee75a..cb402b072 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.cpp @@ -36,7 +36,7 @@ * for instance to free results obtained by backtrace_symbols(). We need * to define this function before including zmalloc.h that may shadow the * free implementation if we use jemalloc or another non standard allocator. */ -void zlibc_free(void *ptr) { +extern "C" void zlibc_free(void *ptr) { free(ptr); } @@ -49,13 +49,16 @@ void zlibc_free(void *ptr) { #ifdef HAVE_MALLOC_SIZE #define PREFIX_SIZE (0) #else +#define PREFIX_SIZE 16 #if defined(__sun) || defined(__sparc) || defined(__sparc__) -#define PREFIX_SIZE (sizeof(long long)) +static_assert(PREFIX_SIZE >= (sizeof(long long)), ""); #else -#define PREFIX_SIZE (sizeof(size_t)) +static_assert(PREFIX_SIZE >= (sizeof(size_t)), ""); #endif #endif +static_assert((PREFIX_SIZE % 16) == 0, "Our prefix must be modulo 16-bytes or our pointers will not be aligned"); + /* Explicitly override malloc/free etc when using tcmalloc. */ #if defined(USE_MEMKIND) #define malloc(size, type) salloc(size, type) @@ -104,9 +107,9 @@ static void zmalloc_default_oom(size_t size) { static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom; -void *zmalloc(size_t size, enum MALLOC_CLASS class) { - (void)class; - void *ptr = malloc(size+PREFIX_SIZE, class); +void *zmalloc(size_t size, enum MALLOC_CLASS mclass) { + (void)mclass; + void *ptr = malloc(size+PREFIX_SIZE, mclass); if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE @@ -137,9 +140,9 @@ void zfree_no_tcache(void *ptr) { } #endif -void *zcalloc(size_t size, enum MALLOC_CLASS class) { - (void)(class); - void *ptr = calloc(1, size+PREFIX_SIZE, class); +void *zcalloc(size_t size, enum MALLOC_CLASS mclass) { + (void)(mclass); + void *ptr = calloc(1, size+PREFIX_SIZE, mclass); if (!ptr) zmalloc_oom_handler(size); #ifdef HAVE_MALLOC_SIZE @@ -152,7 +155,7 @@ void *zcalloc(size_t size, enum MALLOC_CLASS class) { #endif } -void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) { +void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS mclass) { #ifndef HAVE_MALLOC_SIZE void *realptr; #endif @@ -163,10 +166,10 @@ void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) { zfree(ptr); return NULL; } - if (ptr == NULL) return zmalloc(size, class); + if (ptr == NULL) return zmalloc(size, mclass); #ifdef HAVE_MALLOC_SIZE oldsize = zmalloc_size(ptr); - newptr = realloc(ptr,size, class); + newptr = realloc(ptr,size, mclass); if (!newptr) zmalloc_oom_handler(size); update_zmalloc_stat_free(oldsize); @@ -175,7 +178,7 @@ void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) { #else realptr = (char*)ptr-PREFIX_SIZE; oldsize = *((size_t*)realptr); - newptr = realloc(realptr,size+PREFIX_SIZE, class); + newptr = realloc(realptr,size+PREFIX_SIZE, mclass); if (!newptr) zmalloc_oom_handler(size); *((size_t*)newptr) = size; @@ -222,7 +225,7 @@ void zfree(void *ptr) { char *zstrdup(const char *s) { size_t l = strlen(s)+1; - char *p = zmalloc(l, MALLOC_SHARED); + char *p = (char*)zmalloc(l, MALLOC_SHARED); memcpy(p,s,l); return p;