mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
use skynet malloc api directly
This commit is contained in:
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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++) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -36,7 +36,7 @@ struct gate {
|
||||
|
||||
struct gate *
|
||||
gate_create(void) {
|
||||
struct gate * g = malloc(sizeof(*g));
|
||||
struct gate * g = skynet_malloc(sizeof(*g));
|
||||
memset(g,0,sizeof(*g));
|
||||
g->listen_id = -1;
|
||||
return g;
|
||||
@@ -57,8 +57,8 @@ gate_release(struct gate *g) {
|
||||
}
|
||||
messagepool_free(&g->mp);
|
||||
hashid_clear(&g->hash);
|
||||
free(g->conn);
|
||||
free(g);
|
||||
skynet_free(g->conn);
|
||||
skynet_free(g);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -164,17 +164,17 @@ static void
|
||||
_forward(struct gate *g, struct connection * c, int size) {
|
||||
struct skynet_context * ctx = g->ctx;
|
||||
if (g->broker) {
|
||||
void * temp = malloc(size);
|
||||
void * temp = skynet_malloc(size);
|
||||
databuffer_read(&c->buffer,&g->mp,temp, size);
|
||||
skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, 0, temp, size);
|
||||
return;
|
||||
}
|
||||
if (c->agent) {
|
||||
void * temp = malloc(size);
|
||||
void * temp = skynet_malloc(size);
|
||||
databuffer_read(&c->buffer,&g->mp,temp, size);
|
||||
skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, 0 , temp, size);
|
||||
} else if (g->watchdog) {
|
||||
char * tmp = malloc(size + 32);
|
||||
char * tmp = skynet_malloc(size + 32);
|
||||
int n = snprintf(tmp,32,"%d data ",c->id);
|
||||
databuffer_read(&c->buffer,&g->mp,tmp+n,size);
|
||||
skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, 0, tmp, size + n);
|
||||
@@ -215,7 +215,7 @@ dispatch_socket_message(struct gate *g, const struct skynet_socket_message * mes
|
||||
} else {
|
||||
skynet_error(ctx, "Drop unknown connection %d message", message->id);
|
||||
skynet_socket_close(ctx, message->id);
|
||||
free(message->buffer);
|
||||
skynet_free(message->buffer);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -369,7 +369,7 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) {
|
||||
g->ctx = ctx;
|
||||
|
||||
hashid_init(&g->hash, max);
|
||||
g->conn = malloc(max * sizeof(struct connection));
|
||||
g->conn = skynet_malloc(max * sizeof(struct connection));
|
||||
memset(g->conn, 0, max *sizeof(struct connection));
|
||||
g->max_connection = max;
|
||||
int i;
|
||||
|
||||
@@ -66,12 +66,12 @@ _push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct rem
|
||||
// If there is only 1 free slot which is reserved to distinguish full/empty
|
||||
// of circular buffer, expand it.
|
||||
if (((queue->tail + 1) % queue->size) == queue->head) {
|
||||
struct msg * new_buffer = malloc(queue->size * 2 * sizeof(struct msg));
|
||||
struct msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct msg));
|
||||
int i;
|
||||
for (i=0;i<queue->size-1;i++) {
|
||||
new_buffer[i] = queue->data[(i+queue->head) % queue->size];
|
||||
}
|
||||
free(queue->data);
|
||||
skynet_free(queue->data);
|
||||
queue->data = new_buffer;
|
||||
queue->head = 0;
|
||||
queue->tail = queue->size - 1;
|
||||
@@ -80,7 +80,7 @@ _push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct rem
|
||||
struct msg * slot = &queue->data[queue->tail];
|
||||
queue->tail = (queue->tail + 1) % queue->size;
|
||||
|
||||
slot->buffer = malloc(sz + sizeof(*header));
|
||||
slot->buffer = skynet_malloc(sz + sizeof(*header));
|
||||
memcpy(slot->buffer, buffer, sz);
|
||||
memcpy(slot->buffer + sz, header, sizeof(*header));
|
||||
slot->size = sz + sizeof(*header);
|
||||
@@ -98,11 +98,11 @@ _pop_queue(struct msg_queue * queue) {
|
||||
|
||||
static struct msg_queue *
|
||||
_new_queue() {
|
||||
struct msg_queue * queue = malloc(sizeof(*queue));
|
||||
struct msg_queue * queue = skynet_malloc(sizeof(*queue));
|
||||
queue->size = DEFAULT_QUEUE_SIZE;
|
||||
queue->head = 0;
|
||||
queue->tail = 0;
|
||||
queue->data = malloc(DEFAULT_QUEUE_SIZE * sizeof(struct msg));
|
||||
queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct msg));
|
||||
|
||||
return queue;
|
||||
}
|
||||
@@ -113,11 +113,11 @@ _release_queue(struct msg_queue *queue) {
|
||||
return;
|
||||
struct msg * m = _pop_queue(queue);
|
||||
while (m) {
|
||||
free(m->buffer);
|
||||
skynet_free(m->buffer);
|
||||
m = _pop_queue(queue);
|
||||
}
|
||||
free(queue->data);
|
||||
free(queue);
|
||||
skynet_free(queue->data);
|
||||
skynet_free(queue);
|
||||
}
|
||||
|
||||
static struct keyvalue *
|
||||
@@ -148,7 +148,7 @@ _hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) {
|
||||
if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) {
|
||||
_release_queue(node->queue);
|
||||
*ptr->next = node->next;
|
||||
free(node);
|
||||
skynet_free(node);
|
||||
return;
|
||||
}
|
||||
*ptr = &(node->next);
|
||||
@@ -161,7 +161,7 @@ _hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
||||
uint32_t *ptr = (uint32_t *)name;
|
||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
||||
struct keyvalue ** pkv = &hash->node[h % HASH_SIZE];
|
||||
struct keyvalue * node = malloc(sizeof(*node));
|
||||
struct keyvalue * node = skynet_malloc(sizeof(*node));
|
||||
memcpy(node->key, name, GLOBALNAME_LENGTH);
|
||||
node->next = *pkv;
|
||||
node->queue = NULL;
|
||||
@@ -174,7 +174,7 @@ _hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
||||
|
||||
static struct hashmap *
|
||||
_hash_new() {
|
||||
struct hashmap * h = malloc(sizeof(struct hashmap));
|
||||
struct hashmap * h = skynet_malloc(sizeof(struct hashmap));
|
||||
memset(h,0,sizeof(*h));
|
||||
return h;
|
||||
}
|
||||
@@ -187,18 +187,18 @@ _hash_delete(struct hashmap *hash) {
|
||||
while (node) {
|
||||
struct keyvalue * next = node->next;
|
||||
_release_queue(node->queue);
|
||||
free(node);
|
||||
skynet_free(node);
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
free(hash);
|
||||
skynet_free(hash);
|
||||
}
|
||||
|
||||
///////////////
|
||||
|
||||
struct harbor *
|
||||
harbor_create(void) {
|
||||
struct harbor * h = malloc(sizeof(*h));
|
||||
struct harbor * h = skynet_malloc(sizeof(*h));
|
||||
h->ctx = NULL;
|
||||
h->id = 0;
|
||||
h->master_fd = -1;
|
||||
@@ -219,17 +219,17 @@ harbor_release(struct harbor *h) {
|
||||
if (h->master_fd >= 0) {
|
||||
skynet_socket_close(ctx, h->master_fd);
|
||||
}
|
||||
free(h->master_addr);
|
||||
free(h->local_addr);
|
||||
skynet_free(h->master_addr);
|
||||
skynet_free(h->local_addr);
|
||||
int i;
|
||||
for (i=0;i<REMOTE_MAX;i++) {
|
||||
if (h->remote_fd[i] >= 0) {
|
||||
skynet_socket_close(ctx, h->remote_fd[i]);
|
||||
free(h->remote_addr[i]);
|
||||
skynet_free(h->remote_addr[i]);
|
||||
}
|
||||
}
|
||||
_hash_delete(h->map);
|
||||
free(h);
|
||||
skynet_free(h);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -288,7 +288,7 @@ _message_to_header(const uint32_t *message, struct remote_message_header *header
|
||||
|
||||
static void
|
||||
_send_package(struct skynet_context *ctx, int fd, const void * buffer, size_t sz) {
|
||||
uint8_t * sendbuf = malloc(sz+4);
|
||||
uint8_t * sendbuf = skynet_malloc(sz+4);
|
||||
to_bigendian(sendbuf, sz);
|
||||
memcpy(sendbuf+4, buffer, sz);
|
||||
|
||||
@@ -300,7 +300,7 @@ _send_package(struct skynet_context *ctx, int fd, const void * buffer, size_t sz
|
||||
static void
|
||||
_send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) {
|
||||
uint32_t sz_header = sz+sizeof(*cookie);
|
||||
uint8_t * sendbuf = malloc(sz_header+4);
|
||||
uint8_t * sendbuf = skynet_malloc(sz_header+4);
|
||||
to_bigendian(sendbuf, sz_header);
|
||||
memcpy(sendbuf+4, buffer, sz);
|
||||
_header_to_message(cookie, sendbuf+4+sz);
|
||||
@@ -319,7 +319,7 @@ _update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) {
|
||||
struct skynet_context * context = h->ctx;
|
||||
if (h->remote_fd[harbor_id] >=0) {
|
||||
skynet_socket_close(context, h->remote_fd[harbor_id]);
|
||||
free(h->remote_addr[harbor_id]);
|
||||
skynet_free(h->remote_addr[harbor_id]);
|
||||
h->remote_addr[harbor_id] = NULL;
|
||||
}
|
||||
h->remote_fd[harbor_id] = _connect_to(h, ipaddr, false);
|
||||
@@ -484,7 +484,7 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin
|
||||
const struct skynet_socket_message * message = msg;
|
||||
switch(message->type) {
|
||||
case SKYNET_SOCKET_TYPE_DATA:
|
||||
free(message->buffer);
|
||||
skynet_free(message->buffer);
|
||||
skynet_error(context, "recv invalid socket message (size=%d)", message->ud);
|
||||
break;
|
||||
case SKYNET_SOCKET_TYPE_ACCEPT:
|
||||
@@ -552,7 +552,7 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
free((void *)rmsg->message);
|
||||
skynet_free((void *)rmsg->message);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ struct logger {
|
||||
|
||||
struct logger *
|
||||
logger_create(void) {
|
||||
struct logger * inst = malloc(sizeof(*inst));
|
||||
struct logger * inst = skynet_malloc(sizeof(*inst));
|
||||
inst->handle = NULL;
|
||||
inst->close = 0;
|
||||
return inst;
|
||||
@@ -22,7 +22,7 @@ logger_release(struct logger * inst) {
|
||||
if (inst->close) {
|
||||
fclose(inst->handle);
|
||||
}
|
||||
free(inst);
|
||||
skynet_free(inst);
|
||||
}
|
||||
|
||||
static int
|
||||
|
||||
@@ -32,7 +32,7 @@ struct master {
|
||||
|
||||
struct master *
|
||||
master_create() {
|
||||
struct master *m = malloc(sizeof(*m));
|
||||
struct master *m = skynet_malloc(sizeof(*m));
|
||||
int i;
|
||||
for (i=0;i<REMOTE_MAX;i++) {
|
||||
m->remote_fd[i] = -1;
|
||||
@@ -53,17 +53,17 @@ master_release(struct master * m) {
|
||||
assert(ctx);
|
||||
skynet_socket_close(ctx, fd);
|
||||
}
|
||||
free(m->remote_addr[i]);
|
||||
skynet_free(m->remote_addr[i]);
|
||||
}
|
||||
for (i=0;i<HASH_SIZE;i++) {
|
||||
struct name * node = m->map.node[i];
|
||||
while (node) {
|
||||
struct name * next = node->next;
|
||||
free(node);
|
||||
skynet_free(node);
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
free(m);
|
||||
skynet_free(m);
|
||||
}
|
||||
|
||||
static struct name *
|
||||
@@ -85,7 +85,7 @@ _insert_name(struct master *m, char name[GLOBALNAME_LENGTH]) {
|
||||
uint32_t *ptr = (uint32_t *)name;
|
||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
||||
struct name **pname = &m->map.node[h % HASH_SIZE];
|
||||
struct name * node = malloc(sizeof(*node));
|
||||
struct name * node = skynet_malloc(sizeof(*node));
|
||||
memcpy(node->key, name, GLOBALNAME_LENGTH);
|
||||
node->next = *pname;
|
||||
node->hash = h;
|
||||
@@ -133,7 +133,7 @@ to_bigendian(uint8_t *buffer, uint32_t n) {
|
||||
|
||||
static void
|
||||
_send_to(struct master *m, int id, const void * buf, int sz, uint32_t handle) {
|
||||
uint8_t * buffer= (uint8_t *)malloc(4 + sz + 12);
|
||||
uint8_t * buffer= (uint8_t *)skynet_malloc(4 + sz + 12);
|
||||
to_bigendian(buffer, sz+12);
|
||||
memcpy(buffer+4, buf, sz);
|
||||
to_bigendian(buffer+4+sz, 0);
|
||||
@@ -196,8 +196,8 @@ _update_address(struct master *m, int harbor_id, const char * buffer, size_t sz)
|
||||
if (m->remote_fd[harbor_id] >= 0) {
|
||||
close_harbor(m, harbor_id);
|
||||
}
|
||||
free(m->remote_addr[harbor_id]);
|
||||
char * addr = malloc(sz+1);
|
||||
skynet_free(m->remote_addr[harbor_id]);
|
||||
char * addr = skynet_malloc(sz+1);
|
||||
memcpy(addr, buffer, sz);
|
||||
addr[sz] = '\0';
|
||||
m->remote_addr[harbor_id] = addr;
|
||||
|
||||
@@ -253,7 +253,7 @@ _launch(struct skynet_context * context, void *ud, int type, int session, uint32
|
||||
int
|
||||
snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
||||
int sz = strlen(args);
|
||||
char * tmp = malloc(sz+1);
|
||||
char * tmp = skynet_malloc(sz+1);
|
||||
memcpy(tmp, args, sz+1);
|
||||
skynet_callback(ctx, l , _launch);
|
||||
const char * self = skynet_command(ctx, "REG", NULL);
|
||||
@@ -265,7 +265,7 @@ snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
||||
|
||||
struct snlua *
|
||||
snlua_create(void) {
|
||||
struct snlua * l = malloc(sizeof(*l));
|
||||
struct snlua * l = skynet_malloc(sizeof(*l));
|
||||
memset(l,0,sizeof(*l));
|
||||
l->L = lua_newstate(skynet_lalloc, NULL);
|
||||
l->init = _init;
|
||||
@@ -275,5 +275,5 @@ snlua_create(void) {
|
||||
void
|
||||
snlua_release(struct snlua *l) {
|
||||
lua_close(l->L);
|
||||
free(l);
|
||||
skynet_free(l);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user