chiark / gitweb /
buf: Fix two embarassing bugs found while writing Lisp bindings.
[mLib] / dspool.h
1 /* -*-c-*-
2  *
3  * $Id: dspool.h,v 1.3 2004/04/08 01:36:11 mdw Exp $
4  *
5  * Provide pools of strings
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #ifndef MLIB_DSPOOL_H
30 #define MLIB_DSPOOL_H
31
32 #ifdef __cplusplus
33   extern "C" {
34 #endif
35
36 /*----- Header files ------------------------------------------------------*/
37
38 #include "dstr.h"
39 #include "sub.h"
40
41 /*----- Data structures ---------------------------------------------------*/
42
43 typedef struct dspoolstr {
44   dstr ds;
45   struct dspoolstr *next;
46 } dspoolstr;
47
48 typedef struct dspool {
49   dspoolstr *free;
50   size_t isz;
51 } dspool;
52
53 /*----- Functions provided ------------------------------------------------*/
54
55 /* --- @dspool_create@ --- *
56  *
57  * Arguments:   @dspool *p@ = address of pool to create
58  *              @size_t isz@ = initial size of new strings
59  *
60  * Returns:     ---
61  *
62  * Use:         Initializes a dynamic string pool.
63  */
64
65 extern void dspool_create(dspool */*p*/, size_t /*isz*/);
66
67 /* --- @dspool_destroy@ --- *
68  *
69  * Arguments:   @dspool *p@ = pool to destroy
70  *
71  * Returns:     ---
72  *
73  * Use:         Releases all of the strings left in the pool.  Any strings
74  *              not put back into the pool aren't freed.  However, the pool
75  *              is still valid, and the active strings can be put back and
76  *              released later.
77  */
78
79 extern void dspool_destroy(dspool */*p*/);
80
81 /* --- @dspool_get@ --- *
82  *
83  * Arguments:   @dspool *p@ = pointer to a string pool
84  *
85  * Returns:     Pointer to a dynamic string.
86  *
87  * Use:         Fetches a string from the pool.  The string has space for at
88  *              least @isz@ characters (where @isz@ is the size passed to
89  *              @dspool_create@ for the pool).
90  */
91
92 extern dstr *dspool_get(dspool */*p*/);
93
94 #define DSGET(p, d) do {                                                \
95   dspoolstr *_s;                                                        \
96   dspool *_p = (p);                                                     \
97   if (_p->free) {                                                       \
98     _s = _p->free;                                                      \
99     _p->free = _s->next;                                                \
100   } else {                                                              \
101     _s = CREATE(dspoolstr);                                             \
102     DCREATE(&_s->ds);                                                   \
103     if (_p->isz)                                                        \
104       DENSURE(&_s->ds, _p->isz);                                        \
105   }                                                                     \
106   d = &_s->ds;                                                          \
107 } while (0)
108
109 /* --- @dspool_put@ --- *
110  *
111  * Arguments:   @dspool *p@ = pointer to a string pool
112  *              @dstr *d@ = pointer to a dynamic string from a string pool
113  *
114  * Returns:     ---
115  *
116  * Use:         Releases a dynamic string back into a string pool.  It
117  *              doesn't have to be the same pool the string actually came
118  *              from, although it does have to have come from some string
119  *              pool.
120  */
121
122 extern void dspool_put(dspool */*p*/, dstr */*d*/);
123
124 #define DSPUT(p, d) do {                                                \
125   dspool *_p = (p);                                                     \
126   dspoolstr *_s = (dspoolstr *)(d);                                     \
127   DRESET(d);                                                            \
128   _s->next = _p->free;                                                  \
129   _p->free = _s;                                                        \
130 } while (0)
131
132 /*----- That's all, folks -------------------------------------------------*/
133
134 #ifdef __cplusplus
135   }
136 #endif
137
138 #endif