From 7d835d222b0ec0ac60cb4360139f1dde6a172e4d Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 18 Apr 2014 22:35:47 +0800 Subject: [PATCH] use macro for replace malloc api (see skynet_malloc.h) --- Makefile | 7 +-- lualib-src/lua-bson.c | 3 + lualib-src/lua-memory.c | 3 + lualib-src/lua-mongo.c | 3 + lualib-src/lua-netpack.c | 3 + lualib-src/lua-seri.c | 3 + lualib-src/lua-socket.c | 3 + service-src/service_harbor.c | 4 +- service-src/service_snlua.c | 2 +- skynet-src/malloc_hook.c | 104 +++++++++++++++++------------------ skynet-src/malloc_hook.h | 1 + skynet-src/skynet.h | 2 + skynet-src/skynet_env.c | 2 + skynet-src/skynet_error.c | 2 +- skynet-src/skynet_handle.c | 5 +- skynet-src/skynet_main.c | 3 + skynet-src/skynet_malloc.h | 18 ++++++ skynet-src/skynet_module.c | 7 ++- skynet-src/skynet_monitor.c | 3 + skynet-src/skynet_server.c | 4 +- skynet-src/skynet_socket.c | 4 +- skynet-src/skynet_timer.c | 4 +- skynet-src/socket_server.c | 3 + 23 files changed, 125 insertions(+), 68 deletions(-) create mode 100644 skynet-src/skynet_malloc.h diff --git a/Makefile b/Makefile index 958c1fb8..f972007d 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,6 @@ $(LUA_STATICLIB) : JEMALLOC_STATICLIB := 3rd/jemalloc/lib/libjemalloc_pic.a JEMALLOC_INC := 3rd/jemalloc/include/jemalloc -NOBUILDIN_MALLOC := -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -fno-builtin-memalign all : jemalloc .PHONY : jemalloc @@ -52,7 +51,7 @@ all : \ $(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so) $(SKYNET_BUILD_PATH)/skynet : $(foreach v, $(SKYNET_SRC), skynet-src/$(v)) $(LUA_LIB) $(JEMALLOC_STATICLIB) - $(CC) $(CFLAGS) $(NOBUILDIN_MALLOC) -o $@ $^ -Iskynet-src -I$(JEMALLOC_INC) $(LDFLAGS) $(EXPORT) $(LIBS) + $(CC) $(CFLAGS) -o $@ $^ -Iskynet-src -I$(JEMALLOC_INC) $(LDFLAGS) $(EXPORT) $(LIBS) $(LUA_CLIB_PATH) : mkdir $(LUA_CLIB_PATH) @@ -77,10 +76,10 @@ $(LUA_CLIB_PATH)/int64.so : 3rd/lua-int64/int64.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) $^ -o $@ $(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src $(LUA_CLIB_PATH)/mongo.so : lualib-src/lua-mongo.c | $(LUA_CLIB_PATH) - $(CC) $(CFLAGS) $(SHARED) $^ -o $@ + $(CC) $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src $(LUA_CLIB_PATH)/md5.so : 3rd/lua-md5/md5.c 3rd/lua-md5/md5lib.c 3rd/lua-md5/compat-5.2.c | $(LUA_CLIB_PATH) $(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@ diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 1fdf6b5f..04907181 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include #include diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index fa7c026c..e05d853a 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include #include diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index fed26d89..9c956b0a 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include #include diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 492698e7..8f3bbd0a 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_socket.h" #include diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 56f81769..2e3b96d3 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -2,6 +2,9 @@ https://github.com/cloudwu/lua-serialize */ +// include skynet.h first for malloc hook +#include "skynet.h" + #include #include #include diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index 6a5651c2..26be2110 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include #include #include diff --git a/service-src/service_harbor.c b/service-src/service_harbor.c index b0a2121f..1e2d2496 100644 --- a/service-src/service_harbor.c +++ b/service-src/service_harbor.c @@ -586,14 +586,14 @@ harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) { char local_addr[sz]; int harbor_id = 0; sscanf(args,"%s %s %d",master_addr, local_addr, &harbor_id); - h->master_addr = strdup(master_addr); + h->master_addr = skynet_strdup(master_addr); h->id = harbor_id; h->master_fd = _connect_to(h, master_addr, true); if (h->master_fd == -1) { fprintf(stderr, "Harbor: Connect to master failed\n"); exit(1); } - h->local_addr = strdup(local_addr); + h->local_addr = skynet_strdup(local_addr); _launch_gate(ctx, local_addr); skynet_callback(ctx, h, _mainloop); diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 0eab0ec8..86437a89 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -267,7 +267,7 @@ struct snlua * snlua_create(void) { struct snlua * l = malloc(sizeof(*l)); memset(l,0,sizeof(*l)); - l->L = luaL_newstate(); + l->L = lua_newstate(skynet_lalloc, NULL); l->init = _init; return l; } diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index bba82ac4..6203302d 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -3,11 +3,11 @@ #include #include "malloc_hook.h" - #include "jemalloc.h" #include "skynet.h" + static size_t _used_memory = 0; static size_t _memory_block = 0; typedef struct _mem_data { @@ -20,10 +20,6 @@ typedef struct _mem_data { static mem_data mem_stats[SLOT_SIZE]; -static void _init() { - memset(mem_stats, 0, sizeof(mem_stats)); -} - static size_t* get_allocated_field(handle) { int h = (handle & SLOT_SIZE) - 1; @@ -88,50 +84,6 @@ static void malloc_oom(size_t size) { abort(); } -// hook : malloc, realloc, memalign, free, calloc - -void * -malloc(size_t size) { - void* ptr = je_malloc(size + PREFIX_SIZE); - if(!ptr) malloc_oom(size); - return fill_prefix(ptr); -} - -void * -realloc(void *ptr, size_t size) { - if (ptr == NULL) return malloc(size); - - void* rawptr = clean_prefix(ptr); - void *newptr = je_realloc(rawptr, size+PREFIX_SIZE); - if(!newptr) malloc_oom(size); - return fill_prefix(newptr); -} - -#ifdef JEMALLOC_OVERRIDE_MEMALIGN - -void * -memalign(size_t alignment, size_t size) { - void *ptr = je_memalign(alignment, size+PREFIX_SIZE); - if(!ptr) malloc_oom(size); - return fill_prefix(ptr); -} - -#endif - -void -free(void *ptr) { - if (ptr == NULL) return; - void* rawptr = clean_prefix(ptr); - je_free(rawptr); -} - -void * -calloc(size_t nmemb,size_t size) { - void* ptr = je_calloc(nmemb + ((PREFIX_SIZE+size-1)/size), size ); - if(!ptr) malloc_oom(size); - return fill_prefix(ptr); -} - size_t malloc_used_memory(void) { return _used_memory; @@ -190,10 +142,54 @@ dump_c_mem() { printf("+total: %zdkb\n",total >> 10); } -/* - * init - */ -void __attribute__ ((constructor)) malloc_hook_init(void){ - _init(); +// hook : malloc, realloc, memalign, free, calloc + +void * +skynet_malloc(size_t size) { + void* ptr = je_malloc(size + PREFIX_SIZE); + if(!ptr) malloc_oom(size); + return fill_prefix(ptr); +} + +void * +skynet_realloc(void *ptr, size_t size) { + if (ptr == NULL) return malloc(size); + + void* rawptr = clean_prefix(ptr); + void *newptr = je_realloc(rawptr, size+PREFIX_SIZE); + if(!newptr) malloc_oom(size); + return fill_prefix(newptr); +} + +void +skynet_free(void *ptr) { + if (ptr == NULL) return; + void* rawptr = clean_prefix(ptr); + je_free(rawptr); +} + +void * +skynet_calloc(size_t nmemb,size_t size) { + void* ptr = je_calloc(nmemb + ((PREFIX_SIZE+size-1)/size), size ); + if(!ptr) malloc_oom(size); + return fill_prefix(ptr); +} + +char * +skynet_strdup(const char *str) { + size_t sz = strlen(str); + char * ret = skynet_malloc(sz+1); + memcpy(ret, str, sz+1); + return ret; +} + +void * +skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) { + if (nsize == 0) { + skynet_free(ptr); + return NULL; + } else { + return skynet_realloc(ptr, nsize); + } } diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index c26701a1..e7beb1a7 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -1,5 +1,6 @@ #ifndef __MALLOC_HOOK_H #define __MALLOC_HOOK_H + #include extern size_t malloc_used_memory(void); diff --git a/skynet-src/skynet.h b/skynet-src/skynet.h index cab05e62..ce82b7ac 100644 --- a/skynet-src/skynet.h +++ b/skynet-src/skynet.h @@ -1,6 +1,8 @@ #ifndef SKYNET_H #define SKYNET_H +#include "skynet_malloc.h" + #include #include diff --git a/skynet-src/skynet_env.c b/skynet-src/skynet_env.c index 0bc92e8c..c2778d3b 100644 --- a/skynet-src/skynet_env.c +++ b/skynet-src/skynet_env.c @@ -1,3 +1,5 @@ +// include skynet.h first for malloc hook +#include "skynet.h" #include "skynet_env.h" #include diff --git a/skynet-src/skynet_error.c b/skynet-src/skynet_error.c index 1cc3f98b..a3c7b807 100644 --- a/skynet-src/skynet_error.c +++ b/skynet-src/skynet_error.c @@ -29,7 +29,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap); va_end(ap); if (len < LOG_MESSAGE_SIZE) { - data = strdup(tmp); + data = skynet_strdup(tmp); } else { int max_size = LOG_MESSAGE_SIZE; for (;;) { diff --git a/skynet-src/skynet_handle.c b/skynet-src/skynet_handle.c index ee2381ed..baa6d200 100644 --- a/skynet-src/skynet_handle.c +++ b/skynet-src/skynet_handle.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_handle.h" #include "skynet_server.h" #include "rwlock.h" @@ -204,7 +207,7 @@ _insert_name(struct handle_storage *s, const char * name, uint32_t handle) { end = mid - 1; } } - char * result = strdup(name); + char * result = skynet_strdup(name); _insert_name_before(s, result, handle, begin); diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 7fbcf463..11ff87b2 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_imp.h" #include "skynet_env.h" diff --git a/skynet-src/skynet_malloc.h b/skynet-src/skynet_malloc.h new file mode 100644 index 00000000..d3c6400c --- /dev/null +++ b/skynet-src/skynet_malloc.h @@ -0,0 +1,18 @@ +#ifndef skynet_malloc_h +#define skynet_malloc_h + +#include + +#define malloc skynet_malloc +#define calloc skynet_calloc +#define realloc skynet_realloc +#define free skynet_free + +void * skynet_malloc(size_t sz); +void * skynet_calloc(size_t nmemb,size_t size); +void * skynet_realloc(void *ptr, size_t size); +void skynet_free(void *ptr); +char * skynet_strdup(const char *str); +void * skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize); // use for lua + +#endif diff --git a/skynet-src/skynet_module.c b/skynet-src/skynet_module.c index 3702cfc2..a39288a9 100644 --- a/skynet-src/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_module.h" #include @@ -103,7 +106,7 @@ skynet_module_query(const char * name) { M->m[index].module = dl; if (_open_sym(&M->m[index]) == 0) { - M->m[index].name = strdup(name); + M->m[index].name = skynet_strdup(name); M->count ++; result = &M->m[index]; } @@ -152,7 +155,7 @@ void skynet_module_init(const char *path) { struct modules *m = malloc(sizeof(*m)); m->count = 0; - m->path = strdup(path); + m->path = skynet_strdup(path); m->lock = 0; M = m; diff --git a/skynet-src/skynet_monitor.c b/skynet-src/skynet_monitor.c index 7d6c3cd6..e0cf9b49 100644 --- a/skynet-src/skynet_monitor.c +++ b/skynet-src/skynet_monitor.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_monitor.h" #include "skynet_server.h" #include "skynet.h" diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 9ca53d0f..16cbe3ec 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_server.h" #include "skynet_module.h" #include "skynet_handle.h" @@ -5,7 +8,6 @@ #include "skynet_timer.h" #include "skynet_harbor.h" #include "skynet_env.h" -#include "skynet.h" #include "skynet_monitor.h" #include diff --git a/skynet-src/skynet_socket.c b/skynet-src/skynet_socket.c index 7f6f1330..49e31ed6 100644 --- a/skynet-src/skynet_socket.c +++ b/skynet-src/skynet_socket.c @@ -1,9 +1,11 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_socket.h" #include "socket_server.h" #include "skynet_server.h" #include "skynet_mq.h" #include "skynet_harbor.h" -#include "skynet.h" #include #include diff --git a/skynet-src/skynet_timer.c b/skynet-src/skynet_timer.c index faa27ced..ab9d3829 100644 --- a/skynet-src/skynet_timer.c +++ b/skynet-src/skynet_timer.c @@ -1,8 +1,10 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "skynet_timer.h" #include "skynet_mq.h" #include "skynet_server.h" #include "skynet_handle.h" -#include "skynet.h" #include #include diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 539424f9..f898a4a3 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -1,3 +1,6 @@ +// include skynet.h first for malloc hook +#include "skynet.h" + #include "socket_server.h" #include "socket_poll.h"