diff options
author | Dmitry Kozlov <xeb@mail.ru> | 2010-09-20 01:09:07 +0400 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2010-09-20 01:09:34 +0400 |
commit | b96fbc3f966b012720d2b74b1dfd2a0ab95086cf (patch) | |
tree | 606750874faf65029f756dc0b401ee40cc4da89f /accel-pptpd/triton | |
parent | d860a4beaf5f99d5045d03b931b4829426a2f7b0 (diff) | |
download | accel-ppp-b96fbc3f966b012720d2b74b1dfd2a0ab95086cf.tar.gz accel-ppp-b96fbc3f966b012720d2b74b1dfd2a0ab95086cf.zip |
fixed many bugs and memory leaks
Diffstat (limited to 'accel-pptpd/triton')
-rw-r--r-- | accel-pptpd/triton/conf_file.c | 27 | ||||
-rw-r--r-- | accel-pptpd/triton/event.c | 4 | ||||
-rw-r--r-- | accel-pptpd/triton/loader.c | 4 | ||||
-rw-r--r-- | accel-pptpd/triton/log.c | 2 | ||||
-rw-r--r-- | accel-pptpd/triton/md.c | 22 | ||||
-rw-r--r-- | accel-pptpd/triton/mempool.c | 89 | ||||
-rw-r--r-- | accel-pptpd/triton/mempool.h | 17 | ||||
-rw-r--r-- | accel-pptpd/triton/options.c | 2 | ||||
-rw-r--r-- | accel-pptpd/triton/timer.c | 22 | ||||
-rw-r--r-- | accel-pptpd/triton/triton.c | 60 | ||||
-rw-r--r-- | accel-pptpd/triton/triton.h | 20 |
11 files changed, 230 insertions, 39 deletions
diff --git a/accel-pptpd/triton/conf_file.c b/accel-pptpd/triton/conf_file.c index c6ba7453..6eb2e7a8 100644 --- a/accel-pptpd/triton/conf_file.c +++ b/accel-pptpd/triton/conf_file.c @@ -5,6 +5,8 @@ #include "triton_p.h" +#include "memdebug.h" + struct sect_t { struct list_head entry; @@ -35,15 +37,14 @@ int conf_load(const char *fname) return -1; } - buf = malloc(1024); - path0 = malloc(4096); - path = malloc(4096); + buf = _malloc(1024); + path0 = _malloc(4096); + path = _malloc(4096); getcwd(path0, 1024); while(!feof(f)) { - buf = fgets(buf, 1024, f); - if (!buf) + if (!fgets(buf, 1024, f)) break; ++cur_line; if (buf[strlen(buf) - 1] == '\n') @@ -103,9 +104,9 @@ int conf_load(const char *fname) sect_add_item(cur_sect, str, str2); } - free(buf); - free(path); - free(path0); + _free(buf); + _free(path); + _free(path0); fclose(f); return 0; @@ -132,9 +133,9 @@ static struct conf_sect_t *find_sect(const char *name) static struct conf_sect_t *create_sect(const char *name) { - struct sect_t *s = malloc(sizeof(struct sect_t)); + struct sect_t *s = _malloc(sizeof(struct sect_t)); - s->sect = malloc(sizeof(struct conf_sect_t)); + s->sect = _malloc(sizeof(struct conf_sect_t)); s->sect->name = (char*)strdup(name); INIT_LIST_HEAD(&s->sect->items); @@ -145,10 +146,10 @@ static struct conf_sect_t *create_sect(const char *name) static void sect_add_item(struct conf_sect_t *sect, const char *name, const char *val) { - struct conf_option_t *opt = malloc(sizeof(struct conf_option_t)); + struct conf_option_t *opt = _malloc(sizeof(struct conf_option_t)); - opt->name = strdup(name); - opt->val = val ? strdup(val) : NULL; + opt->name = _strdup(name); + opt->val = val ? _strdup(val) : NULL; list_add_tail(&opt->entry, §->items); } diff --git a/accel-pptpd/triton/event.c b/accel-pptpd/triton/event.c index 442543f2..d45eca01 100644 --- a/accel-pptpd/triton/event.c +++ b/accel-pptpd/triton/event.c @@ -4,6 +4,8 @@ #include "triton_p.h" +#include "memdebug.h" + static int max_events = 1024; static struct _triton_event_t **events; @@ -76,7 +78,7 @@ int __export triton_event_register_handler(int ev_id, triton_event_func func) h->func = NULL; else { list_del(&h->entry); - free(h); + _free(h); } return 0; } diff --git a/accel-pptpd/triton/loader.c b/accel-pptpd/triton/loader.c index 24d1cbcb..a8b9c500 100644 --- a/accel-pptpd/triton/loader.c +++ b/accel-pptpd/triton/loader.c @@ -7,6 +7,8 @@ #include "triton_p.h" +#include "memdebug.h" + int load_modules(const char *name) { struct conf_sect_t *sect; @@ -40,7 +42,7 @@ int load_modules(const char *name) out_err: chdir(cwd); - free(cwd); + _free(cwd); return -1; } diff --git a/accel-pptpd/triton/log.c b/accel-pptpd/triton/log.c index 4504db4f..7bb55598 100644 --- a/accel-pptpd/triton/log.c +++ b/accel-pptpd/triton/log.c @@ -3,6 +3,8 @@ #include "triton_p.h" +#include "memdebug.h" + static FILE *f_error; static FILE *f_debug; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c index 05d814cc..06b2b30b 100644 --- a/accel-pptpd/triton/md.c +++ b/accel-pptpd/triton/md.c @@ -8,6 +8,8 @@ #include "triton_p.h" +#include "memdebug.h" + extern int max_events; static int epoll_fd; @@ -26,7 +28,7 @@ int md_init(void) return -1; } - epoll_events = malloc(max_events * sizeof(struct epoll_event)); + epoll_events = _malloc(max_events * sizeof(struct epoll_event)); if (!epoll_events) { fprintf(stderr,"md:cann't allocate memory\n"); return -1; @@ -54,6 +56,20 @@ static void *md_thread(void *arg) { int i,n,r; struct _triton_md_handler_t *h; + sigset_t set; + + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGSEGV); + sigaddset(&set, SIGFPE); + sigaddset(&set, SIGILL); + sigaddset(&set, SIGBUS); + sigdelset(&set, 35); + sigdelset(&set, 36); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); while(1) { n = epoll_wait(epoll_fd, epoll_events, max_events, -1); @@ -100,6 +116,8 @@ void __export triton_md_register_handler(struct triton_context_t *ctx, struct tr spin_lock(&h->ctx->lock); list_add_tail(&h->entry, &h->ctx->handlers); spin_unlock(&h->ctx->lock); + + __sync_fetch_and_add(&triton_stat.md_handler_count, 1); } void __export triton_md_unregister_handler(struct triton_md_handler_t *ud) { @@ -113,6 +131,8 @@ void __export triton_md_unregister_handler(struct triton_md_handler_t *ud) spin_unlock(&h->ctx->lock); sched_yield(); mempool_free(h); + + __sync_fetch_and_sub(&triton_stat.md_handler_count, 1); } int __export triton_md_enable_handler(struct triton_md_handler_t *ud, int mode) { diff --git a/accel-pptpd/triton/mempool.c b/accel-pptpd/triton/mempool.c index 6d9172a1..34739b43 100644 --- a/accel-pptpd/triton/mempool.c +++ b/accel-pptpd/triton/mempool.c @@ -1,11 +1,15 @@ #include <stdint.h> #include <stdlib.h> #include <string.h> +#include <signal.h> #include "triton_p.h" +#include "memdebug.h" + struct _mempool_t { + struct list_head entry; int size; struct list_head items; spinlock_t lock; @@ -20,9 +24,12 @@ struct _item_t char ptr[0]; }; +static LIST_HEAD(pools); +static spinlock_t pools_lock = SPINLOCK_INITIALIZER; + __export mempool_t *mempool_create(int size) { - struct _mempool_t *p = malloc(sizeof(*p)); + struct _mempool_t *p = _malloc(sizeof(*p)); memset(p, 0, sizeof(*p)); INIT_LIST_HEAD(&p->items); @@ -30,35 +37,82 @@ __export mempool_t *mempool_create(int size) p->size = size; p->magic = (uint64_t)random() * (uint64_t)random(); + spin_lock(&pools_lock); + list_add_tail(&p->entry, &pools); + spin_unlock(&pools_lock); + return (mempool_t *)p; } +#ifndef MEMDEBUG __export void *mempool_alloc(mempool_t *pool) { struct _mempool_t *p = (struct _mempool_t *)pool; struct _item_t *it; + uint32_t size = sizeof(*it) + p->size; + + spin_lock(&p->lock); + if (!list_empty(&p->items)) { + it = list_entry(p->items.next, typeof(*it), entry); + list_del(&it->entry); + spin_unlock(&p->lock); + + __sync_fetch_and_sub(&triton_stat.mempool_available, size); + + return it->ptr; + } + spin_unlock(&p->lock); + + it = _malloc(size); + if (!it) { + triton_log_error("mempool: out of memory\n"); + return NULL; + } + it->owner = p; + it->magic = p->magic; + + __sync_fetch_and_add(&triton_stat.mempool_allocated, size); + + return it->ptr; +} +#endif + +void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line) +{ + struct _mempool_t *p = (struct _mempool_t *)pool; + struct _item_t *it; + uint32_t size = sizeof(*it) + p->size; spin_lock(&p->lock); if (!list_empty(&p->items)) { it = list_entry(p->items.next, typeof(*it), entry); list_del(&it->entry); spin_unlock(&p->lock); + + __sync_fetch_and_sub(&triton_stat.mempool_available, size); + return it->ptr; } spin_unlock(&p->lock); - it = malloc(sizeof(*it) + p->size); + + it = md_malloc(size, fname, line); if (!it) { triton_log_error("mempool: out of memory\n"); return NULL; } it->owner = p; it->magic = p->magic; + + __sync_fetch_and_add(&triton_stat.mempool_allocated, size); + return it->ptr; } + __export void mempool_free(void *ptr) { struct _item_t *it = container_of(ptr, typeof(*it), ptr); + uint32_t size = sizeof(*it) + it->owner->size; if (it->magic != it->owner->magic) { triton_log_error("mempool: memory corruption detected"); @@ -67,5 +121,36 @@ __export void mempool_free(void *ptr) spin_lock(&it->owner->lock); list_add_tail(&it->entry,&it->owner->items); spin_unlock(&it->owner->lock); + + __sync_fetch_and_add(&triton_stat.mempool_available, size); +} + +void sigclean(int num) +{ + struct _mempool_t *p; + struct _item_t *it; + uint32_t size; + + triton_log_error("mempool: clean\n"); + + spin_lock(&pools_lock); + list_for_each_entry(p, &pools, entry) { + size = sizeof(*it) + p->size; + spin_lock(&p->lock); + while (!list_empty(&p->items)) { + it = list_entry(p->items.next, typeof(*it), entry); + list_del(&it->entry); + _free(it); + __sync_fetch_and_sub(&triton_stat.mempool_allocated, size); + __sync_fetch_and_sub(&triton_stat.mempool_available, size); + } + spin_unlock(&p->lock); + } + spin_unlock(&pools_lock); +} + +static void __init init(void) +{ + signal(35, sigclean); } diff --git a/accel-pptpd/triton/mempool.h b/accel-pptpd/triton/mempool.h index e8bcaf6a..d3539215 100644 --- a/accel-pptpd/triton/mempool.h +++ b/accel-pptpd/triton/mempool.h @@ -1,10 +1,25 @@ #ifndef __TRITON_MEMPOOL_H #define __TRITON_MEMPOOL_H +#include <stdint.h> + +struct mempool_stat_t +{ + uint32_t allocated; + uint32_t available; +}; + typedef void * mempool_t; mempool_t *mempool_create(int size); -void *mempool_alloc(mempool_t*); void mempool_free(void*); +struct mempool_stat_t mempool_get_stat(void); + +#ifdef MEMDEBUG +void *mempool_alloc_md(mempool_t*, const char *fname, int line); +#define mempool_alloc(pool) mempool_alloc_md(pool, __FILE__, __LINE__) +#else +void *mempool_alloc(mempool_t*); +#endif #endif diff --git a/accel-pptpd/triton/options.c b/accel-pptpd/triton/options.c index ba7fc564..a5214e21 100644 --- a/accel-pptpd/triton/options.c +++ b/accel-pptpd/triton/options.c @@ -4,6 +4,8 @@ #include "triton_p.h" #include "conf_file.h" +#include "memdebug.h" + static struct conf_file_sect_t *sect=NULL; static const char* find_option(const char *name) diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c index d2249786..53abd3b9 100644 --- a/accel-pptpd/triton/timer.c +++ b/accel-pptpd/triton/timer.c @@ -9,6 +9,8 @@ #include "triton_p.h" +#include "memdebug.h" + extern int max_events; static int epoll_fd; static struct epoll_event *epoll_events; @@ -26,7 +28,7 @@ int timer_init(void) return -1; } - epoll_events = malloc(max_events * sizeof(struct epoll_event)); + epoll_events = _malloc(max_events * sizeof(struct epoll_event)); if (!epoll_events) { fprintf(stderr,"timer:cann't allocate memory\n"); return -1; @@ -55,7 +57,19 @@ void *timer_thread(void *arg) { int i,n,r; struct _triton_timer_t *t; - + sigset_t set; + + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGSEGV); + sigaddset(&set, SIGFPE); + sigaddset(&set, SIGILL); + sigaddset(&set, SIGBUS); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + while(1) { n = epoll_wait(epoll_fd, epoll_events, max_events, -1); if (n < 0) { @@ -129,6 +143,8 @@ int __export triton_timer_add(struct triton_context_t *ctx, struct triton_timer_ return -1; } + __sync_fetch_and_add(&triton_stat.timer_count, 1); + return 0; } int __export triton_timer_mod(struct triton_timer_t *ud,int abs_time) @@ -165,5 +181,7 @@ void __export triton_timer_del(struct triton_timer_t *ud) sched_yield(); mempool_free(t); ud->tpd = NULL; + + __sync_fetch_and_sub(&triton_stat.timer_count, 1); } diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 9b9fd753..95930647 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -6,6 +6,7 @@ #include <unistd.h> #include "triton_p.h" +#include "memdebug.h" int thread_count = 1; int max_events = 64; @@ -25,6 +26,8 @@ static int terminate; static mempool_t *ctx_pool; static mempool_t *call_pool; +__export struct triton_stat_t triton_stat; + void triton_thread_wakeup(struct _triton_thread_t *thread) { pthread_kill(thread->thread, SIGUSR1); @@ -33,25 +36,21 @@ void triton_thread_wakeup(struct _triton_thread_t *thread) static void* triton_thread(struct _triton_thread_t *thread) { sigset_t set; - int sig; sigfillset(&set); - pthread_sigmask(SIG_BLOCK, &set, NULL); - - sigdelset(&set, SIGUSR1); - sigdelset(&set, SIGQUIT); sigdelset(&set, SIGSEGV); sigdelset(&set, SIGFPE); sigdelset(&set, SIGILL); sigdelset(&set, SIGBUS); - pthread_sigmask(SIG_UNBLOCK, &set, NULL); + pthread_sigmask(SIG_SETMASK, &set, NULL); - sigemptyset(&set); - sigaddset(&set, SIGUSR1); - sigaddset(&set, SIGQUIT); + sigdelset(&set, SIGUSR1); + sigdelset(&set, SIGQUIT); while (1) { - sigwait(&set, &sig); + __sync_fetch_and_sub(&triton_stat.thread_active, 1); + sigsuspend(&set); + __sync_fetch_and_add(&triton_stat.thread_active, 1); cont: if (thread->ctx->ud->before_switch) @@ -72,6 +71,7 @@ cont: thread->ctx->thread = thread; thread->ctx->queued = 0; spin_unlock(&thread->ctx->lock); + __sync_fetch_and_sub(&triton_stat.context_pending, 1); goto cont; } else { if (!terminate) @@ -142,7 +142,7 @@ static void ctx_thread(struct _triton_context_t *ctx) struct _triton_thread_t *create_thread() { - struct _triton_thread_t *thread = malloc(sizeof(*thread)); + struct _triton_thread_t *thread = _malloc(sizeof(*thread)); if (!thread) return NULL; @@ -152,6 +152,9 @@ struct _triton_thread_t *create_thread() return NULL; } + triton_stat.thread_count++; + triton_stat.thread_active++; + return thread; } @@ -165,6 +168,7 @@ int triton_queue_ctx(struct _triton_context_t *ctx) list_add_tail(&ctx->entry2, &ctx_queue); spin_unlock(&threads_lock); ctx->queued = 1; + __sync_fetch_and_add(&triton_stat.context_pending, 1); return 0; } @@ -195,15 +199,15 @@ int __export triton_context_register(struct triton_context_t *ud, void *bf_arg) if (getcontext(&ctx->uctx)) { triton_log_error("getcontext: %s\n", strerror(errno)); - free(ctx); + _free(ctx); return -1; } ctx->uctx.uc_stack.ss_size = CTX_STACK_SIZE; - ctx->uctx.uc_stack.ss_sp = malloc(CTX_STACK_SIZE); + ctx->uctx.uc_stack.ss_sp = _malloc(CTX_STACK_SIZE); if (!ctx->uctx.uc_stack.ss_sp) { triton_log_error("out of memory\n"); - free(ctx); + _free(ctx); return -1; } makecontext(&ctx->uctx, (void (*)())ctx_thread, 1, ctx); @@ -214,12 +218,21 @@ int __export triton_context_register(struct triton_context_t *ud, void *bf_arg) list_add_tail(&ctx->entry, &ctx_list); spin_unlock(&ctx_list_lock); + __sync_fetch_and_add(&triton_stat.context_count, 1); + return 0; } void __export triton_context_unregister(struct triton_context_t *ud) { struct _triton_context_t *ctx = (struct _triton_context_t *)ud->tpd; + struct _triton_ctx_call_t *call; + + while (!list_empty(&ctx->pending_calls)) { + call = list_entry(ctx->pending_calls.next, typeof(*call), entry); + list_del(&call->entry); + mempool_free(call); + } if (!list_empty(&ctx->handlers)) { triton_log_error("BUG:ctx:triton_unregister_ctx: handlers is not empty"); @@ -246,10 +259,14 @@ void __export triton_context_unregister(struct triton_context_t *ud) abort(); } + _free(ctx->uctx.uc_stack.ss_sp); + ctx->need_free = 1; spin_lock(&ctx_list_lock); list_del(&ctx->entry); spin_unlock(&ctx_list_lock); + + __sync_fetch_and_sub(&triton_stat.context_count, 1); } void __export triton_context_schedule(struct triton_context_t *ud) { @@ -263,6 +280,8 @@ void __export triton_context_schedule(struct triton_context_t *ud) if (swapcontext(&ctx->uctx, uctx)) triton_log_error("swaswpntext: %s\n", strerror(errno)); + + __sync_fetch_and_add(&triton_stat.context_sleeping, 1); } void __export triton_context_wakeup(struct triton_context_t *ud) @@ -277,6 +296,8 @@ void __export triton_context_wakeup(struct triton_context_t *ud) if (r) triton_thread_wakeup(ctx->thread); + + __sync_fetch_and_sub(&triton_stat.context_sleeping, 1); } int __export triton_context_call(struct triton_context_t *ud, void (*func)(void *), void *arg) @@ -302,12 +323,12 @@ int __export triton_context_call(struct triton_context_t *ud, void (*func)(void return 0; } -int __export triton_init(const char *conf_file, const char *mod_sect) +int __export triton_init(const char *conf_file) { ctx_pool = mempool_create(sizeof(struct _triton_context_t)); call_pool = mempool_create(sizeof(struct _triton_ctx_call_t)); - default_ctx = malloc(sizeof(*default_ctx)); + default_ctx = _malloc(sizeof(*default_ctx)); if (!default_ctx) { fprintf(stderr,"cann't allocate memory\n"); return -1; @@ -329,9 +350,14 @@ int __export triton_init(const char *conf_file, const char *mod_sect) if (event_init()) return -1; + return 0; +} + +int __export triton_load_modules(const char *mod_sect) +{ if (load_modules(mod_sect)) return -1; - + return 0; } diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index 8510a512..c3c9d2a9 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -2,6 +2,7 @@ #define TRITON_H #include <sys/time.h> +#include <stdint.h> #include "list.h" @@ -49,6 +50,22 @@ struct conf_sect_t struct list_head items; }; +struct triton_stat_t +{ + uint32_t mempool_allocated; + uint32_t mempool_available; + uint32_t thread_count; + uint32_t thread_active; + uint32_t context_count; + uint32_t context_sleeping; + uint32_t context_pending; + uint32_t md_handler_count; + uint32_t md_handler_pending; + uint32_t timer_count; + uint32_t timer_pending; +}; + +extern struct triton_stat_t triton_stat; int triton_context_register(struct triton_context_t *, void *arg); void triton_context_unregister(struct triton_context_t *); void triton_context_schedule(struct triton_context_t *); @@ -82,7 +99,8 @@ char *conf_get_opt(const char *sect, const char *name); #define TRITON_ERR_NOMSG -6 #define TRITON_ERR_BUSY -5 -int triton_init(const char *conf_file, const char *mod_sect); +int triton_init(const char *conf_file); +int triton_load_modules(const char *md_sect); void triton_run(void); void triton_terminate(void); |