use skynet_malloc api

This commit is contained in:
Cloud Wu
2014-04-22 15:17:16 +08:00
parent 75a28b0fcc
commit 61c8c765f4
7 changed files with 40 additions and 44 deletions

View File

@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"
#include <lua.h>
#include <lauxlib.h>
@@ -74,7 +73,7 @@ struct bson_reader {
static inline void
bson_destroy(struct bson *b) {
if (b->ptr != b->buffer) {
free(b->ptr);
skynet_free(b->ptr);
}
}
@@ -94,10 +93,10 @@ bson_reserve(struct bson *b, int sz) {
} while (b->cap <= b->size + sz);
if (b->ptr == b->buffer) {
b->ptr = malloc(b->cap);
b->ptr = skynet_malloc(b->cap);
memcpy(b->ptr, b->buffer, b->size);
} else {
b->ptr = realloc(b->ptr, b->cap);
b->ptr = skynet_realloc(b->ptr, b->cap);
}
}

View File

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

View File

@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"
#include <lua.h>
#include <lauxlib.h>
@@ -68,7 +67,7 @@ get_length(const document buffer) {
static inline void
buffer_destroy(struct buffer *b) {
if (b->ptr != b->buffer) {
free(b->ptr);
skynet_free(b->ptr);
}
}
@@ -88,10 +87,10 @@ buffer_reserve(struct buffer *b, int sz) {
} while (b->cap <= b->size + sz);
if (b->ptr == b->buffer) {
b->ptr = malloc(b->cap);
b->ptr = skynet_malloc(b->cap);
memcpy(b->ptr, b->buffer, b->size);
} else {
b->ptr = realloc(b->ptr, b->cap);
b->ptr = skynet_realloc(b->ptr, b->cap);
}
}

View File

@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"
#include "skynet_socket.h"
@@ -51,7 +50,7 @@ clear_list(struct uncomplete * uc) {
while (uc) {
void * tmp = uc;
uc = uc->next;
free(tmp);
skynet_free(tmp);
}
}
@@ -71,7 +70,7 @@ lclear(lua_State *L) {
}
for (i=q->head;i<q->tail;i++) {
struct netpack *np = &q->queue[i % q->cap];
free(np->buffer);
skynet_free(np->buffer);
}
q->head = q->tail = 0;
@@ -147,7 +146,7 @@ expand_queue(lua_State *L, struct queue *q) {
static void
push_data(lua_State *L, int fd, void *buffer, int size, int clone) {
if (clone) {
void * tmp = malloc(size);
void * tmp = skynet_malloc(size);
memcpy(tmp, buffer, size);
buffer = tmp;
}
@@ -167,7 +166,7 @@ static struct uncomplete *
save_uncomplete(lua_State *L, int fd) {
struct queue *q = get_queue(L);
int h = hash_fd(fd);
struct uncomplete * uc = malloc(sizeof(struct uncomplete));
struct uncomplete * uc = skynet_malloc(sizeof(struct uncomplete));
memset(uc, 0, sizeof(*uc));
uc->next = q->hash[h];
uc->pack.id = fd;
@@ -198,7 +197,7 @@ push_more(lua_State *L, int fd, uint8_t *buffer, int size) {
struct uncomplete * uc = save_uncomplete(L, fd);
uc->read = size;
uc->pack.size = pack_size;
uc->pack.buffer = malloc(pack_size);
uc->pack.buffer = skynet_malloc(pack_size);
memcpy(uc->pack.buffer, buffer, size);
return;
}
@@ -225,7 +224,7 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
++buffer;
--size;
uc->pack.size = pack_size;
uc->pack.buffer = malloc(pack_size);
uc->pack.buffer = skynet_malloc(pack_size);
uc->read = 0;
}
int need = uc->pack.size - uc->read;
@@ -245,12 +244,12 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
lua_pushinteger(L, fd);
lua_pushlightuserdata(L, uc->pack.buffer);
lua_pushinteger(L, uc->pack.size);
free(uc);
skynet_free(uc);
return 5;
}
// more data
push_data(L, fd, uc->pack.buffer, uc->pack.size, 0);
free(uc);
skynet_free(uc);
push_more(L, fd, buffer, size);
lua_pushvalue(L, lua_upvalueindex(TYPE_MORE));
return 2;
@@ -269,7 +268,7 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
struct uncomplete * uc = save_uncomplete(L, fd);
uc->read = size;
uc->pack.size = pack_size;
uc->pack.buffer = malloc(pack_size);
uc->pack.buffer = skynet_malloc(pack_size);
memcpy(uc->pack.buffer, buffer, size);
return 1;
}
@@ -277,7 +276,7 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
// just one package
lua_pushvalue(L, lua_upvalueindex(TYPE_DATA));
lua_pushinteger(L, fd);
void * result = malloc(pack_size);
void * result = skynet_malloc(pack_size);
memcpy(result, buffer, size);
lua_pushlightuserdata(L, result);
lua_pushinteger(L, size);
@@ -298,7 +297,7 @@ filter_data(lua_State *L, int fd, uint8_t * buffer, int size) {
int ret = filter_data_(L, fd, buffer, size);
// buffer is the data of socket message, it malloc at socket_server.c : function forward_message .
// it should be free before return,
free(buffer);
skynet_free(buffer);
return ret;
}
@@ -419,7 +418,7 @@ lpack(lua_State *L) {
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
}
uint8_t * buffer = malloc(len + 2);
uint8_t * buffer = skynet_malloc(len + 2);
write_size(buffer, len);
memcpy(buffer+2, ptr, len);
@@ -460,7 +459,7 @@ ltostring(lua_State *L) {
lua_pushliteral(L, "");
} else {
lua_pushlstring(L, (const char *)ptr, size);
free(ptr);
skynet_free(ptr);
}
return 1;
}

