]> git.sur5r.net Git - openldap/blob - build/unproto/tok_pool.c
ITS#1991 fix + use struct berval
[openldap] / build / unproto / tok_pool.c
1 /*++
2 /* NAME
3 /*      tok_pool 3
4 /* SUMMARY
5 /*      maintain pool of unused token structures
6 /* PACKAGE
7 /*      unproto
8 /* SYNOPSIS
9 /*      #include "token.h"
10 /*
11 /*      struct token *tok_alloc()
12 /*
13 /*      void tok_free(t)
14 /*      struct token *t;
15 /* DESCRIPTION
16 /*      tok_alloc() and tok_free() maintain a pool of unused token
17 /*      structures.
18 /*
19 /*      tok_alloc() takes the first free token structure from the pool
20 /*      or allocates a new one if the pool is empty.
21 /*
22 /*      tok_free() adds a (possibly composite) token structure to the pool.
23 /* BUGS
24 /*      The pool never shrinks.
25 /* AUTHOR(S)
26 /*      Wietse Venema
27 /*      Eindhoven University of Technology
28 /*      Department of Mathematics and Computer Science
29 /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
30 /* LAST MODIFICATION
31 /*      92/01/15 21:53:04
32 /* VERSION/RELEASE
33 /*      1.2
34 /*--*/
35
36 static char pool_sccsid[] = "@(#) tok_pool.c 1.2 92/01/15 21:53:04";
37
38 /* C library */
39
40 extern char *malloc();
41
42 /* Application-specific stuff */
43
44 #include "token.h"
45 #include "vstring.h"
46 #include "error.h"
47
48 #define TOKLEN  5                       /* initial string buffer length */
49
50 struct token *tok_pool = 0;             /* free token pool */
51
52 /* tok_alloc - allocate token structure from pool or heap */
53
54 struct token *tok_alloc()
55 {
56     register struct token *t;
57
58     if (tok_pool) {                             /* re-use an old one */
59         t = tok_pool;
60         tok_pool = t->next;
61     } else {                                    /* create a new one */
62         if ((t = (struct token *) malloc(sizeof(struct token))) == 0
63             || (t->vstr = vs_alloc(TOKLEN)) == 0)
64             fatal("out of memory");
65     }
66     t->next = t->head = t->tail = 0;
67 #ifdef  DEBUG
68     strcpy(t->vstr->str, "BUSY");
69 #endif
70     return (t);
71 }
72
73 /* tok_free - return (possibly composite) token to pool of free tokens */
74
75 void    tok_free(t)
76 register struct token *t;
77 {
78 #ifdef DEBUG
79     /* Check if we are freeing free token */
80
81     register struct token *p;
82
83     for (p = tok_pool; p; p = p->next)
84         if (p == t)
85             fatal("freeing free token");
86 #endif
87
88     /* Free neighbours and subordinates first */
89
90     if (t->next)
91         tok_free(t->next);
92     if (t->head)
93         tok_free(t->head);
94
95     /* Free self */
96
97     t->next = tok_pool;
98     t->head = t->tail = 0;
99     tok_pool = t;
100 #ifdef  DEBUG
101     strcpy(t->vstr->str, "FREE");
102 #endif
103 }