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