use macro for replace malloc api (see skynet_malloc.h)

This commit is contained in:
Cloud Wu
2014-04-18 22:35:47 +08:00
parent 23cfb11954
commit 7d835d222b
23 changed files with 125 additions and 68 deletions

View File

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

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include <lua.h>
#include <lauxlib.h>

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include <lua.h>
#include <lauxlib.h>

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include <lua.h>
#include <lauxlib.h>

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_socket.h"
#include <lua.h>

View File

@@ -2,6 +2,9 @@
https://github.com/cloudwu/lua-serialize
*/
// include skynet.h first for malloc hook
#include "skynet.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

View File

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

View File

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

View File

@@ -3,11 +3,11 @@
#include <assert.h>
#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);
}
}

View File

@@ -1,5 +1,6 @@
#ifndef __MALLOC_HOOK_H
#define __MALLOC_HOOK_H
#include <stdlib.h>
extern size_t malloc_used_memory(void);

View File

@@ -1,6 +1,8 @@
#ifndef SKYNET_H
#define SKYNET_H
#include "skynet_malloc.h"
#include <stddef.h>
#include <stdint.h>

View File

@@ -1,3 +1,5 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_env.h"
#include <lua.h>

View File

@@ -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 (;;) {

View File

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

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_imp.h"
#include "skynet_env.h"

View File

@@ -0,0 +1,18 @@
#ifndef skynet_malloc_h
#define skynet_malloc_h
#include <stddef.h>
#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

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_module.h"
#include <assert.h>
@@ -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;

View File

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

View File

@@ -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 <string.h>

View File

@@ -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 <assert.h>
#include <stdlib.h>

View File

@@ -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 <time.h>
#include <assert.h>

View File

@@ -1,3 +1,6 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "socket_server.h"
#include "socket_poll.h"