]> jfr.im git - irc/quakenet/newserv.git/blame - core/nsmalloc.c
merge
[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"
271ef2d2
CP
6#define __NSMALLOC_C
7#undef __NSMALLOC_H
8#include "nsmalloc.h"
9
19f37c5c 10#include "../core/hooks.h"
34da4416 11#include "../core/error.h"
12
ed24756c 13struct nsmpool nsmpools[MAXPOOL];
34da4416 14
707c5824 15#ifndef USE_NSMALLOC_VALGRIND
19f37c5c 16
34da4416 17void *nsmalloc(unsigned int poolid, size_t size) {
18 struct nsminfo *nsmp;
19
20 if (poolid >= MAXPOOL)
21 return NULL;
22
20d694a5 23 /* Allocate enough for the structure and the required data */
24 nsmp=(struct nsminfo *)malloc(sizeof(struct nsminfo)+size);
34da4416 25
26 if (!nsmp)
27 return NULL;
28
48005496 29 nsmp->size=size;
ed24756c
CP
30 nsmpools[poolid].size+=size;
31 nsmpools[poolid].count++;
48005496 32
ed24756c
CP
33 nsmp->next=nsmpools[poolid].first.next;
34 nsmp->prev=&nsmpools[poolid].first;
35 if (nsmpools[poolid].first.next)
36 nsmpools[poolid].first.next->prev=nsmp;
37 nsmpools[poolid].first.next=nsmp;
48005496 38
34da4416 39 return (void *)nsmp->data;
40}
41
48005496 42/* we dump core on ptr == NULL */
34da4416 43void nsfree(unsigned int poolid, void *ptr) {
48005496 44 struct nsminfo *nsmp;
34da4416 45
7b74d6e6 46 if (!ptr || poolid >= MAXPOOL)
34da4416 47 return;
48005496
CP
48
49 /* evil */
50 nsmp=(struct nsminfo*)ptr - 1;
51
52 /* always set as we have a sentinel */
271ef2d2 53 nsmp->prev->next=nsmp->next;
48005496 54 if (nsmp->next)
271ef2d2 55 nsmp->next->prev=nsmp->prev;
48005496 56
ed24756c
CP
57 nsmpools[poolid].size-=nsmp->size;
58 nsmpools[poolid].count--;
48005496
CP
59
60 free(nsmp);
61 return;
62}
63
64void *nsrealloc(unsigned int poolid, void *ptr, size_t size) {
65 struct nsminfo *nsmp, *nsmpn;
66
67 if (ptr == NULL)
68 return nsmalloc(poolid, size);
69
70 if (size == 0) {
71 nsfree(poolid, ptr);
72 return NULL;
34da4416 73 }
48005496
CP
74
75 if (poolid >= MAXPOOL)
76 return NULL;
77
78 /* evil */
79 nsmp=(struct nsminfo *)ptr - 1;
80
81 if (size == nsmp->size)
82 return (void *)nsmp->data;
83
84 nsmpn=(struct nsminfo *)realloc(nsmp, sizeof(struct nsminfo)+size);
85 if (!nsmpn)
86 return NULL;
87
ed24756c 88 nsmpools[poolid].size+=size-nsmpn->size;
48005496
CP
89 nsmpn->size=size;
90
91 /* always set as we have a sentinel */
271ef2d2 92 nsmpn->prev->next=nsmpn;
48005496
CP
93
94 if (nsmpn->next)
271ef2d2 95 nsmpn->next->prev=nsmpn;
48005496
CP
96
97 return (void *)nsmpn->data;
34da4416 98}
99
100void nsfreeall(unsigned int poolid) {
101 struct nsminfo *nsmp, *nnsmp;
102
103 if (poolid >= MAXPOOL)
104 return;
105
ed24756c 106 for (nsmp=nsmpools[poolid].first.next;nsmp;nsmp=nnsmp) {
34da4416 107 nnsmp=nsmp->next;
108 free(nsmp);
109 }
110
ed24756c
CP
111 nsmpools[poolid].first.next=NULL;
112 nsmpools[poolid].size=0;
113 nsmpools[poolid].count=0;
34da4416 114}
115
f8d5cfcf
CP
116void nscheckfreeall(unsigned int poolid) {
117 if (poolid >= MAXPOOL)
118 return;
119
ed24756c
CP
120 if (nsmpools[poolid].first.next) {
121 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);
f8d5cfcf
CP
122 nsfreeall(poolid);
123 }
124}
125
19f37c5c 126void nsexit(void) {
103521a1 127 unsigned int i;
128
f8d5cfcf 129 for (i=0;i<MAXPOOL;i++)
88c8f330 130 nsfreeall(i);
103521a1 131}
132
cd871a9c
CP
133#else
134
cd871a9c
CP
135void *nsmalloc(unsigned int poolid, size_t size) {
136 return malloc(size);
137}
138
139void *nsrealloc(unsigned int poolid, void *ptr, size_t size) {
140 return realloc(ptr, size);
141}
142
143void nsfree(unsigned int poolid, void *ptr) {
7b74d6e6
CP
144 if(ptr)
145 free(ptr);
cd871a9c
CP
146}
147
148void nsfreeall(unsigned int poolid) {
149}
150
151void nsexit(void) {
152}
153
154void nscheckfreeall(unsigned int poolid) {
155}
156
157#endif
158