chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / struct / dspool.c
1 /* -*-c-*-
2  *
3  * Provide pools of strings
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "dspool.h"
30 #include "dstr.h"
31 #include "sub.h"
32
33 /*----- Main code ---------------------------------------------------------*/
34
35 /* --- @dspool_create@ --- *
36  *
37  * Arguments:   @dspool *p@ = address of pool to create
38  *              @size_t isz@ = initial size of new strings
39  *
40  * Returns:     ---
41  *
42  * Use:         Initializes a dynamic string pool.
43  */
44
45 void dspool_create(dspool *p, size_t isz)
46 {
47   p->free = 0;
48   p->isz = isz;
49 }
50
51 /* --- @dspool_destroy@ --- *
52  *
53  * Arguments:   @dspool *p@ = pool to destroy
54  *
55  * Returns:     ---
56  *
57  * Use:         Releases all of the strings left in the pool.  Any strings
58  *              not put back into the pool aren't freed.  However, the pool
59  *              is still valid, and the active strings can be put back and
60  *              released later.
61  */
62
63 void dspool_destroy(dspool *p)
64 {
65   dspoolstr *s = p->free;
66   while (s) {
67     dspoolstr *n = s->next;
68     DDESTROY(&s->ds);
69     DESTROY(s);
70     s = n;
71   }
72   p->free = 0;
73 }
74
75 /* --- @dspool_get@ --- *
76  *
77  * Arguments:   @dspool *p@ = pointer to a string pool
78  *
79  * Returns:     Pointer to a dynamic string.
80  *
81  * Use:         Fetches a string from the pool.  The string has space for at
82  *              least @isz@ characters (where @isz@ is the size passed to
83  *              @dspool_create@ for the pool).
84  */
85
86 dstr *dspool_get(dspool *p) { dstr *d; DSGET(p, d); return (d); }
87
88 /* --- @dspool_put@ --- *
89  *
90  * Arguments:   @dspool *p@ = pointer to a string pool
91  *              @dstr *d@ = pointer to a dynamic string from a string pool
92  *
93  * Returns:     ---
94  *
95  * Use:         Releases a dynamic string back into a string pool.  It
96  *              doesn't have to be the same pool the string actually came
97  *              from, although it does have to have come from some string
98  *              pool.
99  */
100
101 void dspool_put(dspool *p, dstr *d) { DSPUT(p, d); }
102
103 /*----- That's all, folks -------------------------------------------------*/