]> jfr.im git - irc/quakenet/newserv.git/blob - chanstats/chanstatsalloc.c
merge
[irc/quakenet/newserv.git] / chanstats / chanstatsalloc.c
1 /* Allocators for chanstats */
2
3 #include "chanstats.h"
4
5 #include <stdlib.h>
6
7 #define ALLOCUNIT 100
8
9 static void *malloclist;
10 static chanstats *freecs;
11
12 void *cstsmalloc(size_t size) {
13 void **mem;
14
15 /* Get the memory we want, with an extra four bytes for our pointer */
16 mem=(void **)malloc(size+sizeof(void *));
17
18 /* Set the first word to point at the last chunk we got */
19 *mem=malloclist;
20
21 /* Now set the "last chunk" pointer to the address of this one */
22 malloclist=(void *)mem;
23
24 /* Return the rest of the memory to the caller */
25 return (void *)(mem+1);
26 }
27
28 void cstsfreeall() {
29 void *vp,**vp2;
30
31 vp=malloclist;
32
33 while (vp!=NULL) {
34 vp2=(void **)vp;
35 vp=*vp2;
36 free((void *)vp2);
37 }
38 }
39
40 void initchanstatsalloc() {
41 malloclist=NULL;
42 freecs=NULL;
43 }
44
45 chanstats *getchanstats() {
46 chanstats *csp;
47 int i;
48
49 if (freecs==NULL) {
50 freecs=(chanstats *)cstsmalloc(ALLOCUNIT*sizeof(chanstats));
51 for(i=0;i<(ALLOCUNIT-1);i++) {
52 freecs[i].index=(chanindex *)&(freecs[i+1]);
53 }
54 freecs[ALLOCUNIT-1].index=NULL;
55 }
56
57 csp=freecs;
58 freecs=(chanstats *)csp->index;
59
60 return csp;
61 }
62
63 void freechanstats(chanstats *csp) {
64 csp->index=(chanindex *)freecs;
65 freecs=csp;
66 }