From: Chris Porter Date: Tue, 4 Mar 2008 00:07:42 +0000 (+0000) Subject: Forgot to commit changes to nsmalloc. X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/commitdiff_plain/ed24756c6109b56fbe43d15f356373478d852904?hp=33424c74edd2e4c3ec685e3a24d5f7bad96bf9a8 Forgot to commit changes to nsmalloc. --- diff --git a/core/nsmalloc.c b/core/nsmalloc.c index 6b3356ee..49a268e9 100644 --- a/core/nsmalloc.c +++ b/core/nsmalloc.c @@ -1,8 +1,6 @@ /* nsmalloc: Simple pooled malloc() thing. */ #include -#include -#include #include "nsmalloc.h" #define __NSMALLOC_C @@ -12,34 +10,9 @@ #include "../core/hooks.h" #include "../core/error.h" -#ifndef USE_VALGRIND - -void *nsmalloc(unsigned int poolid, size_t size); -void nsfree(unsigned int poolid, void *ptr); -void nsfreeall(unsigned int poolid); - -struct nsminfo { - struct nsminfo *next; - struct nsminfo *prev; - - size_t size; - char data[]; -}; - -struct nsmpool { - struct nsminfo first; - - unsigned long count; - size_t size; -}; - -struct nsmpool pools[MAXPOOL]; +struct nsmpool nsmpools[MAXPOOL]; -void nsmstats(int hookhum, void *arg); - -void initnsmalloc(void) { - registerhook(HOOK_CORE_STATSREQUEST, &nsmstats); -} +#ifndef USE_VALGRIND void *nsmalloc(unsigned int poolid, size_t size) { struct nsminfo *nsmp; @@ -54,14 +27,14 @@ void *nsmalloc(unsigned int poolid, size_t size) { return NULL; nsmp->size=size; - pools[poolid].size+=size; - pools[poolid].count++; + nsmpools[poolid].size+=size; + nsmpools[poolid].count++; - nsmp->next=pools[poolid].first.next; - nsmp->prev=&pools[poolid].first; - if (pools[poolid].first.next) - pools[poolid].first.next->prev=nsmp; - pools[poolid].first.next=nsmp; + nsmp->next=nsmpools[poolid].first.next; + nsmp->prev=&nsmpools[poolid].first; + if (nsmpools[poolid].first.next) + nsmpools[poolid].first.next->prev=nsmp; + nsmpools[poolid].first.next=nsmp; return (void *)nsmp->data; } @@ -81,8 +54,8 @@ void nsfree(unsigned int poolid, void *ptr) { if (nsmp->next) nsmp->next->prev=nsmp->prev; - pools[poolid].size-=nsmp->size; - pools[poolid].count--; + nsmpools[poolid].size-=nsmp->size; + nsmpools[poolid].count--; free(nsmp); return; @@ -112,7 +85,7 @@ void *nsrealloc(unsigned int poolid, void *ptr, size_t size) { if (!nsmpn) return NULL; - pools[poolid].size+=size-nsmpn->size; + nsmpools[poolid].size+=size-nsmpn->size; nsmpn->size=size; /* always set as we have a sentinel */ @@ -130,22 +103,22 @@ void nsfreeall(unsigned int poolid) { if (poolid >= MAXPOOL) return; - for (nsmp=pools[poolid].first.next;nsmp;nsmp=nnsmp) { + for (nsmp=nsmpools[poolid].first.next;nsmp;nsmp=nnsmp) { nnsmp=nsmp->next; free(nsmp); } - pools[poolid].first.next=NULL; - pools[poolid].size=0; - pools[poolid].count=0; + nsmpools[poolid].first.next=NULL; + nsmpools[poolid].size=0; + nsmpools[poolid].count=0; } void nscheckfreeall(unsigned int poolid) { if (poolid >= MAXPOOL) return; - if (pools[poolid].first.next) { - Error("core",ERR_INFO,"nsmalloc: Blocks still allocated in pool #%d (%s): %lub, %lu items",poolid,poolnames[poolid]?poolnames[poolid]:"??",pools[poolid].size,pools[poolid].count); + if (nsmpools[poolid].first.next) { + Error("core",ERR_INFO,"nsmalloc: Blocks still allocated in pool #%d (%s): %lub, %lu items",poolid,nsmpoolnames[poolid]?nsmpoolnames[poolid]:"??",nsmpools[poolid].size,nsmpools[poolid].count); nsfreeall(poolid); } } @@ -157,62 +130,8 @@ void nsexit(void) { nscheckfreeall(i); } -static char *formatmbuf(unsigned long count, size_t size, size_t realsize) { - static char buf[1024]; - - snprintf(buf, sizeof(buf), "%lu items, %luKb allocated for %luKb, %luKb (%.2f%%) overhead", count, (unsigned long)size / 1024, (unsigned long)realsize / 1024, (unsigned long)(realsize - size) / 1024, (double)(realsize - size) / (double)size * 100); - return buf; -} - -void nsmstats(int hookhum, void *arg) { - int i; - char buf[1024], extra[1024]; - unsigned long totalcount = 0; - size_t totalsize = 0, totalrealsize = 0; - long level = (long)arg; - - for (i=0;icount) - continue; - - realsize=pool->size + pool->count * sizeof(struct nsminfo) + sizeof(struct nsmpool); - - totalsize+=pool->size; - totalrealsize+=realsize; - totalcount+=pool->count; - - if(level > 10) { - extra[0] = '\0'; - if(level > 100) { - struct nsminfo *np = pool->first.next; - double mean = (double)pool->size / pool->count, variance; - unsigned long long int sumsq = 0; - - for (np=pool->first.next;np;np=np->next) - sumsq+=np->size * np->size; - - variance=(double)sumsq / pool->count - mean * mean; - - snprintf(extra, sizeof(extra), ", mean: %.2fKb stddev: %.2fKb", mean / 1024, sqrtf(variance) / 1024); - } - - snprintf(buf, sizeof(buf), "NSMalloc: pool %2d (%10s): %s%s", i, poolnames[i]?poolnames[i]:"??", formatmbuf(pool->count, pool->size, realsize), extra); - triggerhook(HOOK_CORE_STATSREPLY, buf); - } - } - - snprintf(buf, sizeof(buf), "NSMalloc: pool totals: %s", formatmbuf(totalcount, totalsize, totalrealsize)); - triggerhook(HOOK_CORE_STATSREPLY, buf); -} - #else -void initnsmalloc(void) { -} - void *nsmalloc(unsigned int poolid, size_t size) { return malloc(size); } diff --git a/core/nsmalloc.h b/core/nsmalloc.h index c7c4729b..622d11f9 100644 --- a/core/nsmalloc.h +++ b/core/nsmalloc.h @@ -5,12 +5,12 @@ #ifdef __NSMALLOC_C #define pool(x) #x -#define beginpools() char *poolnames[MAXPOOL] = +#define beginpools() char *nsmpoolnames[MAXPOOL] = #define endpools(); #else #define pool(x) POOL_ ## x #define beginpools(x) typedef enum nsmallocpools -#define endpools() nsmallocpools; +#define endpools() nsmallocpools; extern char *nsmpoolnames[MAXPOOL]; #include @@ -20,10 +20,26 @@ void nsfreeall(unsigned int poolid); void nsexit(void); void *nsrealloc(unsigned int poolid, void *ptr, size_t size); void nscheckfreeall(unsigned int poolid); -void initnsmalloc(void); #define MAXPOOL 100 +struct nsminfo { + struct nsminfo *next; + struct nsminfo *prev; + + size_t size; + char data[]; +}; + +struct nsmpool { + struct nsminfo first; + + unsigned long count; + size_t size; +}; + +extern struct nsmpool nsmpools[MAXPOOL]; + #endif /* Pools here in the order they were created */