]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nickalloc.c
7425f31f1d579e80f4d8d019d3dab3d27ec1151c
[irc/quakenet/newserv.git] / nick / nickalloc.c
1 /* nick/host/realname/authname allocator */
2
3 #include "nick.h"
4 #include "../core/nsmalloc.h"
5
6 #include <assert.h>
7 #include <stdlib.h>
8
9 #define ALLOCUNIT 100
10
11 /* Hosts and realname structures are the same size */
12 /* This assumption is checked in initnickalloc(); */
13
14 nick *freenicks;
15 host *freehosts;
16
17 void initnickalloc() {
18 freenicks=NULL;
19 freehosts=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 *)nsmalloc(POOL_NICK,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 *)nsmalloc(POOL_NICK,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