]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nickalloc.c
Make valgrind a bit happier.
[irc/quakenet/newserv.git] / nick / nickalloc.c
CommitLineData
47657339 1/* nick/host/realname/authname allocator */
c86edd1d
Q
2
3#include "nick.h"
103521a1 4#include "../core/nsmalloc.h"
5
c86edd1d
Q
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
14nick *freenicks;
15host *freehosts;
16
17void initnickalloc() {
18 freenicks=NULL;
19 freehosts=NULL;
20
21 assert(sizeof(host)==sizeof(realname));
22}
23
24realname *newrealname() {
25 return (realname *)newhost();
26}
27
28void freerealname(realname *rn) {
29 freehost((host *)rn);
30}
31
32nick *newnick() {
33 nick *np;
34 int i;
35
36 if (freenicks==NULL) {
103521a1 37 freenicks=(nick *)nsmalloc(POOL_NICK,ALLOCUNIT*sizeof(nick));
c86edd1d
Q
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
50void freenick (nick *np) {
51 np->next=freenicks;
52 freenicks=np;
53}
54
55host *newhost() {
56 host *nh;
57 int i;
58
59 if (freehosts==NULL) {
103521a1 60 freehosts=(host *)nsmalloc(POOL_NICK,ALLOCUNIT*sizeof(host));
c86edd1d
Q
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
73void freehost (host *hp) {
74 hp->next=freehosts;
75 freehosts=hp;
76}
47657339 77