Unverified Commit eeb0f142 authored by guybe7's avatar guybe7 Committed by GitHub
Browse files

Add RM_TryAlloc (#10541)

Similarly to LCS, some modules would want to try to allocate memory, and
fail gracefully if the allocation fails
parent 719db14e
...@@ -464,11 +464,18 @@ static int moduleConvertArgFlags(int flags); ...@@ -464,11 +464,18 @@ static int moduleConvertArgFlags(int flags);
/* Use like malloc(). Memory allocated with this function is reported in /* Use like malloc(). Memory allocated with this function is reported in
* Redis INFO memory, used for keys eviction according to maxmemory settings * Redis INFO memory, used for keys eviction according to maxmemory settings
* and in general is taken into account as memory allocated by Redis. * and in general is taken into account as memory allocated by Redis.
* You should avoid using malloc(). */ * You should avoid using malloc().
* This function panics if unable to allocate enough memory. */
void *RM_Alloc(size_t bytes) { void *RM_Alloc(size_t bytes) {
return zmalloc(bytes); return zmalloc(bytes);
} }
/* Similar to RM_Alloc, but returns NULL in case of allocation failure, instead
* of panicking. */
void *RM_TryAlloc(size_t bytes) {
return ztrymalloc(bytes);
}
/* Use like calloc(). Memory allocated with this function is reported in /* Use like calloc(). Memory allocated with this function is reported in
* Redis INFO memory, used for keys eviction according to maxmemory settings * Redis INFO memory, used for keys eviction according to maxmemory settings
* and in general is taken into account as memory allocated by Redis. * and in general is taken into account as memory allocated by Redis.
...@@ -12241,6 +12248,7 @@ void moduleRegisterCoreAPI(void) { ...@@ -12241,6 +12248,7 @@ void moduleRegisterCoreAPI(void) {
server.moduleapi = dictCreate(&moduleAPIDictType); server.moduleapi = dictCreate(&moduleAPIDictType);
server.sharedapi = dictCreate(&moduleAPIDictType); server.sharedapi = dictCreate(&moduleAPIDictType);
REGISTER_API(Alloc); REGISTER_API(Alloc);
REGISTER_API(TryAlloc);
REGISTER_API(Calloc); REGISTER_API(Calloc);
REGISTER_API(Realloc); REGISTER_API(Realloc);
REGISTER_API(Free); REGISTER_API(Free);
......
...@@ -861,6 +861,7 @@ typedef struct RedisModuleTypeMethods { ...@@ -861,6 +861,7 @@ typedef struct RedisModuleTypeMethods {
#endif #endif
REDISMODULE_API void * (*RedisModule_Alloc)(size_t bytes) REDISMODULE_ATTR; REDISMODULE_API void * (*RedisModule_Alloc)(size_t bytes) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_TryAlloc)(size_t bytes) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_Realloc)(void *ptr, size_t bytes) REDISMODULE_ATTR; REDISMODULE_API void * (*RedisModule_Realloc)(void *ptr, size_t bytes) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_Free)(void *ptr) REDISMODULE_ATTR; REDISMODULE_API void (*RedisModule_Free)(void *ptr) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_Calloc)(size_t nmemb, size_t size) REDISMODULE_ATTR; REDISMODULE_API void * (*RedisModule_Calloc)(size_t nmemb, size_t size) REDISMODULE_ATTR;
...@@ -1191,6 +1192,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int ...@@ -1191,6 +1192,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
void *getapifuncptr = ((void**)ctx)[0]; void *getapifuncptr = ((void**)ctx)[0];
RedisModule_GetApi = (int (*)(const char *, void *)) (unsigned long)getapifuncptr; RedisModule_GetApi = (int (*)(const char *, void *)) (unsigned long)getapifuncptr;
REDISMODULE_GET_API(Alloc); REDISMODULE_GET_API(Alloc);
REDISMODULE_GET_API(TryAlloc);
REDISMODULE_GET_API(Calloc); REDISMODULE_GET_API(Calloc);
REDISMODULE_GET_API(Free); REDISMODULE_GET_API(Free);
REDISMODULE_GET_API(Realloc); REDISMODULE_GET_API(Realloc);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment