2 dbuf.c - Dynamic buffer implementation
3 version 1.1.1, April 11th, 2003
5 Copyright (c) 2002-2003 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? */
47 while (dbuf->len + size >= dbuf->alloc)
48 dbuf->alloc += dbuf->alloc;
50 if ((dbuf->buf = realloc(dbuf->buf, dbuf->alloc)) == NULL)
59 * Initialize the dbuf structure and
60 * allocate buffer to hold size bytes.
63 int dbuf_init(struct dbuf_s *dbuf, size_t size)
72 return ((dbuf->buf = malloc(dbuf->alloc)) != NULL);
77 * Allocate new dbuf structure on the heap
83 struct dbuf_s *dbuf_new(size_t size)
87 dbuf = (struct dbuf_s *)malloc(sizeof(struct dbuf_s));
89 if (dbuf_init(dbuf, size) == 0) {
99 * Set the buffer size. Buffer size can be only decreased.
102 int dbuf_set_size(struct dbuf_s *dbuf, size_t size)
104 assert(dbuf != NULL);
105 assert(dbuf->alloc != 0);
106 assert(size < dbuf->len);
108 if (size <= dbuf->len) {
118 * Append the buf to the end of the buffer.
121 int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t size)
123 assert(dbuf != NULL);
124 assert(dbuf->alloc != 0);
125 assert(dbuf->buf != NULL);
127 if (dbuf_expand(dbuf, size) != 0) {
128 memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, size);
138 * Add '\0' character at the end of the buffer without
139 * count it in the dbuf->len.
142 const char *dbuf_c_str(struct dbuf_s *dbuf)
144 assert(dbuf != NULL);
145 assert(dbuf->alloc != 0);
146 assert(dbuf->buf != NULL);
148 /* only if not already null terminated */
149 if (((char *)dbuf->buf)[dbuf->len] != '\0') {
150 dbuf_expand(dbuf, 1);
151 ((char *)dbuf->buf)[dbuf->len] = '\0';
159 * Get the buffer pointer.
162 const void *dbuf_get_buf(struct dbuf_s *dbuf)
164 assert(dbuf != NULL);
165 assert(dbuf->alloc != 0);
166 assert(dbuf->buf != NULL);
173 * Get the buffer size.
176 size_t dbuf_get_size(struct dbuf_s *dbuf)
178 assert(dbuf != NULL);
179 assert(dbuf->alloc != 0);
180 assert(dbuf->buf != NULL);
187 * Trim the allocated buffer to required size
190 int dbuf_trim(struct dbuf_s *dbuf)
194 assert(dbuf != NULL);
195 assert(dbuf->alloc != 0);
196 assert(dbuf->buf != NULL);
198 buf = realloc(dbuf->buf, dbuf->len);
201 dbuf->alloc = dbuf->len;
210 * Detach the buffer from dbuf structure.
211 * The dbuf structure can be reused by
217 void *dbuf_detach(struct dbuf_s *dbuf)
221 assert(dbuf != NULL);
222 assert(dbuf->alloc != 0);
223 assert(dbuf->buf != NULL);
235 * Destroy the dbuf structure and
239 void dbuf_destroy(struct dbuf_s *dbuf)
241 free(dbuf_detach(dbuf));
246 * Delete dbuf structure on the heap:
247 * destroy it and free the allocated space.
248 * The user's responsablity is not to use
249 * the pointer any more: the best think to do
250 * is to set the pointer to NULL value.
255 void dbuf_delete(struct dbuf_s *dbuf)