mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
use macro for replace malloc api (see skynet_malloc.h)
This commit is contained in:
7
Makefile
7
Makefile
@@ -20,7 +20,6 @@ $(LUA_STATICLIB) :
|
|||||||
|
|
||||||
JEMALLOC_STATICLIB := 3rd/jemalloc/lib/libjemalloc_pic.a
|
JEMALLOC_STATICLIB := 3rd/jemalloc/lib/libjemalloc_pic.a
|
||||||
JEMALLOC_INC := 3rd/jemalloc/include/jemalloc
|
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
|
all : jemalloc
|
||||||
.PHONY : jemalloc
|
.PHONY : jemalloc
|
||||||
@@ -52,7 +51,7 @@ all : \
|
|||||||
$(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so)
|
$(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so)
|
||||||
|
|
||||||
$(SKYNET_BUILD_PATH)/skynet : $(foreach v, $(SKYNET_SRC), skynet-src/$(v)) $(LUA_LIB) $(JEMALLOC_STATICLIB)
|
$(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) :
|
$(LUA_CLIB_PATH) :
|
||||||
mkdir $(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 $@
|
$(CC) $(CFLAGS) $(SHARED) $^ -o $@
|
||||||
|
|
||||||
$(LUA_CLIB_PATH)/bson.so : lualib-src/lua-bson.c | $(LUA_CLIB_PATH)
|
$(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)
|
$(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)
|
$(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 $@
|
$(CC) $(CFLAGS) $(SHARED) -I3rd/lua-md5 $^ -o $@
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_socket.h"
|
#include "skynet_socket.h"
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
https://github.com/cloudwu/lua-serialize
|
https://github.com/cloudwu/lua-serialize
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|||||||
@@ -586,14 +586,14 @@ harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) {
|
|||||||
char local_addr[sz];
|
char local_addr[sz];
|
||||||
int harbor_id = 0;
|
int harbor_id = 0;
|
||||||
sscanf(args,"%s %s %d",master_addr, local_addr, &harbor_id);
|
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->id = harbor_id;
|
||||||
h->master_fd = _connect_to(h, master_addr, true);
|
h->master_fd = _connect_to(h, master_addr, true);
|
||||||
if (h->master_fd == -1) {
|
if (h->master_fd == -1) {
|
||||||
fprintf(stderr, "Harbor: Connect to master failed\n");
|
fprintf(stderr, "Harbor: Connect to master failed\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
h->local_addr = strdup(local_addr);
|
h->local_addr = skynet_strdup(local_addr);
|
||||||
|
|
||||||
_launch_gate(ctx, local_addr);
|
_launch_gate(ctx, local_addr);
|
||||||
skynet_callback(ctx, h, _mainloop);
|
skynet_callback(ctx, h, _mainloop);
|
||||||
|
|||||||
@@ -267,7 +267,7 @@ struct snlua *
|
|||||||
snlua_create(void) {
|
snlua_create(void) {
|
||||||
struct snlua * l = malloc(sizeof(*l));
|
struct snlua * l = malloc(sizeof(*l));
|
||||||
memset(l,0,sizeof(*l));
|
memset(l,0,sizeof(*l));
|
||||||
l->L = luaL_newstate();
|
l->L = lua_newstate(skynet_lalloc, NULL);
|
||||||
l->init = _init;
|
l->init = _init;
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "malloc_hook.h"
|
#include "malloc_hook.h"
|
||||||
|
|
||||||
#include "jemalloc.h"
|
#include "jemalloc.h"
|
||||||
|
|
||||||
#include "skynet.h"
|
#include "skynet.h"
|
||||||
|
|
||||||
|
|
||||||
static size_t _used_memory = 0;
|
static size_t _used_memory = 0;
|
||||||
static size_t _memory_block = 0;
|
static size_t _memory_block = 0;
|
||||||
typedef struct _mem_data {
|
typedef struct _mem_data {
|
||||||
@@ -20,10 +20,6 @@ typedef struct _mem_data {
|
|||||||
|
|
||||||
static mem_data mem_stats[SLOT_SIZE];
|
static mem_data mem_stats[SLOT_SIZE];
|
||||||
|
|
||||||
static void _init() {
|
|
||||||
memset(mem_stats, 0, sizeof(mem_stats));
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t*
|
static size_t*
|
||||||
get_allocated_field(handle) {
|
get_allocated_field(handle) {
|
||||||
int h = (handle & SLOT_SIZE) - 1;
|
int h = (handle & SLOT_SIZE) - 1;
|
||||||
@@ -88,50 +84,6 @@ static void malloc_oom(size_t size) {
|
|||||||
abort();
|
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
|
size_t
|
||||||
malloc_used_memory(void) {
|
malloc_used_memory(void) {
|
||||||
return _used_memory;
|
return _used_memory;
|
||||||
@@ -190,10 +142,54 @@ dump_c_mem() {
|
|||||||
printf("+total: %zdkb\n",total >> 10);
|
printf("+total: %zdkb\n",total >> 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// hook : malloc, realloc, memalign, free, calloc
|
||||||
* init
|
|
||||||
*/
|
void *
|
||||||
void __attribute__ ((constructor)) malloc_hook_init(void){
|
skynet_malloc(size_t size) {
|
||||||
_init();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#ifndef __MALLOC_HOOK_H
|
#ifndef __MALLOC_HOOK_H
|
||||||
#define __MALLOC_HOOK_H
|
#define __MALLOC_HOOK_H
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
extern size_t malloc_used_memory(void);
|
extern size_t malloc_used_memory(void);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef SKYNET_H
|
#ifndef SKYNET_H
|
||||||
#define SKYNET_H
|
#define SKYNET_H
|
||||||
|
|
||||||
|
#include "skynet_malloc.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
#include "skynet_env.h"
|
#include "skynet_env.h"
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) {
|
|||||||
int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap);
|
int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (len < LOG_MESSAGE_SIZE) {
|
if (len < LOG_MESSAGE_SIZE) {
|
||||||
data = strdup(tmp);
|
data = skynet_strdup(tmp);
|
||||||
} else {
|
} else {
|
||||||
int max_size = LOG_MESSAGE_SIZE;
|
int max_size = LOG_MESSAGE_SIZE;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_handle.h"
|
#include "skynet_handle.h"
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
#include "rwlock.h"
|
#include "rwlock.h"
|
||||||
@@ -204,7 +207,7 @@ _insert_name(struct handle_storage *s, const char * name, uint32_t handle) {
|
|||||||
end = mid - 1;
|
end = mid - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char * result = strdup(name);
|
char * result = skynet_strdup(name);
|
||||||
|
|
||||||
_insert_name_before(s, result, handle, begin);
|
_insert_name_before(s, result, handle, begin);
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_imp.h"
|
#include "skynet_imp.h"
|
||||||
#include "skynet_env.h"
|
#include "skynet_env.h"
|
||||||
|
|
||||||
|
|||||||
18
skynet-src/skynet_malloc.h
Normal file
18
skynet-src/skynet_malloc.h
Normal 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
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_module.h"
|
#include "skynet_module.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -103,7 +106,7 @@ skynet_module_query(const char * name) {
|
|||||||
M->m[index].module = dl;
|
M->m[index].module = dl;
|
||||||
|
|
||||||
if (_open_sym(&M->m[index]) == 0) {
|
if (_open_sym(&M->m[index]) == 0) {
|
||||||
M->m[index].name = strdup(name);
|
M->m[index].name = skynet_strdup(name);
|
||||||
M->count ++;
|
M->count ++;
|
||||||
result = &M->m[index];
|
result = &M->m[index];
|
||||||
}
|
}
|
||||||
@@ -152,7 +155,7 @@ void
|
|||||||
skynet_module_init(const char *path) {
|
skynet_module_init(const char *path) {
|
||||||
struct modules *m = malloc(sizeof(*m));
|
struct modules *m = malloc(sizeof(*m));
|
||||||
m->count = 0;
|
m->count = 0;
|
||||||
m->path = strdup(path);
|
m->path = skynet_strdup(path);
|
||||||
m->lock = 0;
|
m->lock = 0;
|
||||||
|
|
||||||
M = m;
|
M = m;
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_monitor.h"
|
#include "skynet_monitor.h"
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
#include "skynet.h"
|
#include "skynet.h"
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
#include "skynet_module.h"
|
#include "skynet_module.h"
|
||||||
#include "skynet_handle.h"
|
#include "skynet_handle.h"
|
||||||
@@ -5,7 +8,6 @@
|
|||||||
#include "skynet_timer.h"
|
#include "skynet_timer.h"
|
||||||
#include "skynet_harbor.h"
|
#include "skynet_harbor.h"
|
||||||
#include "skynet_env.h"
|
#include "skynet_env.h"
|
||||||
#include "skynet.h"
|
|
||||||
#include "skynet_monitor.h"
|
#include "skynet_monitor.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_socket.h"
|
#include "skynet_socket.h"
|
||||||
#include "socket_server.h"
|
#include "socket_server.h"
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
#include "skynet_mq.h"
|
#include "skynet_mq.h"
|
||||||
#include "skynet_harbor.h"
|
#include "skynet_harbor.h"
|
||||||
#include "skynet.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_timer.h"
|
#include "skynet_timer.h"
|
||||||
#include "skynet_mq.h"
|
#include "skynet_mq.h"
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
#include "skynet_handle.h"
|
#include "skynet_handle.h"
|
||||||
#include "skynet.h"
|
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// include skynet.h first for malloc hook
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "socket_server.h"
|
#include "socket_server.h"
|
||||||
#include "socket_poll.h"
|
#include "socket_poll.h"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user