Fix module linking and crashing issues

Former-commit-id: 1c953a4d5b332a0758d77d378c0caffa4d9bcfe4
This commit is contained in:
John Sully 2019-04-16 13:34:22 -04:00
parent e79d075076
commit 98c30dfac1
4 changed files with 29 additions and 17 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -5,6 +5,10 @@
#include <stdint.h>
#include <stdio.h>
#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 */

View File

@ -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;