2 dbuf.c - Dynamic buffer implementation
3 version 1.1.2, May 22th, 2006
5 Copyright (c) 2002-2006 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.
35 * Assure that the buffer is large enough to hold
36 * size bytes; enlarge it if necessary.
39 static int dbuf_expand(struct dbuf_s *dbuf, size_t size)
41 assert(dbuf->alloc != 0);
42 assert(dbuf->buf != NULL);
44 if (dbuf->len + size > dbuf->alloc) {
45 /* new_allocated_size = current_allocated_size * 2^n */
46 /* can this be optimized? */
48 dbuf->alloc += dbuf->alloc;
50 while (dbuf->len + size > dbuf->alloc);
52 if ((dbuf->buf = realloc(dbuf->buf, dbuf->alloc)) == NULL)
61 * Initialize the dbuf structure and
62 * allocate buffer to hold size bytes.
65 int dbuf_init(struct dbuf_s *dbuf, size_t size)
74 return ((dbuf->buf = malloc(dbuf->alloc)) != NULL);
79 * Allocate new dbuf structure on the heap
85 struct dbuf_s *dbuf_new(size_t size)
89 dbuf = (struct dbuf_s *)malloc(sizeof(struct dbuf_s));
91 if (dbuf_init(dbuf, size) == 0) {
101 * Set the buffer size. Buffer size can be only decreased.
104 int dbuf_set_size(struct dbuf_s *dbuf, size_t size)
106 assert(dbuf != NULL);
107 assert(dbuf->alloc != 0);
108 assert(size < dbuf->len);
110 if (size <= dbuf->len) {
120 * Append the buf to the end of the buffer.
123 int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t size)
125 assert(dbuf != NULL);
126 assert(dbuf->alloc != 0);
127 assert(dbuf->buf != NULL);
129 if (dbuf_expand(dbuf, size) != 0) {
130 memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, size);
140 * Add '\0' character at the end of the buffer without
141 * count it in the dbuf->len.
144 const char *dbuf_c_str(struct dbuf_s *dbuf)
146 assert(dbuf != NULL);
147 assert(dbuf->alloc != 0);
148 assert(dbuf->buf != NULL);
150 dbuf_expand(dbuf, 1);
151 ((char *)dbuf->buf)[dbuf->len] = '\0';
158 * Get the buffer pointer.
161 const void *dbuf_get_buf(struct dbuf_s *dbuf)
163 assert(dbuf != NULL);
164 assert(dbuf->alloc != 0);
165 assert(dbuf->buf != NULL);
172 * Get the buffer size.
175 size_t dbuf_get_size(struct dbuf_s *dbuf)
177 assert(dbuf != NULL);
178 assert(dbuf->alloc != 0);
179 assert(dbuf->buf != NULL);
186 * Trim the allocated buffer to required size
189 int dbuf_trim(struct dbuf_s *dbuf)
193 assert(dbuf != NULL);
194 assert(dbuf->alloc != 0);
195 assert(dbuf->buf != NULL);
197 buf = realloc(dbuf->buf, dbuf->len);
200 dbuf->alloc = dbuf->len;
209 * Detach the buffer from dbuf structure.
210 * The dbuf structure can be reused by
216 void *dbuf_detach(struct dbuf_s *dbuf)
220 assert(dbuf != NULL);
221 assert(dbuf->alloc != 0);
222 assert(dbuf->buf != NULL);
234 * Destroy the dbuf structure and
238 void dbuf_destroy(struct dbuf_s *dbuf)
240 free(dbuf_detach(dbuf));
245 * Delete dbuf structure on the heap:
246 * destroy it and free the allocated space.
247 * The user's responsablity is not to use
248 * the pointer any more: the best think to do
249 * is to set the pointer to NULL value.
254 void dbuf_delete(struct dbuf_s *dbuf)