Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cloudwu/skynet
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Apr 22, 2014
2 parents b6c208c + f91a2a0 commit 387b950
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 153 deletions.
9 changes: 4 additions & 5 deletions lualib-src/lua-bson.c
@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"

#include <lua.h>
#include <lauxlib.h>
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
3 changes: 0 additions & 3 deletions lualib-src/lua-memory.c
@@ -1,6 +1,3 @@
// include skynet.h first for malloc hook
#include "skynet.h"

#include <lua.h>
#include <lauxlib.h>

Expand Down
9 changes: 4 additions & 5 deletions lualib-src/lua-mongo.c
@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"

#include <lua.h>
#include <lauxlib.h>
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
29 changes: 14 additions & 15 deletions lualib-src/lua-netpack.c
@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"

#include "skynet_socket.h"

Expand Down Expand Up @@ -51,7 +50,7 @@ clear_list(struct uncomplete * uc) {
while (uc) {
void * tmp = uc;
uc = uc->next;
free(tmp);
skynet_free(tmp);
}
}

Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -269,15 +268,15 @@ 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;
}
if (size == pack_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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 8 additions & 9 deletions lualib-src/lua-seri.c
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -622,7 +621,7 @@ _luaseri_pack(lua_State *L) {

while (b) {
struct block * next = b->next;
free(b);
skynet_free(b);
b = next;
}

Expand Down
13 changes: 6 additions & 7 deletions lualib-src/lua-socket.c
@@ -1,5 +1,4 @@
// include skynet.h first for malloc hook
#include "skynet.h"
#include "skynet_malloc.h"

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions service-src/databuffer.h
Expand Up @@ -39,7 +39,7 @@ messagepool_free(struct messagepool *pool) {
while(p) {
struct messagepool_list *tmp = p;
p=p->next;
free(tmp);
skynet_free(tmp);
}
pool->pool = NULL;
pool->freelist = NULL;
Expand All @@ -54,7 +54,7 @@ _return_message(struct databuffer *db, struct messagepool *mp) {
} else {
db->head = m->next;
}
free(m->buffer);
skynet_free(m->buffer);
m->buffer = NULL;
m->size = 0;
m->next = mp->freelist;
Expand Down Expand Up @@ -95,7 +95,7 @@ databuffer_push(struct databuffer *db, struct messagepool *mp, void *data, int s
m = mp->freelist;
mp->freelist = m->next;
} else {
struct messagepool_list * mpl = malloc(sizeof(*mpl));
struct messagepool_list * mpl = skynet_malloc(sizeof(*mpl));
struct message * temp = mpl->pool;
int i;
for (i=1;i<MESSAGEPOOL;i++) {
Expand Down
8 changes: 4 additions & 4 deletions service-src/hashid.h
Expand Up @@ -29,19 +29,19 @@ hashid_init(struct hashid *hi, int max) {
hi->hashmod = hashcap - 1;
hi->cap = max;
hi->count = 0;
hi->id = malloc(max * sizeof(struct hashid_node));
hi->id = skynet_malloc(max * sizeof(struct hashid_node));
for (i=0;i<max;i++) {
hi->id[i].id = -1;
hi->id[i].next = NULL;
}
hi->hash = malloc(hashcap * sizeof(struct hashid_node *));
hi->hash = skynet_malloc(hashcap * sizeof(struct hashid_node *));
memset(hi->hash, 0, hashcap * sizeof(struct hashid_node *));
}

static void
hashid_clear(struct hashid *hi) {
free(hi->id);
free(hi->hash);
skynet_free(hi->id);
skynet_free(hi->hash);
hi->id = NULL;
hi->hash = NULL;
hi->hashmod = 1;
Expand Down

0 comments on commit 387b950

Please sign in to comment.