diff --git a/deps/memkind/src/include/memkind.h b/deps/memkind/src/include/memkind.h index 720d7a302..4a8046663 100644 --- a/deps/memkind/src/include/memkind.h +++ b/deps/memkind/src/include/memkind.h @@ -318,6 +318,7 @@ void memkind_free(memkind_t kind, void *ptr); int memkind_fd(struct memkind *kind); void memkind_pmem_remapfd(struct memkind *kind, int fdNew); int memkind_tmpfile(const char *dir, int *fd); +memkind_t memkind_get_kind(void *ptr); #ifdef __cplusplus } diff --git a/deps/memkind/src/src/memkind.c b/deps/memkind/src/src/memkind.c index 74d4b3c17..188eb6985 100644 --- a/deps/memkind/src/src/memkind.c +++ b/deps/memkind/src/src/memkind.c @@ -791,3 +791,18 @@ MEMKIND_EXPORT int memkind_get_kind_by_partition(int partition, return memkind_get_kind_by_partition_internal(partition, kind); } +int memkind_lookup_arena(void *ptr, unsigned int *arena); +MEMKIND_EXPORT memkind_t memkind_get_kind(void *ptr) +{ + unsigned arena; + int err = memkind_lookup_arena(ptr, &arena); + memkind_t kind = NULL; + if (MEMKIND_UNLIKELY(err)) + return NULL; + kind = get_kind_by_arena(arena); + + if (!kind) + return MEMKIND_DEFAULT; + + return kind; +} diff --git a/deps/memkind/src/src/memkind_arena.c b/deps/memkind/src/src/memkind_arena.c index 7919a5dc3..d93ee4282 100644 --- a/deps/memkind/src/src/memkind_arena.c +++ b/deps/memkind/src/src/memkind_arena.c @@ -435,7 +435,7 @@ static void tcache_finalize(void *args) } } -static inline int memkind_lookup_arena(void *ptr, unsigned int *arena) +int memkind_lookup_arena(void *ptr, unsigned int *arena) { size_t sz = sizeof(unsigned); unsigned temp_arena; diff --git a/src/acl.c b/src/acl.c index b37b4719a..6b6274098 100644 --- a/src/acl.c +++ b/src/acl.c @@ -286,7 +286,7 @@ void ACLAddAllowedSubcommand(user *u, unsigned long id, const char *sub) { /* Now we can make space for the new item (and the null term). */ items += 2; u->allowed_subcommands[id] = zrealloc(u->allowed_subcommands[id], - sizeof(sds)*items); + sizeof(sds)*items, MALLOC_LOCAL); u->allowed_subcommands[id][items-2] = sdsnew(sub); u->allowed_subcommands[id][items-1] = NULL; } diff --git a/src/ae.c b/src/ae.c index 7f59aecf1..80f1d8111 100644 --- a/src/ae.c +++ b/src/ae.c @@ -111,8 +111,8 @@ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) { if (eventLoop->maxfd >= setsize) return AE_ERR; if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR; - eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize); - eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize); + eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize, MALLOC_LOCAL); + eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL); eventLoop->setsize = setsize; /* Make sure that if we created new slots, they are initialized with diff --git a/src/ae_epoll.c b/src/ae_epoll.c index 298bcf6ce..d299c8c1b 100644 --- a/src/ae_epoll.c +++ b/src/ae_epoll.c @@ -58,7 +58,7 @@ static int aeApiCreate(aeEventLoop *eventLoop) { static int aeApiResize(aeEventLoop *eventLoop, int setsize) { aeApiState *state = eventLoop->apidata; - state->events = zrealloc(state->events, sizeof(struct epoll_event)*setsize); + state->events = zrealloc(state->events, sizeof(struct epoll_event)*setsize, MALLOC_LOCAL); return 0; } diff --git a/src/ae_kqueue.c b/src/ae_kqueue.c index 36148bf58..19ac9ffc1 100644 --- a/src/ae_kqueue.c +++ b/src/ae_kqueue.c @@ -60,7 +60,7 @@ static int aeApiCreate(aeEventLoop *eventLoop) { static int aeApiResize(aeEventLoop *eventLoop, int setsize) { aeApiState *state = eventLoop->apidata; - state->events = zrealloc(state->events, sizeof(struct kevent)*setsize); + state->events = zrealloc(state->events, sizeof(struct kevent)*setsize, MALLOC_LOCAL); return 0; } diff --git a/src/bitops.c b/src/bitops.c index e2b53169a..c35b2ca23 100644 --- a/src/bitops.c +++ b/src/bitops.c @@ -978,7 +978,7 @@ void bitfieldCommand(client *c) { } /* Populate the array of operations we'll process. */ - ops = zrealloc(ops,sizeof(*ops)*(numops+1)); + ops = zrealloc(ops,sizeof(*ops)*(numops+1), MALLOC_SHARED); ops[numops].offset = bitoffset; ops[numops].i64 = i64; ops[numops].opcode = opcode; diff --git a/src/cluster.c b/src/cluster.c index bd04646d0..5b3d9663e 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -842,7 +842,7 @@ int clusterNodeAddSlave(clusterNode *master, clusterNode *slave) { for (j = 0; j < master->numslaves; j++) if (master->slaves[j] == slave) return C_ERR; master->slaves = zrealloc(master->slaves, - sizeof(clusterNode*)*(master->numslaves+1)); + sizeof(clusterNode*)*(master->numslaves+1), MALLOC_LOCAL); master->slaves[master->numslaves] = slave; master->numslaves++; master->flags |= CLUSTER_NODE_MIGRATE_TO; @@ -5119,8 +5119,8 @@ void migrateCommand(client *c) { * the caller there was nothing to migrate. We don't return an error in * this case, since often this is due to a normal condition like the key * expiring in the meantime. */ - ov = zrealloc(ov,sizeof(robj*)*num_keys); - kv = zrealloc(kv,sizeof(robj*)*num_keys); + ov = zrealloc(ov,sizeof(robj*)*num_keys, MALLOC_LOCAL); + kv = zrealloc(kv,sizeof(robj*)*num_keys, MALLOC_LOCAL); int oi = 0; for (j = 0; j < num_keys; j++) { diff --git a/src/config.c b/src/config.c index 5f5fd6792..3e7733138 100644 --- a/src/config.c +++ b/src/config.c @@ -143,7 +143,7 @@ int yesnotoi(char *s) { } void appendServerSaveParams(time_t seconds, int changes) { - server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1)); + server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1), MALLOC_LOCAL); server.saveparams[server.saveparamslen].seconds = seconds; server.saveparams[server.saveparamslen].changes = changes; server.saveparamslen++; @@ -805,7 +805,7 @@ void loadServerConfigFromString(char *config) { if (err) goto loaderr; } } else if (!strcasecmp(argv[0],"scratch-file-path")) { - storage_init(argv[1]); + storage_init(argv[1], server.maxmemory); } else { err = "Bad directive or wrong number of arguments"; goto loaderr; } @@ -1642,7 +1642,7 @@ struct rewriteConfigState { /* Append the new line to the current configuration state. */ void rewriteConfigAppendLine(struct rewriteConfigState *state, sds line) { - state->lines = zrealloc(state->lines, sizeof(char*) * (state->numlines+1)); + state->lines = zrealloc(state->lines, sizeof(char*) * (state->numlines+1), MALLOC_LOCAL); state->lines[state->numlines++] = line; } diff --git a/src/geo.c b/src/geo.c index 1c792255e..8e89dbefb 100644 --- a/src/geo.c +++ b/src/geo.c @@ -64,7 +64,7 @@ geoArray *geoArrayCreate(void) { geoPoint *geoArrayAppend(geoArray *ga) { if (ga->used == ga->buckets) { ga->buckets = (ga->buckets == 0) ? 8 : ga->buckets*2; - ga->array = zrealloc(ga->array,sizeof(geoPoint)*ga->buckets); + ga->array = zrealloc(ga->array,sizeof(geoPoint)*ga->buckets, MALLOC_SHARED); } geoPoint *gp = ga->array+ga->used; ga->used++; diff --git a/src/intset.c b/src/intset.c index 4cf15f4db..328d50b19 100644 --- a/src/intset.c +++ b/src/intset.c @@ -104,7 +104,7 @@ intset *intsetNew(void) { /* Resize the intset */ static intset *intsetResize(intset *is, uint32_t len) { uint32_t size = len*intrev32ifbe(is->encoding); - is = zrealloc(is,sizeof(intset)+size); + is = zrealloc(is,sizeof(intset)+size, MALLOC_SHARED); return is; } diff --git a/src/listpack_malloc.h b/src/listpack_malloc.h index 4c0317528..314570997 100644 --- a/src/listpack_malloc.h +++ b/src/listpack_malloc.h @@ -40,6 +40,6 @@ #define LISTPACK_ALLOC_H #include "zmalloc.h" #define lp_malloc(size) zmalloc(size, MALLOC_SHARED) -#define lp_realloc zrealloc +#define lp_realloc(ptr, size) zrealloc(ptr, size, MALLOC_SHARED) #define lp_free zfree #endif diff --git a/src/module.c b/src/module.c index b06493a1b..e73494149 100644 --- a/src/module.c +++ b/src/module.c @@ -280,7 +280,7 @@ void RM_FreeDict(RedisModuleCtx *ctx, RedisModuleDict *d); * and in general is taken into account as memory allocated by Redis. * You should avoid using malloc(). */ void *RM_Alloc(size_t bytes) { - return zmalloc(bytes, MALLOC_LOCAL); + return zmalloc(bytes, MALLOC_SHARED); } /* Use like calloc(). Memory allocated with this function is reported in @@ -288,12 +288,12 @@ void *RM_Alloc(size_t bytes) { * and in general is taken into account as memory allocated by Redis. * You should avoid using calloc() directly. */ void *RM_Calloc(size_t nmemb, size_t size) { - return zcalloc(nmemb*size, MALLOC_LOCAL); + return zcalloc(nmemb*size, MALLOC_SHARED); } /* Use like realloc() for memory obtained with RedisModule_Alloc(). */ void* RM_Realloc(void *ptr, size_t bytes) { - return zrealloc(ptr,bytes); + return zrealloc(ptr,bytes, MALLOC_SHARED); } /* Use like free() for memory obtained by RedisModule_Alloc() and @@ -558,7 +558,7 @@ int RM_IsKeysPositionRequest(RedisModuleCtx *ctx) { void RM_KeyAtPos(RedisModuleCtx *ctx, int pos) { if (!(ctx->flags & REDISMODULE_CTX_KEYS_POS_REQUEST)) return; if (pos <= 0) return; - ctx->keys_pos = zrealloc(ctx->keys_pos,sizeof(int)*(ctx->keys_count+1)); + ctx->keys_pos = zrealloc(ctx->keys_pos,sizeof(int)*(ctx->keys_count+1), MALLOC_LOCAL); ctx->keys_pos[ctx->keys_count++] = pos; } @@ -735,7 +735,7 @@ void autoMemoryAdd(RedisModuleCtx *ctx, int type, void *ptr) { if (ctx->amqueue_used == ctx->amqueue_len) { ctx->amqueue_len *= 2; if (ctx->amqueue_len < 16) ctx->amqueue_len = 16; - ctx->amqueue = zrealloc(ctx->amqueue,sizeof(struct AutoMemEntry)*ctx->amqueue_len); + ctx->amqueue = zrealloc(ctx->amqueue,sizeof(struct AutoMemEntry)*ctx->amqueue_len, MALLOC_LOCAL); } ctx->amqueue[ctx->amqueue_used].type = type; ctx->amqueue[ctx->amqueue_used].ptr = ptr; @@ -1121,7 +1121,7 @@ int RM_ReplyWithArray(RedisModuleCtx *ctx, long len) { if (c == NULL) return REDISMODULE_OK; if (len == REDISMODULE_POSTPONED_ARRAY_LEN) { ctx->postponed_arrays = zrealloc(ctx->postponed_arrays,sizeof(void*)* - (ctx->postponed_arrays_count+1)); + (ctx->postponed_arrays_count+1), MALLOC_LOCAL); ctx->postponed_arrays[ctx->postponed_arrays_count] = addReplyDeferredLen(c); ctx->postponed_arrays_count++; @@ -2616,7 +2616,7 @@ robj **moduleCreateArgvFromUserFormat(const char *cmdname, const char *fmt, int /* As a first guess to avoid useless reallocations, size argv to * hold one argument for each char specifier in 'fmt'. */ argv_size = strlen(fmt)+1; /* +1 because of the command name. */ - argv = zrealloc(argv,sizeof(robj*)*argv_size); + argv = zrealloc(argv,sizeof(robj*)*argv_size, MALLOC_LOCAL); /* Build the arguments vector based on the format specifier. */ argv[0] = createStringObject(cmdname,strlen(cmdname)); @@ -2648,7 +2648,7 @@ robj **moduleCreateArgvFromUserFormat(const char *cmdname, const char *fmt, int * We resize by vector_len-1 elements, because we held * one element in argv for the vector already */ argv_size += vlen-1; - argv = zrealloc(argv,sizeof(robj*)*argv_size); + argv = zrealloc(argv,sizeof(robj*)*argv_size, MALLOC_LOCAL); size_t i = 0; for (i = 0; i < vlen; i++) { diff --git a/src/multi.c b/src/multi.c index 720ec90e2..6aff069a3 100644 --- a/src/multi.c +++ b/src/multi.c @@ -59,7 +59,7 @@ void queueMultiCommand(client *c) { int j; c->mstate.commands = zrealloc(c->mstate.commands, - sizeof(multiCmd)*(c->mstate.count+1)); + sizeof(multiCmd)*(c->mstate.count+1), MALLOC_LOCAL); mc = c->mstate.commands+c->mstate.count; mc->cmd = c->cmd; mc->argc = c->argc; diff --git a/src/networking.c b/src/networking.c index b853da5bd..d438e70b8 100644 --- a/src/networking.c +++ b/src/networking.c @@ -2170,7 +2170,7 @@ void rewriteClientCommandArgument(client *c, int i, robj *newval) { robj *oldval; if (i >= c->argc) { - c->argv = zrealloc(c->argv,sizeof(robj*)*(i+1)); + c->argv = zrealloc(c->argv,sizeof(robj*)*(i+1), MALLOC_LOCAL); c->argc = i+1; c->argv[i] = NULL; } diff --git a/src/object.c b/src/object.c index a5da2b142..2d4095942 100644 --- a/src/object.c +++ b/src/object.c @@ -1035,7 +1035,7 @@ struct redisMemOverhead *getMemoryOverheadData(void) { if (keyscount==0) continue; mh->total_keys += keyscount; - mh->db = zrealloc(mh->db,sizeof(mh->db[0])*(mh->num_dbs+1)); + mh->db = zrealloc(mh->db,sizeof(mh->db[0])*(mh->num_dbs+1), MALLOC_LOCAL); mh->db[mh->num_dbs].dbid = j; mem = dictSize(db->dict) * sizeof(dictEntry) + diff --git a/src/quicklist.c b/src/quicklist.c index ea0a2ae77..02771ef00 100644 --- a/src/quicklist.c +++ b/src/quicklist.c @@ -194,7 +194,7 @@ REDIS_STATIC int __quicklistCompressNode(quicklistNode *node) { zfree(lzf); return 0; } - lzf = zrealloc(lzf, sizeof(*lzf) + lzf->sz); + lzf = zrealloc(lzf, sizeof(*lzf) + lzf->sz, MALLOC_SHARED); zfree(node->zl); node->zl = (unsigned char *)lzf; node->encoding = QUICKLIST_NODE_ENCODING_LZF; diff --git a/src/rax_malloc.h b/src/rax_malloc.h index a5aea44d9..81a5815ba 100644 --- a/src/rax_malloc.h +++ b/src/rax_malloc.h @@ -39,6 +39,6 @@ #define RAX_ALLOC_H #include "zmalloc.h" #define rax_malloc(size) zmalloc(size, MALLOC_SHARED) -#define rax_realloc zrealloc +#define rax_realloc(ptr, size) zrealloc(ptr, size, MALLOC_SHARED) #define rax_free zfree #endif diff --git a/src/rdb.c b/src/rdb.c index 0374a3999..4065ca11b 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -2154,7 +2154,7 @@ void backgroundSaveDoneHandlerSocket(int exitcode, int bysignal) { /* Make space for enough elements as specified by the first * uint64_t element in the array. */ - ok_slaves = zrealloc(ok_slaves,sizeof(uint64_t)+readlen); + ok_slaves = zrealloc(ok_slaves,sizeof(uint64_t)+readlen, MALLOC_LOCAL); if (readlen && read(server.rdb_pipe_read_result_from_child, ok_slaves+1, readlen) != readlen) diff --git a/src/redis-benchmark.c b/src/redis-benchmark.c index 5dffd46b6..369bb0aa4 100644 --- a/src/redis-benchmark.c +++ b/src/redis-benchmark.c @@ -395,7 +395,7 @@ static client createClient(char *cmd, size_t len, client from) { c->randptr = zmalloc(sizeof(char*)*c->randfree, MALLOC_LOCAL); while ((p = strstr(p,"__rand_int__")) != NULL) { if (c->randfree == 0) { - c->randptr = zrealloc(c->randptr,sizeof(char*)*c->randlen*2); + c->randptr = zrealloc(c->randptr,sizeof(char*)*c->randlen*2, MALLOC_LOCAL); c->randfree += c->randlen; } c->randptr[c->randlen++] = p; @@ -653,7 +653,7 @@ int main(int argc, const char **argv) { client c; - storage_init(NULL); + storage_init(NULL, 0); srandom(time(NULL)); signal(SIGHUP, SIG_IGN); diff --git a/src/redis-cli.c b/src/redis-cli.c index abcfd2b78..d0dffb005 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -529,7 +529,7 @@ static void cliIntegrateHelp(void) { if (i != helpEntriesLen) continue; helpEntriesLen++; - helpEntries = zrealloc(helpEntries,sizeof(helpEntry)*helpEntriesLen); + helpEntries = zrealloc(helpEntries,sizeof(helpEntry)*helpEntriesLen, MALLOC_LOCAL); helpEntry *new = helpEntries+(helpEntriesLen-1); new->argc = 1; @@ -1815,7 +1815,7 @@ static void repl(void) { static int noninteractive(int argc, char **argv) { int retval = 0; if (config.stdinarg) { - argv = zrealloc(argv, (argc+1)*sizeof(char*)); + argv = zrealloc(argv, (argc+1)*sizeof(char*), MALLOC_LOCAL); argv[argc] = readArgFromStdin(); retval = issueCommand(argc+1, argv); } else { @@ -3456,7 +3456,7 @@ static int clusterManagerNodeLoadInfo(clusterManagerNode *node, int opts, sds dst = sdsnew(p); node->migrating_count += 2; node->migrating = zrealloc(node->migrating, - (node->migrating_count * sizeof(sds))); + (node->migrating_count * sizeof(sds)), MALLOC_LOCAL); node->migrating[node->migrating_count - 2] = slot; node->migrating[node->migrating_count - 1] = @@ -3470,7 +3470,7 @@ static int clusterManagerNodeLoadInfo(clusterManagerNode *node, int opts, sds src = sdsnew(p); node->importing_count += 2; node->importing = zrealloc(node->importing, - (node->importing_count * sizeof(sds))); + (node->importing_count * sizeof(sds)), MALLOC_LOCAL); node->importing[node->importing_count - 2] = slot; node->importing[node->importing_count - 1] = @@ -3704,7 +3704,7 @@ static sds clusterManagerGetConfigSignature(clusterManagerNode *node) { } else line = p; if (slotsdef[0] != '[') { c++; - slots = zrealloc(slots, (c * sizeof(char *))); + slots = zrealloc(slots, (c * sizeof(char *)), MALLOC_LOCAL); slots[c - 1] = slotsdef; } } @@ -3713,7 +3713,7 @@ static sds clusterManagerGetConfigSignature(clusterManagerNode *node) { qsort(slots, c, sizeof(char *), clusterManagerSlotCompare); node_count++; node_configs = - zrealloc(node_configs, (node_count * sizeof(char *))); + zrealloc(node_configs, (node_count * sizeof(char *)), MALLOC_LOCAL); /* Make room for '|' separators. */ tot_size += (sizeof(char) * (c - 1)); char *cfg = zmalloc((sizeof(char) * tot_size) + 1, MALLOC_LOCAL); @@ -6610,8 +6610,8 @@ static void findBigKeys(void) { /* Reallocate our type and size array if we need to */ if(keys->elements > arrsize) { - types = zrealloc(types, sizeof(int)*keys->elements); - sizes = zrealloc(sizes, sizeof(unsigned long long)*keys->elements); + types = zrealloc(types, sizeof(int)*keys->elements, MALLOC_LOCAL); + sizes = zrealloc(sizes, sizeof(unsigned long long)*keys->elements, MALLOC_LOCAL); if(!types || !sizes) { fprintf(stderr, "Failed to allocate storage for keys!\n"); @@ -6760,7 +6760,7 @@ static void findHotKeys(void) { /* Reallocate our freqs array if we need to */ if(keys->elements > arrsize) { - freqs = zrealloc(freqs, sizeof(unsigned long long)*keys->elements); + freqs = zrealloc(freqs, sizeof(unsigned long long)*keys->elements, MALLOC_LOCAL); if(!freqs) { fprintf(stderr, "Failed to allocate storage for keys!\n"); @@ -7176,7 +7176,7 @@ static void intrinsicLatencyMode(void) { int main(int argc, char **argv) { int firstarg; - storage_init(NULL); + storage_init(NULL, 0); config.hostip = sdsnew("127.0.0.1"); config.hostport = 6379; config.hostsocket = NULL; diff --git a/src/scripting.c b/src/scripting.c index 3b3b996e0..85da662e6 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -407,7 +407,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) { /* Build the arguments vector */ if (argv_size < argc) { - argv = zrealloc(argv,sizeof(robj*)*argc); + argv = zrealloc(argv,sizeof(robj*)*argc, MALLOC_LOCAL); argv_size = argc; } diff --git a/src/sds.c b/src/sds.c index a87173d94..cc574bade 100644 --- a/src/sds.c +++ b/src/sds.c @@ -228,7 +228,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { hdrlen = sdsHdrSize(type); if (oldtype==type) { - newsh = s_realloc(sh, hdrlen+newlen+1); + newsh = s_realloc(sh, hdrlen+newlen+1, MALLOC_SHARED); if (newsh == NULL) return NULL; s = (char*)newsh+hdrlen; } else { @@ -269,7 +269,7 @@ sds sdsRemoveFreeSpace(sds s) { * only if really needed. Otherwise if the change is huge, we manually * reallocate the string to use the different header type. */ if (oldtype==type || type > SDS_TYPE_8) { - newsh = s_realloc(sh, oldhdrlen+len+1); + newsh = s_realloc(sh, oldhdrlen+len+1, MALLOC_SHARED); if (newsh == NULL) return NULL; s = (char*)newsh+oldhdrlen; } else { @@ -829,7 +829,7 @@ sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *c sds *newtokens; slots *= 2; - newtokens = s_realloc(tokens,sizeof(sds)*slots); + newtokens = s_realloc(tokens,sizeof(sds)*slots, MALLOC_SHARED); if (newtokens == NULL) goto cleanup; tokens = newtokens; } @@ -1038,7 +1038,7 @@ sds *sdssplitargs(const char *line, int *argc) { if (*p) p++; } /* add the token to the vector */ - vector = s_realloc(vector,((*argc)+1)*sizeof(char*)); + vector = s_realloc(vector,((*argc)+1)*sizeof(char*), MALLOC_SHARED); vector[*argc] = current; (*argc)++; current = NULL; @@ -1112,7 +1112,7 @@ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) { * the programs SDS is linked to, if they want to touch the SDS internals * even if they use a different allocator. */ void *sds_malloc(size_t size) { return s_malloc(size, MALLOC_SHARED); } -void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); } +void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size, MALLOC_SHARED); } void sds_free(void *ptr) { s_free(ptr); } #if defined(SDS_TEST_MAIN) diff --git a/src/sentinel.c b/src/sentinel.c index 7add1e744..dc7da22e1 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -1499,7 +1499,7 @@ int sentinelResetMasterAndChangeAddress(sentinelRedisInstance *master, char *ip, sentinelRedisInstance *slave = dictGetVal(de); if (sentinelAddrIsEqual(slave->addr,newaddr)) continue; - slaves = zrealloc(slaves,sizeof(sentinelAddr*)*(numslaves+1)); + slaves = zrealloc(slaves,sizeof(sentinelAddr*)*(numslaves+1), MALLOC_LOCAL); slaves[numslaves++] = createSentinelAddr(slave->addr->ip, slave->addr->port); } @@ -1509,7 +1509,7 @@ int sentinelResetMasterAndChangeAddress(sentinelRedisInstance *master, char *ip, * as a slave as well, so that we'll be able to sense / reconfigure * the old master. */ if (!sentinelAddrIsEqual(newaddr,master->addr)) { - slaves = zrealloc(slaves,sizeof(sentinelAddr*)*(numslaves+1)); + slaves = zrealloc(slaves,sizeof(sentinelAddr*)*(numslaves+1), MALLOC_LOCAL); slaves[numslaves++] = createSentinelAddr(master->addr->ip, master->addr->port); } diff --git a/src/server.c b/src/server.c index 1444100b7..ce00e4711 100644 --- a/src/server.c +++ b/src/server.c @@ -2973,7 +2973,7 @@ int redisOpArrayAppend(redisOpArray *oa, struct redisCommand *cmd, int dbid, { redisOp *op; - oa->ops = zrealloc(oa->ops,sizeof(redisOp)*(oa->numops+1)); + oa->ops = zrealloc(oa->ops,sizeof(redisOp)*(oa->numops+1), MALLOC_LOCAL); op = oa->ops+oa->numops; op->cmd = cmd; op->dbid = dbid; @@ -4740,7 +4740,7 @@ int main(int argc, char **argv) { struct timeval tv; int j; - storage_init(NULL); + storage_init(NULL, 0); #ifdef REDIS_TEST if (argc == 3 && !strcasecmp(argv[1], "test")) { diff --git a/src/sparkline.c b/src/sparkline.c index e18ce98fd..5ee6c7252 100644 --- a/src/sparkline.c +++ b/src/sparkline.c @@ -70,7 +70,7 @@ void sparklineSequenceAddSample(struct sequence *seq, double value, char *label) if (value < seq->min) seq->min = value; else if (value > seq->max) seq->max = value; } - seq->samples = zrealloc(seq->samples,sizeof(struct sample)*(seq->length+1)); + seq->samples = zrealloc(seq->samples,sizeof(struct sample)*(seq->length+1), MALLOC_SHARED); seq->samples[seq->length].value = value; seq->samples[seq->length].label = label; seq->length++; diff --git a/src/storage-lite.c b/src/storage-lite.c index ed199ef45..9ed51db1b 100644 --- a/src/storage-lite.c +++ b/src/storage-lite.c @@ -7,15 +7,17 @@ #include #include "storage.h" #include +#include // initialize the memory subsystem. // NOTE: This may be called twice, first with NULL specifying we should use ram // later, after the configuration file is loaded with a path to where we should // place our temporary file. -void storage_init(const char *tmpfilePath) +void storage_init(const char *tmpfilePath, size_t cbReserve) { assert(tmpfilePath == NULL); (void)tmpfilePath; + (void)cbReserve; } void *salloc(size_t cb, enum MALLOC_CLASS class) @@ -35,7 +37,13 @@ void sfree(void *pv) free(pv); } -void *srealloc(void *pv, size_t cb) +void *srealloc(void *pv, size_t cb, enum MALLOC_CLASS class) { + (void)class; return realloc(pv, cb); } + +size_t salloc_usable_size(void *ptr) +{ + return malloc_usable_size(ptr); +} \ No newline at end of file diff --git a/src/storage.c b/src/storage.c index d6fcc1b8f..72472fd9f 100644 --- a/src/storage.c +++ b/src/storage.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "storage.h" struct memkind *mkdisk = NULL; @@ -125,7 +126,7 @@ int forkFile() // NOTE: This may be called twice, first with NULL specifying we should use ram // later, after the configuration file is loaded with a path to where we should // place our temporary file. -void storage_init(const char *tmpfilePath) +void storage_init(const char *tmpfilePath, size_t cbFileReserve) { if (tmpfilePath == NULL) { @@ -161,6 +162,11 @@ void storage_init(const char *tmpfilePath) } close(fdTest); + // Now lets make the file big + if (cbFileReserve == 0) + cbFileReserve = 1*1024*1024*1024; // 1 GB (enough to be interesting) + posix_fallocate64(memkind_fd(mkdisk), 0, cbFileReserve); + pool_initialize(&poolobj, sizeof(robj)); pool_initialize(&poolembstrobj, EMBSTR_ROBJ_SIZE); @@ -187,28 +193,34 @@ void sfree_objembstr(robj *obj) pool_free(&poolembstrobj, obj); } -void *salloc(size_t cb, enum MALLOC_CLASS class) +size_t salloc_usable_size(void *ptr) +{ + return memkind_malloc_usable_size(memkind_get_kind(ptr), ptr); +} + +static memkind_t kindFromClass(enum MALLOC_CLASS class) { switch (class) { case MALLOC_SHARED: - return memkind_malloc(mkdisk, cb); + return mkdisk; default: - return memkind_malloc(MEMKIND_DEFAULT, cb); + break; } - return NULL; + return MEMKIND_DEFAULT; +} + +void *salloc(size_t cb, enum MALLOC_CLASS class) +{ + if (cb == 0) + cb = 1; + + return memkind_malloc(kindFromClass(class), cb); } void *scalloc(size_t cb, size_t c, enum MALLOC_CLASS class) { - switch (class) - { - case MALLOC_SHARED: - return memkind_calloc(mkdisk, cb, c); - default: - return memkind_calloc(MEMKIND_DEFAULT, cb, c); - } - return NULL; + return memkind_calloc(kindFromClass(class), cb, c); } void sfree(void *pv) @@ -216,10 +228,9 @@ void sfree(void *pv) memkind_free(NULL, pv); } -void *srealloc(void *pv, size_t cb) +void *srealloc(void *pv, size_t cb, enum MALLOC_CLASS class) { - memkind_t kind = mkdisk; - return memkind_realloc(kind, pv, cb); + return memkind_realloc(kindFromClass(class), pv, cb); } int fdNew = -1; diff --git a/src/storage.h b/src/storage.h index f35bc62e3..06c3572a5 100644 --- a/src/storage.h +++ b/src/storage.h @@ -9,11 +9,12 @@ enum MALLOC_CLASS MALLOC_SHARED, }; -void storage_init(const char *tmpfilePath); +void storage_init(const char *tmpfilePath, size_t cbFileReserve); void *salloc(size_t cb, enum MALLOC_CLASS class); void *scalloc(size_t cb, size_t c, enum MALLOC_CLASS class); void sfree(void*); -void *srealloc(void *pv, size_t cb); +void *srealloc(void *pv, size_t cb, enum MALLOC_CLASS class); +size_t salloc_usable_size(void *ptr); #endif diff --git a/src/ziplist.c b/src/ziplist.c index 733e2e685..ff0422d21 100644 --- a/src/ziplist.c +++ b/src/ziplist.c @@ -587,7 +587,7 @@ unsigned char *ziplistNew(void) { /* Resize the ziplist. */ unsigned char *ziplistResize(unsigned char *zl, unsigned int len) { - zl = zrealloc(zl,len); + zl = zrealloc(zl,len, MALLOC_SHARED); ZIPLIST_BYTES(zl) = intrev32ifbe(len); zl[len-1] = ZIP_END; return zl; @@ -903,7 +903,7 @@ unsigned char *ziplistMerge(unsigned char **first, unsigned char **second) { size_t second_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*second)); /* Extend target to new zlbytes then append or prepend source. */ - target = zrealloc(target, zlbytes); + target = zrealloc(target, zlbytes, MALLOC_SHARED); if (append) { /* append == appending to target */ /* Copy source after target (copying over original [END]): diff --git a/src/zipmap.c b/src/zipmap.c index 88e7f8706..54c32f214 100644 --- a/src/zipmap.c +++ b/src/zipmap.c @@ -200,7 +200,7 @@ static unsigned int zipmapRawEntryLength(unsigned char *p) { } static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) { - zm = zrealloc(zm, len); + zm = zrealloc(zm, len, MALLOC_SHARED); zm[len-1] = ZIPMAP_END; return zm; } diff --git a/src/zmalloc.c b/src/zmalloc.c index 3af72417f..35599ce0e 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -60,7 +60,7 @@ void zlibc_free(void *ptr) { #if defined(USE_MEMKIND) #define malloc(size, type) salloc(size, type) #define calloc(count, size, type) scalloc(count, size, type) -#define realloc(ptr, size) srealloc(ptr, size) +#define realloc(ptr, size, type) srealloc(ptr, size, type) #define free(ptr) sfree(ptr) #elif defined(USE_TCMALLOC) #define malloc(size) tc_malloc(size) @@ -156,17 +156,17 @@ void *zcalloc(size_t size, enum MALLOC_CLASS class) { #endif } -void *zrealloc(void *ptr, size_t size) { +void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class) { #ifndef HAVE_MALLOC_SIZE void *realptr; #endif size_t oldsize; void *newptr; - if (ptr == NULL) return zmalloc(size, MALLOC_SHARED); + if (ptr == NULL) return zmalloc(size, class); #ifdef HAVE_MALLOC_SIZE oldsize = zmalloc_size(ptr); - newptr = realloc(ptr,size); + newptr = realloc(ptr,size, class); if (!newptr) zmalloc_oom_handler(size); update_zmalloc_stat_free(oldsize); diff --git a/src/zmalloc.h b/src/zmalloc.h index 2ad37b905..63ae7074a 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -36,10 +36,13 @@ #define __str(s) #s #include "storage.h" +#define USE_MEMKIND 1 #if defined(USE_MEMKIND) #define ZMALLOC_LIB ("memkind") #undef USE_JEMALLOC #define USE_MALLOC_CLASS 1 + #define HAVE_MALLOC_SIZE 1 + #define zmalloc_size(p) salloc_usable_size(p) #elif defined(USE_TCMALLOC) #define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR)) #include @@ -84,7 +87,7 @@ void *zmalloc(size_t size, enum MALLOC_CLASS class); void *zcalloc(size_t size, enum MALLOC_CLASS class); -void *zrealloc(void *ptr, size_t size); +void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS class); void zfree(void *ptr); char *zstrdup(const char *s); size_t zmalloc_used_memory(void);