Skip to content

Commit 61c8c76

Browse files
committed
use skynet_malloc api
1 parent 75a28b0 commit 61c8c76

File tree

7 files changed

+40
-44
lines changed

7 files changed

+40
-44
lines changed

lualib-src/lua-bson.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// include skynet.h first for malloc hook
2-
#include "skynet.h"
1+
#include "skynet_malloc.h"
32

43
#include <lua.h>
54
#include <lauxlib.h>
@@ -74,7 +73,7 @@ struct bson_reader {
7473
static inline void
7574
bson_destroy(struct bson *b) {
7675
if (b->ptr != b->buffer) {
77-
free(b->ptr);
76+
skynet_free(b->ptr);
7877
}
7978
}
8079

@@ -94,10 +93,10 @@ bson_reserve(struct bson *b, int sz) {
9493
} while (b->cap <= b->size + sz);
9594

9695
if (b->ptr == b->buffer) {
97-
b->ptr = malloc(b->cap);
96+
b->ptr = skynet_malloc(b->cap);
9897
memcpy(b->ptr, b->buffer, b->size);
9998
} else {
100-
b->ptr = realloc(b->ptr, b->cap);
99+
b->ptr = skynet_realloc(b->ptr, b->cap);
101100
}
102101
}
103102

lualib-src/lua-memory.c

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// include skynet.h first for malloc hook
2-
#include "skynet.h"
3-
41
#include <lua.h>
52
#include <lauxlib.h>
63

lualib-src/lua-mongo.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// include skynet.h first for malloc hook
2-
#include "skynet.h"
1+
#include "skynet_malloc.h"
32

43
#include <lua.h>
54
#include <lauxlib.h>
@@ -68,7 +67,7 @@ get_length(const document buffer) {
6867
static inline void
6968
buffer_destroy(struct buffer *b) {
7069
if (b->ptr != b->buffer) {
71-
free(b->ptr);
70+
skynet_free(b->ptr);
7271
}
7372
}
7473

@@ -88,10 +87,10 @@ buffer_reserve(struct buffer *b, int sz) {
8887
} while (b->cap <= b->size + sz);
8988

9089
if (b->ptr == b->buffer) {
91-
b->ptr = malloc(b->cap);
90+
b->ptr = skynet_malloc(b->cap);
9291
memcpy(b->ptr, b->buffer, b->size);
9392
} else {
94-
b->ptr = realloc(b->ptr, b->cap);
93+
b->ptr = skynet_realloc(b->ptr, b->cap);
9594
}
9695
}
9796

lualib-src/lua-netpack.c

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// include skynet.h first for malloc hook
2-
#include "skynet.h"
1+
#include "skynet_malloc.h"
32

43
#include "skynet_socket.h"
54

@@ -51,7 +50,7 @@ clear_list(struct uncomplete * uc) {
5150
while (uc) {
5251
void * tmp = uc;
5352
uc = uc->next;
54-
free(tmp);
53+
skynet_free(tmp);
5554
}
5655
}
5756

@@ -71,7 +70,7 @@ lclear(lua_State *L) {
7170
}
7271
for (i=q->head;i<q->tail;i++) {
7372
struct netpack *np = &q->queue[i % q->cap];
74-
free(np->buffer);
73+
skynet_free(np->buffer);
7574
}
7675
q->head = q->tail = 0;
7776

