2 dbuf.c - Dynamic buffer implementation
3 version 1.2.0, January 6th, 2007
5 Copyright (c) 2002-2007 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 * 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? */
50 dbuf->alloc += dbuf->alloc;
52 while (dbuf->len + size > dbuf->alloc);
54 if ((dbuf->buf = realloc(dbuf->buf, dbuf->alloc)) == NULL)
63 * Initialize the dbuf structure and
64 * allocate buffer to hold size bytes.
67 int dbuf_init(struct dbuf_s *dbuf, size_t size)
76 return ((dbuf->buf = malloc(dbuf->alloc)) != NULL);
81 * Allocate new dbuf structure on the heap
87 struct dbuf_s *dbuf_new(size_t size)
91 dbuf = (struct dbuf_s *)malloc(sizeof(struct dbuf_s));
93 if (dbuf_init(dbuf, size) == 0) {
103 * Set the buffer size. Buffer size can be only decreased.
106 int dbuf_set_length(struct dbuf_s *dbuf, size_t len)
108 assert(dbuf != NULL);
109 assert(dbuf->alloc != 0);
110 assert(len <= dbuf->len);
112 if (len <= dbuf->len) {
122 * Append the buf to the end of the buffer.
125 int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t len)
127 assert(dbuf != NULL);
128 assert(dbuf->alloc != 0);
129 assert(dbuf->buf != NULL);
131 if (_dbuf_expand(dbuf, len) != 0) {
132 memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, len);
142 * Add '\0' character at the end of the buffer without
143 * count it in the dbuf->len.
146 const char *dbuf_c_str(struct dbuf_s *dbuf)
148 assert(dbuf != NULL);
149 assert(dbuf->alloc != 0);
150 assert(dbuf->buf != NULL);
152 if (_dbuf_expand(dbuf, 1) != 0) {
153 ((char *)dbuf->buf)[dbuf->len] = '\0';
162 * Get the buffer pointer.
165 const void *dbuf_get_buf(struct dbuf_s *dbuf)
167 assert(dbuf != NULL);
168 assert(dbuf->alloc != 0);
169 assert(dbuf->buf != NULL);
176 * Get the buffer length.
179 size_t dbuf_get_length(struct dbuf_s *dbuf)
181 assert(dbuf != NULL);
182 assert(dbuf->alloc != 0);
183 assert(dbuf->buf != NULL);
190 * Trim the allocated buffer to required size
193 int dbuf_trim(struct dbuf_s *dbuf)
197 assert(dbuf != NULL);
198 assert(dbuf->alloc != 0);
199 assert(dbuf->buf != NULL);
201 buf = realloc(dbuf->buf, dbuf->len);
204 dbuf->alloc = dbuf->len;
213 * Detach the buffer from dbuf structure.
214 * The dbuf structure can be reused by
220 void *dbuf_detach(struct dbuf_s *dbuf)
224 assert(dbuf != NULL);
225 assert(dbuf->alloc != 0);
226 assert(dbuf->buf != NULL);
238 * Destroy the dbuf structure and
242 void dbuf_destroy(struct dbuf_s *dbuf)
244 free(dbuf_detach(dbuf));
249 * Delete dbuf structure on the heap:
250 * destroy it and free the allocated space.
251 * The user's responsablity is not to use
252 * the pointer any more: the best think to do
253 * is to set the pointer to NULL value.
258 void dbuf_delete(struct dbuf_s *dbuf)
266 * Free detached buffer.
271 void dbuf_free(const void *buf)