View File

@@ -2,8 +2,7 @@
https://github.com/cloudwu/lua-serialize
*/
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"
#include <lua.h>
#include <lauxlib.h>
@@ -50,7 +49,7 @@ struct read_block {
inline static struct block *
blk_alloc(void) {
struct block *b = malloc(sizeof(struct block));
struct block *b = skynet_malloc(sizeof(struct block));
b->next = NULL;
return b;
}
@@ -113,7 +112,7 @@ wb_free(struct write_block *wb) {
struct block *blk = wb->head;
while (blk) {
struct block * next = blk->next;
free(blk);
skynet_free(blk);
blk = next;
}
wb->head = NULL;
@@ -145,7 +144,7 @@ rb_read(struct read_block *rb, void *buffer, int sz) {
if (rb->ptr == BLOCK_SIZE) {
struct block * next = rb->current->next;
free(rb->current);
skynet_free(rb->current);
rb->current = next;
rb->ptr = 0;
}
@@ -168,7 +167,7 @@ rb_read(struct read_block *rb, void *buffer, int sz) {
for (;;) {
struct block * next = rb->current->next;
free(rb->current);
skynet_free(rb->current);
rb->current = next;
if (sz < BLOCK_SIZE) {
@@ -188,7 +187,7 @@ static void
rb_close(struct read_block *rb) {
while (rb->current) {
struct block * next = rb->current->next;
free(rb->current);
skynet_free(rb->current);
rb->current = next;
}
rb->len = 0;
@@ -549,7 +548,7 @@ _seri(lua_State *L, struct block *b) {
memcpy(&len, b->buffer ,sizeof(len));
len -= 4;
uint8_t * buffer = malloc(len);
uint8_t * buffer = skynet_malloc(len);
uint8_t * ptr = buffer;
int sz = len;
if (len < BLOCK_SIZE - 4) {
@@ -622,7 +621,7 @@ _luaseri_pack(lua_State *L) {
while (b) {
struct block * next = b->next;
free(b);
skynet_free(b);
b = next;
}

View File

@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"
#include <stdlib.h>
#include <string.h>
@@ -38,7 +37,7 @@ lfreepool(lua_State *L) {
for (i=0;i<sz;i++) {
struct buffer_node *node = &pool[i];
if (node->msg) {
free(node->msg);
skynet_free(node->msg);
node->msg = NULL;
}
}
@@ -143,7 +142,7 @@ return_free_node(lua_State *L, int pool, struct socket_buffer *sb) {
lua_rawgeti(L,pool,1);
free_node->next = lua_touserdata(L,-1);
lua_pop(L,1);
free(free_node->msg);
skynet_free(free_node->msg);
free_node->msg = NULL;
free_node->sz = 0;
@@ -253,7 +252,7 @@ static int
ldrop(lua_State *L) {
void * msg = lua_touserdata(L,1);
luaL_checkinteger(L,2);
free(msg);
skynet_free(msg);
return 0;
}
@@ -324,7 +323,7 @@ static int
lstr2p(lua_State *L) {
size_t sz = 0;
const char * str = luaL_checklstring(L,1,&sz);
void *ptr = malloc(sz);
void *ptr = skynet_malloc(sz);
memcpy(ptr, str, sz);
lua_pushlightuserdata(L, ptr);
lua_pushinteger(L, (int)sz);
@@ -414,7 +413,7 @@ get_buffer(lua_State *L, int *sz) {
} else {
size_t len = 0;
const char * str = luaL_checklstring(L, 2, &len);
buffer = malloc(len);
buffer = skynet_malloc(len);
memcpy(buffer, str, len);
*sz = (int)len;
}

View File

@@ -3,11 +3,15 @@
#include <stddef.h>
#ifdef SKYNET_MALLOC_RENAME
#define malloc skynet_malloc
#define calloc skynet_calloc
#define realloc skynet_realloc
#define free skynet_free
#endif
void * skynet_malloc(size_t sz);
void * skynet_calloc(size_t nmemb,size_t size);
void * skynet_realloc(void *ptr, size_t size);