use skynet malloc api directly

This commit is contained in:
Cloud Wu
2014-04-22 12:26:08 +08:00
parent 08c511160f
commit 75a28b0fcc
19 changed files with 98 additions and 107 deletions

View File

@@ -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;
}
}