]> jfr.im git - irc/quakenet/newserv.git/blame - core/nsmalloc.c
Make valgrind a bit happier.
[irc/quakenet/newserv.git] / core / nsmalloc.c
CommitLineData
34da4416 1/* nsmalloc: Simple pooled malloc() thing. */
2
3#include <stdlib.h>
4
5#include "nsmalloc.h"
6#include "../core/error.h"
7
8void *nsmalloc(unsigned int poolid, size_t size);
9void nsfree(unsigned int poolid, void *ptr);
10void nsfreeall(unsigned int poolid);
11
12struct nsminfo {
13 struct nsminfo *next;
20d694a5 14 char data[];
34da4416 15};
16
17struct nsminfo *pools[MAXPOOL];
18
19void *nsmalloc(unsigned int poolid, size_t size) {
20 struct nsminfo *nsmp;
21
22 if (poolid >= MAXPOOL)
23 return NULL;
24
20d694a5 25 /* Allocate enough for the structure and the required data */
26 nsmp=(struct nsminfo *)malloc(sizeof(struct nsminfo)+size);
34da4416 27
28 if (!nsmp)
29 return NULL;
30
31 nsmp->next=pools[poolid];
32 pools[poolid]=nsmp;
33
34 return (void *)nsmp->data;
35}
36
37void nsfree(unsigned int poolid, void *ptr) {
38 struct nsminfo *nsmp, **nsmh;
39
40 if (poolid >= MAXPOOL)
41 return;
42
43 for (nsmh=&(pools[poolid]);*nsmh;nsmh=&((*nsmh)->next)) {
44 if ((void *)&((*nsmh)->data) == ptr) {
45 nsmp=*nsmh;
46 *nsmh = nsmp->next;
47 free(nsmp);
48 return;
49 }
50 }
51
52 Error("core",ERR_WARNING,"Attempt to free unknown pointer %p in pool %d\n",ptr,poolid);
53}
54
55void nsfreeall(unsigned int poolid) {
56 struct nsminfo *nsmp, *nnsmp;
57
58 if (poolid >= MAXPOOL)
59 return;
60
61 for (nsmp=pools[poolid];nsmp;nsmp=nnsmp) {
62 nnsmp=nsmp->next;
63 free(nsmp);
64 }
65
66 pools[poolid]=NULL;
67}
68
103521a1 69void nsexit() {
70 unsigned int i;
71
72 for (i=0;i<MAXPOOL;i++) {
73 if (pools[i]) {
74 Error("core",ERR_INFO,"nsmalloc: Blocks still allocated in pool #%d\n",i);
75 nsfreeall(i);
76 }
77 }
78}
79