struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / util / dbuf.c
blob3ae656090ed39ec9234f648592c8f6a00d8c0645
1 /*
2 dbuf.c - Dynamic buffer implementation
3 version 1.4.0, 2011-03-27
5 Copyright (c) 2002-2011 Borut Razem
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
23 Borut Razem
24 borut.razem@siol.net
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include "dbuf.h"
35 * Assure that the buffer is large enough to hold
36 * current length + size bytes; enlarge it if necessary.
38 * Intended for internal use.
41 int _dbuf_expand(struct dbuf_s *dbuf, size_t size)
43 assert(dbuf->alloc != 0);
44 assert(dbuf->buf != NULL);
46 if (dbuf->len + size > dbuf->alloc) {
47 /* new_allocated_size = current_allocated_size * 2^n */
48 /* can this be optimized? */
49 do {
50 dbuf->alloc += dbuf->alloc;
52 while (dbuf->len + size > dbuf->alloc);
54 if ((dbuf->buf = realloc(dbuf->buf, dbuf->alloc)) == NULL)
55 return 0;
58 return 1;
63 * Initialize the dbuf structure and
64 * allocate buffer to hold size bytes.
67 int dbuf_init(struct dbuf_s *dbuf, size_t size)
69 assert(size != 0);
71 if (size == 0)
72 size = 1;
74 dbuf->len = 0;
75 dbuf->alloc = size;
76 return ((dbuf->buf = malloc(dbuf->alloc)) != NULL);
81 * Test if dbuf is initialized.
82 * NOTE: the dbuf structure should be set
83 * to all zeroes at definition:
84 * struct dbuf_s dbuf = { 0 };
86 * See: dbuf_init()
89 int dbuf_is_initialized (struct dbuf_s *dbuf)
91 if (dbuf->buf == NULL) {
92 assert(dbuf->alloc == 0);
93 assert(dbuf->len == 0);
94 return 0;
96 else {
97 assert(dbuf->alloc != 0);
98 assert(dbuf->len >= 0 && dbuf->len <= dbuf->alloc);
99 return 1;
104 * Allocate new dbuf structure on the heap
105 * and initialize it.
107 * See: dbuf_delete()
110 struct dbuf_s *dbuf_new(size_t size)
112 struct dbuf_s *dbuf;
114 dbuf = (struct dbuf_s *)malloc(sizeof(struct dbuf_s));
115 if (dbuf != NULL) {
116 if (dbuf_init(dbuf, size) == 0) {
117 free(dbuf);
118 return NULL;
121 return dbuf;
126 * Set the buffer size. Buffer size can be only decreased.
129 int dbuf_set_length(struct dbuf_s *dbuf, size_t len)
131 if (!dbuf_is_initialized (dbuf))
132 dbuf_init (dbuf, len ? len : 1);
134 assert(dbuf != NULL);
135 assert(dbuf->alloc != 0);
136 assert(len <= dbuf->len);
138 if (len <= dbuf->len) {
139 dbuf->len = len;
140 return 1;
143 return 0;
148 * Append the buf to the end of the buffer.
151 int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t len)
153 assert(dbuf != NULL);
154 assert(dbuf->alloc != 0);
155 assert(dbuf->buf != NULL);
157 if (_dbuf_expand(dbuf, len) != 0) {
158 memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, len);
159 dbuf->len += len;
160 return 1;
163 return 0;
167 * Prepend the buf to the beginning of the buffer.
170 int dbuf_prepend(struct dbuf_s *dbuf, const void *buf, size_t len)
172 assert(dbuf);
173 assert(dbuf->alloc);
174 assert(dbuf->buf);
176 if (_dbuf_expand(dbuf, len) != 0) {
177 memmove(&(((char *)dbuf->buf)[len]), dbuf->buf, dbuf->len);
178 memcpy(dbuf->buf, buf, len);
179 dbuf->len += len;
180 return 1;
183 return 0;
187 * Add '\0' character at the end of the buffer without
188 * count it in the dbuf->len.
191 const char *dbuf_c_str(struct dbuf_s *dbuf)
193 assert(dbuf != NULL);
194 assert(dbuf->alloc != 0);
195 assert(dbuf->buf != NULL);
197 if (_dbuf_expand(dbuf, 1) != 0) {
198 ((char *)dbuf->buf)[dbuf->len] = '\0';
199 return dbuf->buf;
202 return NULL;
207 * Get the buffer pointer.
210 const void *dbuf_get_buf(const struct dbuf_s *dbuf)
212 assert(dbuf != NULL);
213 assert(dbuf->alloc != 0);
214 assert(dbuf->buf != NULL);
216 return dbuf->buf;
221 * Get the buffer length.
224 size_t dbuf_get_length(const struct dbuf_s *dbuf)
226 assert(dbuf != NULL);
227 assert(dbuf->alloc != 0);
228 assert(dbuf->buf != NULL);
230 return dbuf->len;
235 * Trim the allocated buffer to required size
238 int dbuf_trim(struct dbuf_s *dbuf)
240 void *buf;
242 assert(dbuf != NULL);
243 assert(dbuf->alloc != 0);
244 assert(dbuf->buf != NULL);
246 buf = realloc(dbuf->buf, dbuf->len);
248 if (buf != NULL) {
249 dbuf->alloc = dbuf->len;
250 dbuf->buf = buf;
253 return buf != NULL;
258 * Detach the buffer from dbuf structure.
259 * The dbuf structure can be reused by
260 * reinitializing it.
262 * See: dbuf_init()
265 void *dbuf_detach(struct dbuf_s *dbuf)
267 void *ret;
269 assert(dbuf != NULL);
270 assert(dbuf->alloc != 0);
271 assert(dbuf->buf != NULL);
273 ret = dbuf->buf;
274 dbuf->buf = NULL;
275 dbuf->len = 0;
276 dbuf->alloc = 0;
278 return ret;
283 * Add '\0' character at the end of the buffer without
284 * count it in the dbuf->len and detach the buffer from dbuf structure.
285 * The dbuf structure can be reused by reinitializing it.
287 * See: dbuf_init()
290 char *dbuf_detach_c_str(struct dbuf_s *dbuf)
292 dbuf_c_str(dbuf);
293 return dbuf_detach(dbuf);
298 * Destroy the dbuf structure and
299 * free the buffer
302 void dbuf_destroy(struct dbuf_s *dbuf)
304 free(dbuf_detach(dbuf));
309 * Delete dbuf structure on the heap:
310 * destroy it and free the allocated space.
311 * The user's responsablity is not to use
312 * the pointer any more: the best think to do
313 * is to set the pointer to NULL value.
315 * See dbuf_new()
318 void dbuf_delete(struct dbuf_s *dbuf)
320 dbuf_destroy(dbuf);
321 free(dbuf);
326 * Free detached buffer.
328 * See dbuf_detach()
331 void dbuf_free(const void *buf)
333 free((void *)buf);