]> jfr.im git - irc/quakenet/newserv.git/blame - core/nsmalloc.c
BANS/CHANNEL: Changed IP parsing to use ircd functions. Adds IPv6 support and fixes
[irc/quakenet/newserv.git] / core / nsmalloc.c
CommitLineData
34da4416 1/* nsmalloc: Simple pooled malloc() thing. */
2
3#include <stdlib.h>
f358bcd6 4#include <string.h>
34da4416 5
6#include "nsmalloc.h"
271ef2d2
CP
7#define __NSMALLOC_C
8#undef __NSMALLOC_H
9#include "nsmalloc.h"
10
19f37c5c 11#include "../core/hooks.h"
34da4416 12#include "../core/error.h"
13
ed24756c 14struct nsmpool nsmpools[MAXPOOL];
34da4416 15
707c5824 16#ifndef USE_NSMALLOC_VALGRIND
19f37c5c 17
34da4416 18void *nsmalloc(unsigned int poolid, size_t size) {
19 struct nsminfo *nsmp;
20
21 if (poolid >= MAXPOOL)
22 return NULL;
23
20d694a5 24 /* Allocate enough for the structure and the required data */
25 nsmp=(struct nsminfo *)malloc(sizeof(struct nsminfo)+size);
34da4416 26
27 if (!nsmp)
28 return NULL;
29
48005496 30 nsmp->size=size;
ed24756c
CP
31 nsmpools[poolid].size+=size;
32 nsmpools[poolid].count++;
48005496 33
ed24756c
CP
34 nsmp->next=nsmpools[poolid].first.next;
35 nsmp->prev=&nsmpools[poolid].first;
36 if (nsmpools[poolid].first.next)
37 nsmpools[poolid].first.next->prev=nsmp;
38 nsmpools[poolid].first.next=nsmp;
48005496 39
34da4416 40 return (void *)nsmp->data;
41}
42
f358bcd6
CP
43void *nscalloc(unsigned int poolid, size_t nmemb, size_t size) {
44 size_t total = nmemb * size;
45 void *m;
46
47 m = nsmalloc(poolid, total);
48 if(!m)
49 return NULL;
50
51 memset(m, 0, total);
52
53 return m;
54}
55
48005496 56/* we dump core on ptr == NULL */
34da4416 57void nsfree(unsigned int poolid, void *ptr) {
48005496 58 struct nsminfo *nsmp;
34da4416 59
7b74d6e6 60 if (!ptr || poolid >= MAXPOOL)
34da4416 61 return;
48005496
CP
62
63 /* evil */
64 nsmp=(struct nsminfo*)ptr - 1;
65
66 /* always set as we have a sentinel */
271ef2d2 67 nsmp->prev->next=nsmp->next;
48005496 68 if (nsmp->next)
271ef2d2 69 nsmp->next->prev=nsmp->prev;
48005496 70
ed24756c
CP
71 nsmpools[poolid].size-=nsmp->size;
72 nsmpools[poolid].count--;
48005496
CP
73
74 free(nsmp);
75 return;
76}
77
78void *nsrealloc(unsigned int poolid, void *ptr, size_t size) {
79 struct nsminfo *nsmp, *nsmpn;
80
81 if (ptr == NULL)
82 return nsmalloc(poolid, size);
83
84 if (size == 0) {
85 nsfree(poolid, ptr);
86 return NULL;
34da4416 87 }
48005496
CP
88
89 if (poolid >= MAXPOOL)
90 return NULL;
91
92 /* evil */
93 nsmp=(struct nsminfo *)ptr - 1;
94
95 if (size == nsmp->size)
96 return (void *)nsmp->data;
97
98 nsmpn=(struct nsminfo *)realloc(nsmp, sizeof(struct nsminfo)+size);
99 if (!nsmpn)
100 return NULL;
101
ed24756c 102 nsmpools[poolid].size+=size-nsmpn->size;
48005496
CP
103 nsmpn->size=size;
104
105 /* always set as we have a sentinel */
271ef2d2 106 nsmpn->prev->next=nsmpn;
48005496
CP
107
108 if (nsmpn->next)
271ef2d2 109 nsmpn->next->prev=nsmpn;
48005496
CP
110
111 return (void *)nsmpn->data;
34da4416 112}
113
114void nsfreeall(unsigned int poolid) {
115 struct nsminfo *nsmp, *nnsmp;
116
117 if (poolid >= MAXPOOL)
118 return;
119
ed24756c 120 for (nsmp=nsmpools[poolid].first.next;nsmp;nsmp=nnsmp) {
34da4416 121 nnsmp=nsmp->next;
122 free(nsmp);
123 }
124
ed24756c
CP
125 nsmpools[poolid].first.next=NULL;
126 nsmpools[poolid].size=0;
127 nsmpools[poolid].count=0;
34da4416 128}
129
f8d5cfcf
CP
130void nscheckfreeall(unsigned int poolid) {
131 if (poolid >= MAXPOOL)
132 return;
133
ed24756c 134 if (nsmpools[poolid].first.next) {
a05832ea 135 Error("core",ERR_INFO,"nsmalloc: Blocks still allocated in pool #%d (%s): %zub, %lu items",poolid,nsmpoolnames[poolid]?nsmpoolnames[poolid]:"??",nsmpools[poolid].size,nsmpools[poolid].count);
f8d5cfcf
CP
136 nsfreeall(poolid);
137 }
138}
139
19f37c5c 140void nsexit(void) {
103521a1 141 unsigned int i;
142
f8d5cfcf 143 for (i=0;i<MAXPOOL;i++)
88c8f330 144 nsfreeall(i);
103521a1 145}
146
cd871a9c
CP
147#else
148
cd871a9c
CP
149void *nsmalloc(unsigned int poolid, size_t size) {
150 return malloc(size);
151}
152
f358bcd6
CP
153void *nscalloc(unsigned int poolid, size_t nmemb, size_t size) {
154 return calloc(nmemb, size);
155}
156
cd871a9c
CP
157void *nsrealloc(unsigned int poolid, void *ptr, size_t size) {
158 return realloc(ptr, size);
159}
160
161void nsfree(unsigned int poolid, void *ptr) {
7b74d6e6
CP
162 if(ptr)
163 free(ptr);
cd871a9c
CP
164}
165
166void nsfreeall(unsigned int poolid) {
167}
168
169void nsexit(void) {
170}
171
172void nscheckfreeall(unsigned int poolid) {
173}
174
175#endif
176