@@ -147,7 +146,7 @@ expand_queue(lua_State *L, struct queue *q) {
147146
static void
148147
push_data(lua_State *L, int fd, void *buffer, int size, int clone) {
149148
if (clone) {
150-
void * tmp = malloc(size);
149+
void * tmp = skynet_malloc(size);
151150
memcpy(tmp, buffer, size);
152151
buffer = tmp;
153152
}
@@ -167,7 +166,7 @@ static struct uncomplete *
167166
save_uncomplete(lua_State *L, int fd) {
168167
struct queue *q = get_queue(L);
169168
int h = hash_fd(fd);
170-
struct uncomplete * uc = malloc(sizeof(struct uncomplete));
169+
struct uncomplete * uc = skynet_malloc(sizeof(struct uncomplete));
171170
memset(uc, 0, sizeof(*uc));
172171
uc->next = q->hash[h];
173172
uc->pack.id = fd;
@@ -198,7 +197,7 @@ push_more(lua_State *L, int fd, uint8_t *buffer, int size) {
198197
struct uncomplete * uc = save_uncomplete(L, fd);
199198
uc->read = size;
200199
uc->pack.size = pack_size;
201-
uc->pack.buffer = malloc(pack_size);
200+
uc->pack.buffer = skynet_malloc(pack_size);
202201
memcpy(uc->pack.buffer, buffer, size);
203202
return;
204203
}
@@ -225,7 +224,7 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
225224
++buffer;
226225
--size;
227226
uc->pack.size = pack_size;
228-
uc->pack.buffer = malloc(pack_size);
227+
uc->pack.buffer = skynet_malloc(pack_size);
229228
uc->read = 0;
230229
}
231230
int need = uc->pack.size - uc->read;
@@ -245,12 +244,12 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
245244
lua_pushinteger(L, fd);
246245
lua_pushlightuserdata(L, uc->pack.buffer);
247246
lua_pushinteger(L, uc->pack.size);
248-
free(uc);
247+
skynet_free(uc);
249248
return 5;
250249
}
251250
// more data
252251
push_data(L, fd, uc->pack.buffer, uc->pack.size, 0);
253-
free(uc);
252+
skynet_free(uc);
254253
push_more(L, fd, buffer, size);
255254
lua_pushvalue(L, lua_upvalueindex(TYPE_MORE));
256255
return 2;
@@ -269,15 +268,15 @@ filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
269268
struct uncomplete * uc = save_uncomplete(L, fd);
270269
uc->read = size;
271270
uc->pack.size = pack_size;
272-
uc->pack.buffer = malloc(pack_size);
271+
uc->pack.buffer = skynet_malloc(pack_size);
273272
memcpy(uc->pack.buffer, buffer, size);
274273
return 1;
275274
}
276275
if (size == pack_size) {
277276
// just one package
278277
lua_pushvalue(L, lua_upvalueindex(TYPE_DATA));
279278
lua_pushinteger(L, fd);
280-
void * result = malloc(pack_size);
279+
void * result = skynet_malloc(pack_size);
281280
memcpy(result, buffer, size);
282281
lua_pushlightuserdata(L, result);
283282
lua_pushinteger(L, size);
@@ -298,7 +297,7 @@ filter_data(lua_State *L, int fd, uint8_t * buffer, int size) {
298297
int ret = filter_data_(L, fd, buffer, size);
299298
// buffer is the data of socket message, it malloc at socket_server.c : function forward_message .
300299
// it should be free before return,
301-
free(buffer);
300+
skynet_free(buffer);
302301
return ret;
303302
}
304303

@@ -419,7 +418,7 @@ lpack(lua_State *L) {
419418
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
420419
}
421420

422-
uint8_t * buffer = malloc(len + 2);
421+
uint8_t * buffer = skynet_malloc(len + 2);
423422
write_size(buffer, len);
424423
memcpy(buffer+2, ptr, len);
425424

@@ -460,7 +459,7 @@ ltostring(lua_State *L) {
460459
lua_pushliteral(L, "");
461460
} else {
462461
lua_pushlstring(L, (const char *)ptr, size);
463-
free(ptr);
462+
skynet_free(ptr);
464463
}
465464
return 1;
466465
}

lualib-src/lua-seri.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
https://github.com/cloudwu/lua-serialize
33
*/
44

5-
// include skynet.h first for malloc hook
6-
#include "skynet.h"
5+
#include "skynet_malloc.h"
76

