/* */
This source file includes following definitions.
- pool_open
- pool_malloc
- pool_strdup
- pool_strdup_withterm
- pool_reset
- pool_close
1 /*
2 * Copyright (c) 2006 Tama Communications Corporation
3 *
4 * This file is part of GNU GLOBAL.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "checkalloc.h"
27 #include "pool.h"
28
29 /** @file
30
31 Pool: usage and memory status
32
33 @code
34 pool = pool_open(); [head]
35
36 memory = pool_malloc(pool, 10); [head] [..........]
37 memory = pool_malloc(pool, 10); [head] [..........][..........]
38 pool_reset(pool); [head] [++++++++++][++++++++++]
39 string = pool_strdup(pool, "12345", 0); [head] [12345]++++][++++++++++]
40 string = pool_strdup_withterm(pool, "12345:678", ':'); [head] [12345][12345]+++++++++]
41 (...: alloc, +++: free)
42 pool_close(pool); (nothing)
43 @endcode
44
45 */
46
47 #define obstack_chunk_alloc check_malloc
48 #define obstack_chunk_free free
49
50 /**
51 * pool_open: open memory pool
52 *
53 * @return pool #POOL structure
54 */
55 POOL *
56 pool_open(void)
57 {
58 POOL *pool = (POOL *)check_calloc(sizeof(POOL), 1);
59
60 obstack_init(&pool->obstack);
61 pool->first_object = obstack_alloc(&pool->obstack, 1);
62 return pool;
63 }
64 /**
65 * pool_malloc: allocate memory from pool
66 *
67 * @param[in] pool #POOL structure
68 * @param[in] size memory size
69 * @return allocated memory
70 */
71 void *
72 pool_malloc(POOL *pool, int size)
73 {
74 return obstack_alloc(&pool->obstack, size);
75 }
76 /**
77 * pool_strdup: memory pool version of @NAME{strdup()}
78 *
79 * @param[in] pool #POOL structure
80 * @param[in] string string
81 * @param[in] size
82 * @return allocated memory
83 */
84 char *
85 pool_strdup(POOL *pool, const char *string, int size)
86 {
87 if (size == 0)
88 size = strlen(string);
89 return obstack_copy0(&pool->obstack, string, size);
90 }
91 /**
92 * pool_strdup_withterm: memory pool version of @NAME{strdup()}
93 *
94 * @param[in] pool #POOL structure
95 * @param[in] string string
96 * @param[in] term terminate character
97 * @return allocated memory
98 */
99 char *
100 pool_strdup_withterm(POOL *pool, const char *string, int term)
101 {
102 const char *p = strchr(string, term);
103 int size = p ? p - string : strlen(string);
104 return obstack_copy0(&pool->obstack, string, size);
105 }
106 /**
107 * pool_reset: reset memory pool
108 *
109 * @param[in] pool #POOL structure
110 */
111 void
112 pool_reset(POOL *pool)
113 {
114 /*
115 * Free all memory in pool->obstack but leave it valid for further allocation.
116 */
117 obstack_free(&pool->obstack, pool->first_object);
118 }
119 /**
120 * pool_close: close memory pool
121 *
122 * @param[in] pool #POOL structure
123 */
124 void
125 pool_close(POOL *pool)
126 {
127 obstack_free(&pool->obstack, NULL);
128 free(pool);
129 }
/* */