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