87
#include <lua.h>
98
#include <lauxlib.h>
@@ -50,7 +49,7 @@ struct read_block {
5049

5150
inline static struct block *
5251
blk_alloc(void) {
53-
struct block *b = malloc(sizeof(struct block));
52+
struct block *b = skynet_malloc(sizeof(struct block));
5453
b->next = NULL;
5554
return b;
5655
}
@@ -113,7 +112,7 @@ wb_free(struct write_block *wb) {
113112
struct block *blk = wb->head;
114113
while (blk) {
115114
struct block * next = blk->next;
116-
free(blk);
115+
skynet_free(blk);
117116
blk = next;
118117
}
119118
wb->head = NULL;
@@ -145,7 +144,7 @@ rb_read(struct read_block *rb, void *buffer, int sz) {
145144

146145
if (rb->ptr == BLOCK_SIZE) {
147146
struct block * next = rb->current->next;
148-
free(rb->current);
147+
skynet_free(rb->current);
149148
rb->current = next;
150149
rb->ptr = 0;
151150
}
@@ -168,7 +167,7 @@ rb_read(struct read_block *rb, void *buffer, int sz) {
168167

169168
for (;;) {
170169
struct block * next = rb->current->next;
171-
free(rb->current);
170+
skynet_free(rb->current);
172171
rb->current = next;
173172

174173
if (sz < BLOCK_SIZE) {
@@ -188,7 +187,7 @@ static void
188187
rb_close(struct read_block *rb) {
189188
while (rb->current) {
190189
struct block * next = rb->current->next;
191-
free(rb->current);
190+
skynet_free(rb->current);
192191
rb->current = next;
193192
}
194193
rb->len = 0;
@@ -549,7 +548,7 @@ _seri(lua_State *L, struct block *b) {
549548
memcpy(&len, b->buffer ,sizeof(len));
550549

551550
len -= 4;
552-
uint8_t * buffer = malloc(len);
551+
uint8_t * buffer = skynet_malloc(len);
553552
uint8_t * ptr = buffer;
554553
int sz = len;
555554
if (len < BLOCK_SIZE - 4) {
@@ -622,7 +621,7 @@ _luaseri_pack(lua_State *L) {
622621

623622
while (b) {
624623
struct block * next = b->next;
625-
free(b);
624+
skynet_free(b);
626625
b = next;
627626
}
628627

lualib-src/lua-socket.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// include skynet.h first for malloc hook
2-
#include "skynet.h"
1+
#include "skynet_malloc.h"
32

43
#include <stdlib.h>
54
#include <string.h>
@@ -38,7 +37,7 @@ lfreepool(lua_State *L) {
3837
for (i=0;i<sz;i++) {
3938
struct buffer_node *node = &pool[i];
4039
if (node->msg) {
41-
free(node->msg);
40+
skynet_free(node->msg);
4241
node->msg = NULL;
4342
}
4443
}
@@ -143,7 +142,7 @@ return_free_node(lua_State *L, int pool, struct socket_buffer *sb) {
143142
lua_rawgeti(L,pool,1);
144143
free_node->next = lua_touserdata(L,-1);
145144
lua_pop(L,1);
146-
free(free_node->msg);
145+
skynet_free(free_node->msg);
147146
free_node->msg = NULL;
148147

149148
free_node->sz = 0;
@@ -253,7 +252,7 @@ static int
253252
ldrop(lua_State *L) {
254253
void * msg = lua_touserdata(L,1);
255254
luaL_checkinteger(L,2);
256-
free(msg);
255+
skynet_free(msg);
257256
return 0;
258257
}
259258

@@ -324,7 +323,7 @@ static int
324323
lstr2p(lua_State *L) {
325324
size_t sz = 0;
326325
const char * str = luaL_checklstring(L,1,&sz);
327-
void *ptr = malloc(sz);
326+
void *ptr = skynet_malloc(sz);
328327
memcpy(ptr, str, sz);
329328
lua_pushlightuserdata(L, ptr);
330329
lua_pushinteger(L, (int)sz);
@@ -414,7 +413,7 @@ get_buffer(lua_State *L, int *sz) {
414413
} else {
415414
size_t len = 0;
416415
const char * str = luaL_checklstring(L, 2, &len);
417-
buffer = malloc(len);
416+
buffer = skynet_malloc(len);
418417
memcpy(buffer, str, len);
419418
*sz = (int)len;
420419
}

skynet-src/skynet_malloc.h

+4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
#include <stddef.h>
55

6+
#ifdef SKYNET_MALLOC_RENAME
7+
68
#define malloc skynet_malloc
79
#define calloc skynet_calloc
810
#define realloc skynet_realloc
911
#define free skynet_free
1012

13+
#endif
14+
1115
void * skynet_malloc(size_t sz);
1216
void * skynet_calloc(size_t nmemb,size_t size);
1317
void * skynet_realloc(void *ptr, size_t size);

0 commit comments

Comments
 (0)