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