]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nickalloc.c
Mergey merge.
[irc/quakenet/newserv.git] / nick / nickalloc.c
CommitLineData
47657339 1/* nick/host/realname/authname allocator */
c86edd1d
Q
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
12nick *freenicks;
13host *freehosts;
14
15void initnickalloc() {
16 freenicks=NULL;
17 freehosts=NULL;
18
19 assert(sizeof(host)==sizeof(realname));
20}
21
22realname *newrealname() {
23 return (realname *)newhost();
24}
25
26void freerealname(realname *rn) {
27 freehost((host *)rn);
28}
29
30nick *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
48void freenick (nick *np) {
49 np->next=freenicks;
50 freenicks=np;
51}
52
53host *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
71void freehost (host *hp) {
72 hp->next=freehosts;
73 freehosts=hp;
74}
47657339 75