Skip to content

Commit 3aedcbc

Browse files
committed
Fix module linking and crashing issues
1 parent 85e6220 commit 3aedcbc

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ $(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
270270
$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)
271271
$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)
272272

273-
dict-benchmark: dict.cpp zmalloc.c sds.c siphash.c
273+
dict-benchmark: dict.cpp zmalloc.cpp sds.c siphash.c
274274
$(REDIS_CC) $(FINAL_CFLAGS) $^ -D DICT_BENCHMARK_MAIN -o $@ $(FINAL_LIBS)
275275

276276
# Because the jemalloc.h header is generated as a part of the jemalloc build,
@@ -312,7 +312,7 @@ lcov:
312312
@genhtml --legend -o lcov-html redis.info
313313

314314
test-sds: sds.c sds.h
315-
$(REDIS_CC) sds.c zmalloc.c -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test
315+
$(REDIS_CC) sds.c zmalloc.cpp -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test
316316
/tmp/sds_test
317317

318318
.PHONY: lcov

src/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5077,7 +5077,7 @@ dictType moduleAPIDictType = {
50775077
NULL /* val destructor */
50785078
};
50795079

5080-
int moduleRegisterApi(const char *funcname, void *funcptr) {
5080+
extern "C" int moduleRegisterApi(const char *funcname, void *funcptr) {
50815081
return dictAdd(server.moduleapi, (char*)funcname, funcptr);
50825082
}
50835083

src/redismodule.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <stdint.h>
66
#include <stdio.h>
77

8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
812
/* ---------------- Defines common between core and modules --------------- */
913

1014
/* Error status return values. */
@@ -538,4 +542,9 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
538542
#define RedisModuleString robj
539543

540544
#endif /* REDISMODULE_CORE */
545+
546+
#ifdef __cplusplus
547+
}
548+
#endif
549+
541550
#endif /* REDISMOUDLE_H */

src/zmalloc.c renamed to src/zmalloc.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* for instance to free results obtained by backtrace_symbols(). We need
3737
* to define this function before including zmalloc.h that may shadow the
3838
* free implementation if we use jemalloc or another non standard allocator. */
39-
void zlibc_free(void *ptr) {
39+
extern "C" void zlibc_free(void *ptr) {
4040
free(ptr);
4141
}
4242

@@ -49,13 +49,16 @@ void zlibc_free(void *ptr) {
4949
#ifdef HAVE_MALLOC_SIZE
5050
#define PREFIX_SIZE (0)
5151
#else
52+
#define PREFIX_SIZE 16
5253
#if defined(__sun) || defined(__sparc) || defined(__sparc__)
53-
#define PREFIX_SIZE (sizeof(long long))
54+
static_assert(PREFIX_SIZE >= (sizeof(long long)), "");
5455
#else
55-
#define PREFIX_SIZE (sizeof(size_t))
56+
static_assert(PREFIX_SIZE >= (sizeof(size_t)), "");
5657
#endif
5758
#endif
5859

60+
static_assert((PREFIX_SIZE % 16) == 0, "Our prefix must be modulo 16-bytes or our pointers will not be aligned");
61+
5962
/* Explicitly override malloc/free etc when using tcmalloc. */
6063
#if defined(USE_MEMKIND)
6164
#define malloc(size, type) salloc(size, type)
@@ -104,9 +107,9 @@ static void zmalloc_default_oom(size_t size) {
104107

105108
static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;
106109

107-
void *zmalloc(size_t size, enum MALLOC_CLASS class) {
108-
(void)class;
109-
void *ptr = malloc(size+PREFIX_SIZE, class);
110+
void *zmalloc(size_t size, enum MALLOC_CLASS mclass) {
111+
(void)mclass;
112+
void *ptr = malloc(size+PREFIX_SIZE, mclass);
110113

111114
if (!ptr) zmalloc_oom_handler(size);
112115
#ifdef HAVE_MALLOC_SIZE
@@ -137,9 +140,9 @@ void zfree_no_tcache(void *ptr) {
137140
}
138141
#endif
139142

140-
void *zcalloc(size_t size, enum MALLOC_CLASS class) {
141-
(void)(class);
142-
void *ptr = calloc(1, size+PREFIX_SIZE, class);
143+
void *zcalloc(size_t size, enum MALLOC_CLASS mclass) {
144+
(void)(mclass);
145+
void *ptr = calloc(1, size+PREFIX_SIZE, mclass);
143146

144147
if (!ptr) zmalloc_oom_handler(size);
145148
#ifdef HAVE_MALLOC_SIZE
@@ -152,7 +155,7 @@ void *zcalloc(size_t size, enum MALLOC_CLASS class) {
152155
#endif
153156
}
154157

155-
void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) {
158+
void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS mclass) {
156159
#ifndef HAVE_MALLOC_SIZE
157160
void *realptr;
158161
#endif
@@ -163,10 +166,10 @@ void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) {
163166
zfree(ptr);
164167
return NULL;
165168
}
166-
if (ptr == NULL) return zmalloc(size, class);
169+
if (ptr == NULL) return zmalloc(size, mclass);
167170
#ifdef HAVE_MALLOC_SIZE
168171
oldsize = zmalloc_size(ptr);
169-
newptr = realloc(ptr,size, class);
172+
newptr = realloc(ptr,size, mclass);
170173
if (!newptr) zmalloc_oom_handler(size);
171174

172175
update_zmalloc_stat_free(oldsize);
@@ -175,7 +178,7 @@ void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) {
175178
#else
176179
realptr = (char*)ptr-PREFIX_SIZE;
177180
oldsize = *((size_t*)realptr);
178-
newptr = realloc(realptr,size+PREFIX_SIZE, class);
181+
newptr = realloc(realptr,size+PREFIX_SIZE, mclass);
179182
if (!newptr) zmalloc_oom_handler(size);
180183

181184
*((size_t*)newptr) = size;
@@ -222,7 +225,7 @@ void zfree(void *ptr) {
222225

223226
char *zstrdup(const char *s) {
224227
size_t l = strlen(s)+1;
225-
char *p = zmalloc(l, MALLOC_SHARED);
228+
char *p = (char*)zmalloc(l, MALLOC_SHARED);
226229

227230
memcpy(p,s,l);
228231
return p;

0 commit comments

Comments
 (0)