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:
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
#include "skynet_env.h"
|
||||
|
||||
@@ -49,7 +48,7 @@ skynet_setenv(const char *key, const char *value) {
|
||||
|
||||
void
|
||||
skynet_env_init() {
|
||||
E = malloc(sizeof(*E));
|
||||
E = skynet_malloc(sizeof(*E));
|
||||
E->lock = 0;
|
||||
E->L = luaL_newstate();
|
||||
}
|
||||
|
||||
@@ -34,14 +34,14 @@ skynet_error(struct skynet_context * context, const char *msg, ...) {
|
||||
int max_size = LOG_MESSAGE_SIZE;
|
||||
for (;;) {
|
||||
max_size *= 2;
|
||||
data = malloc(max_size);
|
||||
data = skynet_malloc(max_size);
|
||||
va_start(ap,msg);
|
||||
len = vsnprintf(data, max_size, msg, ap);
|
||||
va_end(ap);
|
||||
if (len < max_size) {
|
||||
break;
|
||||
}
|
||||
free(data);
|
||||
skynet_free(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_handle.h"
|
||||
@@ -54,14 +53,14 @@ skynet_handle_register(struct skynet_context *ctx) {
|
||||
}
|
||||
}
|
||||
assert((s->slot_size*2 - 1) <= HANDLE_MASK);
|
||||
struct skynet_context ** new_slot = malloc(s->slot_size * 2 * sizeof(struct skynet_context *));
|
||||
struct skynet_context ** new_slot = skynet_malloc(s->slot_size * 2 * sizeof(struct skynet_context *));
|
||||
memset(new_slot, 0, s->slot_size * 2 * sizeof(struct skynet_context *));
|
||||
for (i=0;i<s->slot_size;i++) {
|
||||
int hash = skynet_context_handle(s->slot[i]) & (s->slot_size * 2 - 1);
|
||||
assert(new_slot[hash] == NULL);
|
||||
new_slot[hash] = s->slot[i];
|
||||
}
|
||||
free(s->slot);
|
||||
skynet_free(s->slot);
|
||||
s->slot = new_slot;
|
||||
s->slot_size *= 2;
|
||||
}
|
||||
@@ -83,7 +82,7 @@ skynet_handle_retire(uint32_t handle) {
|
||||
int j=0, n=s->name_count;
|
||||
for (i=0; i<n; ++i) {
|
||||
if (s->name[i].handle == handle) {
|
||||
free(s->name[i].name);
|
||||
skynet_free(s->name[i].name);
|
||||
continue;
|
||||
} else if (i!=j) {
|
||||
s->name[j] = s->name[i];
|
||||
@@ -169,7 +168,7 @@ static void
|
||||
_insert_name_before(struct handle_storage *s, char *name, uint32_t handle, int before) {
|
||||
if (s->name_count >= s->name_cap) {
|
||||
s->name_cap *= 2;
|
||||
struct handle_name * n = malloc(s->name_cap * sizeof(struct handle_name));
|
||||
struct handle_name * n = skynet_malloc(s->name_cap * sizeof(struct handle_name));
|
||||
int i;
|
||||
for (i=0;i<before;i++) {
|
||||
n[i] = s->name[i];
|
||||
@@ -177,7 +176,7 @@ _insert_name_before(struct handle_storage *s, char *name, uint32_t handle, int b
|
||||
for (i=before;i<s->name_count;i++) {
|
||||
n[i+1] = s->name[i];
|
||||
}
|
||||
free(s->name);
|
||||
skynet_free(s->name);
|
||||
s->name = n;
|
||||
} else {
|
||||
int i;
|
||||
@@ -228,9 +227,9 @@ skynet_handle_namehandle(uint32_t handle, const char *name) {
|
||||
void
|
||||
skynet_handle_init(int harbor) {
|
||||
assert(H==NULL);
|
||||
struct handle_storage * s = malloc(sizeof(*H));
|
||||
struct handle_storage * s = skynet_malloc(sizeof(*H));
|
||||
s->slot_size = DEFAULT_SLOT_SIZE;
|
||||
s->slot = malloc(s->slot_size * sizeof(struct skynet_context *));
|
||||
s->slot = skynet_malloc(s->slot_size * sizeof(struct skynet_context *));
|
||||
memset(s->slot, 0, s->slot_size * sizeof(struct skynet_context *));
|
||||
|
||||
rwlock_init(&s->lock);
|
||||
@@ -239,7 +238,7 @@ skynet_handle_init(int harbor) {
|
||||
s->handle_index = 1;
|
||||
s->name_cap = 2;
|
||||
s->name_count = 0;
|
||||
s->name = malloc(s->name_cap * sizeof(struct handle_name));
|
||||
s->name = skynet_malloc(s->name_cap * sizeof(struct handle_name));
|
||||
|
||||
H = s;
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_imp.h"
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_module.h"
|
||||
@@ -153,7 +152,7 @@ skynet_module_instance_release(struct skynet_module *m, void *inst) {
|
||||
|
||||
void
|
||||
skynet_module_init(const char *path) {
|
||||
struct modules *m = malloc(sizeof(*m));
|
||||
struct modules *m = skynet_malloc(sizeof(*m));
|
||||
m->count = 0;
|
||||
m->path = skynet_strdup(path);
|
||||
m->lock = 0;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_monitor.h"
|
||||
@@ -17,14 +16,14 @@ struct skynet_monitor {
|
||||
|
||||
struct skynet_monitor *
|
||||
skynet_monitor_new() {
|
||||
struct skynet_monitor * ret = malloc(sizeof(*ret));
|
||||
struct skynet_monitor * ret = skynet_malloc(sizeof(*ret));
|
||||
memset(ret, 0, sizeof(*ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
skynet_monitor_delete(struct skynet_monitor *sm) {
|
||||
free(sm);
|
||||
skynet_free(sm);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -83,7 +83,7 @@ skynet_globalmq_pop() {
|
||||
|
||||
struct message_queue *
|
||||
skynet_mq_create(uint32_t handle) {
|
||||
struct message_queue *q = malloc(sizeof(*q));
|
||||
struct message_queue *q = skynet_malloc(sizeof(*q));
|
||||
q->handle = handle;
|
||||
q->cap = DEFAULT_QUEUE_SIZE;
|
||||
q->head = 0;
|
||||
@@ -92,15 +92,15 @@ skynet_mq_create(uint32_t handle) {
|
||||
q->in_global = MQ_IN_GLOBAL;
|
||||
q->release = 0;
|
||||
q->lock_session = 0;
|
||||
q->queue = malloc(sizeof(struct skynet_message) * q->cap);
|
||||
q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
static void
|
||||
_release(struct message_queue *q) {
|
||||
free(q->queue);
|
||||
free(q);
|
||||
skynet_free(q->queue);
|
||||
skynet_free(q);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
@@ -148,7 +148,7 @@ skynet_mq_pop(struct message_queue *q, struct skynet_message *message) {
|
||||
|
||||
static void
|
||||
expand_queue(struct message_queue *q) {
|
||||
struct skynet_message *new_queue = malloc(sizeof(struct skynet_message) * q->cap * 2);
|
||||
struct skynet_message *new_queue = skynet_malloc(sizeof(struct skynet_message) * q->cap * 2);
|
||||
int i;
|
||||
for (i=0;i<q->cap;i++) {
|
||||
new_queue[i] = q->queue[(q->head + i) % q->cap];
|
||||
@@ -157,7 +157,7 @@ expand_queue(struct message_queue *q) {
|
||||
q->tail = q->cap;
|
||||
q->cap *= 2;
|
||||
|
||||
free(q->queue);
|
||||
skynet_free(q->queue);
|
||||
q->queue = new_queue;
|
||||
}
|
||||
|
||||
@@ -239,10 +239,10 @@ skynet_mq_unlock(struct message_queue *q) {
|
||||
|
||||
void
|
||||
skynet_mq_init() {
|
||||
struct global_queue *q = malloc(sizeof(*q));
|
||||
struct global_queue *q = skynet_malloc(sizeof(*q));
|
||||
memset(q,0,sizeof(*q));
|
||||
q->queue = malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *));
|
||||
q->flag = malloc(MAX_GLOBAL_MQ * sizeof(bool));
|
||||
q->queue = skynet_malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *));
|
||||
q->flag = skynet_malloc(MAX_GLOBAL_MQ * sizeof(bool));
|
||||
memset(q->flag, 0, sizeof(bool) * MAX_GLOBAL_MQ);
|
||||
Q=q;
|
||||
}
|
||||
@@ -286,7 +286,7 @@ _drop_queue(struct message_queue *q) {
|
||||
int s = 0;
|
||||
while(!skynet_mq_pop(q, &msg)) {
|
||||
++s;
|
||||
free(msg.data);
|
||||
skynet_free(msg.data);
|
||||
}
|
||||
_release(q);
|
||||
return s;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_server.h"
|
||||
@@ -97,7 +96,7 @@ skynet_context_new(const char * name, const char *param) {
|
||||
void *inst = skynet_module_instance_create(mod);
|
||||
if (inst == NULL)
|
||||
return NULL;
|
||||
struct skynet_context * ctx = malloc(sizeof(*ctx));
|
||||
struct skynet_context * ctx = skynet_malloc(sizeof(*ctx));
|
||||
CHECKCALLING_INIT(ctx)
|
||||
|
||||
ctx->mod = mod;
|
||||
@@ -152,7 +151,7 @@ static void
|
||||
_delete_context(struct skynet_context *ctx) {
|
||||
skynet_module_instance_release(ctx->mod, ctx->instance);
|
||||
skynet_mq_mark_release(ctx->queue);
|
||||
free(ctx);
|
||||
skynet_free(ctx);
|
||||
_context_dec();
|
||||
}
|
||||
|
||||
@@ -204,7 +203,7 @@ _dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
|
||||
int type = msg->sz >> HANDLE_REMOTE_SHIFT;
|
||||
size_t sz = msg->sz & HANDLE_MASK;
|
||||
if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) {
|
||||
free(msg->data);
|
||||
skynet_free(msg->data);
|
||||
}
|
||||
handle_tls = 0xffffffff;
|
||||
CHECKCALLING_END(ctx)
|
||||
@@ -236,7 +235,7 @@ skynet_context_message_dispatch(struct skynet_monitor *sm) {
|
||||
skynet_monitor_trigger(sm, msg.source , handle);
|
||||
|
||||
if (ctx->cb == NULL) {
|
||||
free(msg.data);
|
||||
skynet_free(msg.data);
|
||||
skynet_error(NULL, "Drop message from %x to %x without callback , size = %d",msg.source, handle, (int)msg.sz);
|
||||
} else {
|
||||
_dispatch_message(ctx, &msg);
|
||||
@@ -323,7 +322,7 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
||||
return skynet_handle_namehandle(context->handle, param + 1);
|
||||
} else {
|
||||
assert(context->handle!=0);
|
||||
struct remote_name *rname = malloc(sizeof(*rname));
|
||||
struct remote_name *rname = skynet_malloc(sizeof(*rname));
|
||||
_copy_name(rname->name, param);
|
||||
rname->handle = context->handle;
|
||||
skynet_harbor_register(rname);
|
||||
@@ -355,7 +354,7 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
||||
if (name[0] == '.') {
|
||||
return skynet_handle_namehandle(handle_id, name + 1);
|
||||
} else {
|
||||
struct remote_name *rname = malloc(sizeof(*rname));
|
||||
struct remote_name *rname = skynet_malloc(sizeof(*rname));
|
||||
_copy_name(rname->name, name);
|
||||
rname->handle = handle_id;
|
||||
skynet_harbor_register(rname);
|
||||
@@ -491,7 +490,7 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da
|
||||
}
|
||||
|
||||
if (needcopy && *data) {
|
||||
char * msg = malloc(*sz+1);
|
||||
char * msg = skynet_malloc(*sz+1);
|
||||
memcpy(msg, *data, *sz);
|
||||
msg[*sz] = '\0';
|
||||
*data = msg;
|
||||
@@ -513,7 +512,7 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati
|
||||
return session;
|
||||
}
|
||||
if (skynet_harbor_message_isremote(destination)) {
|
||||
struct remote_message * rmsg = malloc(sizeof(*rmsg));
|
||||
struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg));
|
||||
rmsg->destination.handle = destination;
|
||||
rmsg->message = data;
|
||||
rmsg->sz = sz;
|
||||
@@ -526,7 +525,7 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati
|
||||
smsg.sz = sz;
|
||||
|
||||
if (skynet_context_push(destination, &smsg)) {
|
||||
free(data);
|
||||
skynet_free(data);
|
||||
skynet_error(NULL, "Drop message from %x to %x (type=%d)(size=%d)", source, destination, type&0xff, (int)(sz & HANDLE_MASK));
|
||||
return -1;
|
||||
}
|
||||
@@ -544,7 +543,7 @@ skynet_sendname(struct skynet_context * context, const char * addr , int type, i
|
||||
des = skynet_handle_findname(addr + 1);
|
||||
if (des == 0) {
|
||||
if (type & PTYPE_TAG_DONTCOPY) {
|
||||
free(data);
|
||||
skynet_free(data);
|
||||
}
|
||||
skynet_error(context, "Drop message to %s", addr);
|
||||
return session;
|
||||
@@ -552,7 +551,7 @@ skynet_sendname(struct skynet_context * context, const char * addr , int type, i
|
||||
} else {
|
||||
_filter_args(context, type, &session, (void **)&data, &sz);
|
||||
|
||||
struct remote_message * rmsg = malloc(sizeof(*rmsg));
|
||||
struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg));
|
||||
_copy_name(rmsg->destination.name, addr);
|
||||
rmsg->destination.handle = 0;
|
||||
rmsg->message = data;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_socket.h"
|
||||
@@ -43,7 +42,7 @@ forward_message(int type, bool padding, struct socket_message * result) {
|
||||
sz += 1;
|
||||
}
|
||||
}
|
||||
sm = (struct skynet_socket_message *)malloc(sz);
|
||||
sm = (struct skynet_socket_message *)skynet_malloc(sz);
|
||||
sm->type = type;
|
||||
sm->id = result->id;
|
||||
sm->ud = result->ud;
|
||||
@@ -63,7 +62,7 @@ forward_message(int type, bool padding, struct socket_message * result) {
|
||||
if (skynet_context_push((uint32_t)result->opaque, &message)) {
|
||||
// todo: report somewhere to close socket
|
||||
// don't call skynet_socket_close here (It will block mainloop)
|
||||
free(sm);
|
||||
skynet_free(sm);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +105,7 @@ int
|
||||
skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) {
|
||||
int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz);
|
||||
if (wsz < 0) {
|
||||
free(buffer);
|
||||
skynet_free(buffer);
|
||||
return -1;
|
||||
} else if (wsz > 1024 * 1024) {
|
||||
int kb4 = wsz / 1024 / 4;
|
||||
|
||||
@@ -72,8 +72,8 @@ free_monitor(struct monitor *m) {
|
||||
}
|
||||
pthread_mutex_destroy(&m->mutex);
|
||||
pthread_cond_destroy(&m->cond);
|
||||
free(m->m);
|
||||
free(m);
|
||||
skynet_free(m->m);
|
||||
skynet_free(m);
|
||||
}
|
||||
|
||||
static void *
|
||||
@@ -140,12 +140,12 @@ static void
|
||||
_start(int thread) {
|
||||
pthread_t pid[thread+3];
|
||||
|
||||
struct monitor *m = malloc(sizeof(*m));
|
||||
struct monitor *m = skynet_malloc(sizeof(*m));
|
||||
memset(m, 0, sizeof(*m));
|
||||
m->count = thread;
|
||||
m->sleep = 0;
|
||||
|
||||
m->m = malloc(thread * sizeof(struct skynet_monitor *));
|
||||
m->m = skynet_malloc(thread * sizeof(struct skynet_monitor *));
|
||||
int i;
|
||||
for (i=0;i<thread;i++) {
|
||||
m->m[i] = skynet_monitor_new();
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "skynet_timer.h"
|
||||
@@ -93,7 +92,7 @@ add_node(struct timer *T,struct timer_node *node)
|
||||
static void
|
||||
timer_add(struct timer *T,void *arg,size_t sz,int time)
|
||||
{
|
||||
struct timer_node *node = (struct timer_node *)malloc(sizeof(*node)+sz);
|
||||
struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz);
|
||||
memcpy(node+1,arg,sz);
|
||||
|
||||
while (__sync_lock_test_and_set(&T->lock,1)) {};
|
||||
@@ -147,7 +146,7 @@ timer_execute(struct timer *T) {
|
||||
|
||||
struct timer_node * temp = current;
|
||||
current=current->next;
|
||||
free(temp);
|
||||
skynet_free(temp);
|
||||
} while (current);
|
||||
}
|
||||
}
|
||||
@@ -170,7 +169,7 @@ timer_update(struct timer *T)
|
||||
static struct timer *
|
||||
timer_create_timer()
|
||||
{
|
||||
struct timer *r=(struct timer *)malloc(sizeof(struct timer));
|
||||
struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer));
|
||||
memset(r,0,sizeof(*r));
|
||||
|
||||
int i,j;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// include skynet.h first for malloc hook
|
||||
#include "skynet.h"
|
||||
|
||||
#include "socket_server.h"
|
||||
@@ -128,8 +127,8 @@ union sockaddr_all {
|
||||
struct sockaddr_in6 v6;
|
||||
};
|
||||
|
||||
#define MALLOC malloc
|
||||
#define FREE free
|
||||
#define MALLOC skynet_malloc
|
||||
#define FREE skynet_free
|
||||
|
||||
static void
|
||||
socket_keepalive(int fd) {
|
||||
|
||||
Reference in New Issue
Block a user