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