]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nickalloc.c
395aec0a751a19a063a5582a579ce4df753465f6
[irc/quakenet/newserv.git] / nick / nickalloc.c
1 /* nick/host/realname/authname allocator */
2
3 #include "nick.h"
4 #include <assert.h>
5 #include <stdlib.h>
6
7 #define ALLOCUNIT 100
8
9 /* Hosts and realname structures are the same size */
10 /* This assumption is checked in initnickalloc(); */
11
12 nick *freenicks;
13 host *freehosts;
14 authname *freeauthnames;
15
16 void initnickalloc() {
17 freenicks=NULL;
18 freehosts=NULL;
19 freeauthnames=NULL;
20
21 assert(sizeof(host)==sizeof(realname));
22 }
23
24 realname *newrealname() {
25 return (realname *)newhost();
26 }
27
28 void freerealname(realname *rn) {
29 freehost((host *)rn);
30 }
31
32 nick *newnick() {
33 nick *np;
34 int i;
35
36 if (freenicks==NULL) {
37 freenicks=(nick *)malloc(ALLOCUNIT*sizeof(nick));
38 for (i=0;i<(ALLOCUNIT-1);i++) {
39 freenicks[i].next=&(freenicks[i+1]);
40 }
41 freenicks[ALLOCUNIT-1].next=NULL;
42 }
43
44 np=freenicks;
45 freenicks=np->next;
46
47 return np;
48 }
49
50 void freenick (nick *np) {
51 np->next=freenicks;
52 freenicks=np;
53 }
54
55 host *newhost() {
56 host *nh;
57 int i;
58
59 if (freehosts==NULL) {
60 freehosts=(host *)malloc(ALLOCUNIT*sizeof(host));
61 for (i=0;i<(ALLOCUNIT-1);i++) {
62 freehosts[i].next=&(freehosts[i+1]);
63 }
64 freehosts[ALLOCUNIT-1].next=NULL;
65 }
66
67 nh=freehosts;
68 freehosts=nh->next;
69
70 return nh;
71 }
72
73 void freehost (host *hp) {
74 hp->next=freehosts;
75 freehosts=hp;
76 }
77
78 authname *newauthname() {
79 authname *anp;
80 int i;
81
82 if (freeauthnames==NULL) {
83 freeauthnames=(authname *)malloc(ALLOCUNIT*sizeof(authname));
84 for (i=0;i<(ALLOCUNIT-1);i++) {
85 freeauthnames[i].next=&(freeauthnames[i+1]);
86 }
87 freeauthnames[ALLOCUNIT-1].next=NULL;
88 }
89
90 anp=freeauthnames;
91 freeauthnames=anp->next;
92
93 return anp;
94 }
95
96 void freeauthname (authname *anp) {
97 anp->next=freeauthnames;
98 freeauthnames=anp;
99 }