chiark / gitweb /
Document new interrogation macros. Fix text a bit.
[mLib] / dspool.h
1 /* -*-c-*-
2  *
3  * $Id: dspool.h,v 1.2 1999/12/10 23:42:04 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 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: dspool.h,v $
32  * Revision 1.2  1999/12/10 23:42:04  mdw
33  * Change header file guard names.
34  *
35  * Revision 1.1  1999/05/21 22:15:26  mdw
36  * Dynamic string pool system, based on an idea from the `sw-tools'
37  * project.  Could do with more work to make it really good.
38  *
39  */
40
41 #ifndef MLIB_DSPOOL_H
42 #define MLIB_DSPOOL_H
43
44 #ifdef __cplusplus
45   extern "C" {
46 #endif
47
48 /*----- Header files ------------------------------------------------------*/
49
50 #include "dstr.h"
51 #include "sub.h"
52
53 /*----- Data structures ---------------------------------------------------*/
54
55 typedef struct dspoolstr {
56   dstr ds;
57   struct dspoolstr *next;
58 } dspoolstr;
59
60 typedef struct dspool {
61   dspoolstr *free;
62   size_t isz;
63 } dspool;
64
65 /*----- Functions provided ------------------------------------------------*/
66
67 /* --- @dspool_create@ --- *
68  *
69  * Arguments:   @dspool *p@ = address of pool to create
70  *              @size_t isz@ = initial size of new strings
71  *
72  * Returns:     ---
73  *
74  * Use:         Initializes a dynamic string pool.
75  */
76
77 extern void dspool_create(dspool */*p*/, size_t /*isz*/);
78
79 /* --- @dspool_destroy@ --- *
80  *
81  * Arguments:   @dspool *p@ = pool to destroy
82  *
83  * Returns:     ---
84  *
85  * Use:         Releases all of the strings left in the pool.  Any strings
86  *              not put back into the pool aren't freed.  However, the pool
87  *              is still valid, and the active strings can be put back and
88  *              released later.
89  */
90
91 extern void dspool_destroy(dspool */*p*/);
92
93 /* --- @dspool_get@ --- *
94  *
95  * Arguments:   @dspool *p@ = pointer to a string pool
96  *
97  * Returns:     Pointer to a dynamic string.
98  *
99  * Use:         Fetches a string from the pool.  The string has space for at
100  *              least @isz@ characters (where @isz@ is the size passed to
101  *              @dspool_create@ for the pool).
102  */
103
104 extern dstr *dspool_get(dspool */*p*/);
105
106 #define DSGET(p, d) do {                                                \
107   dspoolstr *_s;                                                        \
108   dspool *_p = (p);                                                     \
109   if (_p->free) {                                                       \
110     _s = _p->free;                                                      \
111     _p->free = _s->next;                                                \
112   } else {                                                              \
113     _s = CREATE(dspoolstr);                                             \
114     DCREATE(&_s->ds);                                                   \
115     if (_p->isz)                                                        \
116       DENSURE(&_s->ds, _p->isz);                                        \
117   }                                                                     \
118   d = &_s->ds;                                                          \
119 } while (0)
120
121 /* --- @dspool_put@ --- *
122  *
123  * Arguments:   @dspool *p@ = pointer to a string pool
124  *              @dstr *d@ = pointer to a dynamic string from a string pool
125  *
126  * Returns:     ---
127  *
128  * Use:         Releases a dynamic string back into a string pool.  It
129  *              doesn't have to be the same pool the string actually came
130  *              from, although it does have to have come from some string
131  *              pool.
132  */
133
134 extern void dspool_put(dspool */*p*/, dstr */*d*/);
135
136 #define DSPUT(p, d) do {                                                \
137   dspool *_p = (p);                                                     \
138   dspoolstr *_s = (dspoolstr *)(d);                                     \
139   DRESET(d);                                                            \
140   _s->next = _p->free;                                                  \
141   _p->free = _s;                                                        \
142 } while (0)
143
144 /*----- That's all, folks -------------------------------------------------*/
145
146 #ifdef __cplusplus
147   }
148 #endif
149
150 #endif