]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nickalloc.c
Initial Import
[irc/quakenet/newserv.git] / nick / nickalloc.c
1 /* nick/host/realname 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
15 void initnickalloc() {
16 freenicks=NULL;
17 freehosts=NULL;
18
19 assert(sizeof(host)==sizeof(realname));
20 }
21
22 realname *newrealname() {
23 return (realname *)newhost();
24 }
25
26 void freerealname(realname *rn) {
27 freehost((host *)rn);
28 }
29
30 nick *newnick() {
31 nick *np;
32 int i;
33
34 if (freenicks==NULL) {
35 freenicks=(nick *)malloc(ALLOCUNIT*sizeof(nick));
36 for (i=0;i<(ALLOCUNIT-1);i++) {
37 freenicks[i].next=&(freenicks[i+1]);
38 }
39 freenicks[ALLOCUNIT-1].next=NULL;
40 }
41
42 np=freenicks;
43 freenicks=np->next;
44
45 return np;
46 }
47
48 void freenick (nick *np) {
49 np->next=freenicks;
50 freenicks=np;
51 }
52
53 host *newhost() {
54 host *nh;
55 int i;
56
57 if (freehosts==NULL) {
58 freehosts=(host *)malloc(ALLOCUNIT*sizeof(host));
59 for (i=0;i<(ALLOCUNIT-1);i++) {
60 freehosts[i].next=&(freehosts[i+1]);
61 }
62 freehosts[ALLOCUNIT-1].next=NULL;
63 }
64
65 nh=freehosts;
66 freehosts=nh->next;
67
68 return nh;
69 }
70
71 void freehost (host *hp) {
72 hp->next=freehosts;
73 freehosts=hp;
74 }