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.
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 * 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 };
89 int dbuf_is_initialized (struct dbuf_s
*dbuf
)
91 if (dbuf
->buf
== NULL
) {
92 assert(dbuf
->alloc
== 0);
93 assert(dbuf
->len
== 0);
97 assert(dbuf
->alloc
!= 0);
98 assert(dbuf
->len
>= 0 && dbuf
->len
<= dbuf
->alloc
);
104 * Allocate new dbuf structure on the heap
110 struct dbuf_s
*dbuf_new(size_t size
)
114 dbuf
= (struct dbuf_s
*)malloc(sizeof(struct dbuf_s
));
116 if (dbuf_init(dbuf
, size
) == 0) {
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
) {
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
);
167 * Prepend the buf to the beginning of the buffer.
170 int dbuf_prepend(struct dbuf_s
*dbuf
, const void *buf
, size_t len
)
176 if (_dbuf_expand(dbuf
, len
) != 0) {
177 memmove(&(((char *)dbuf
->buf
)[len
]), dbuf
->buf
, dbuf
->len
);
178 memcpy(dbuf
->buf
, buf
, len
);
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';
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
);
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
);
235 * Trim the allocated buffer to required size
238 int dbuf_trim(struct dbuf_s
*dbuf
)
242 assert(dbuf
!= NULL
);
243 assert(dbuf
->alloc
!= 0);
244 assert(dbuf
->buf
!= NULL
);
246 buf
= realloc(dbuf
->buf
, dbuf
->len
);
249 dbuf
->alloc
= dbuf
->len
;
258 * Detach the buffer from dbuf structure.
259 * The dbuf structure can be reused by
265 void *dbuf_detach(struct dbuf_s
*dbuf
)
269 assert(dbuf
!= NULL
);
270 assert(dbuf
->alloc
!= 0);
271 assert(dbuf
->buf
!= NULL
);
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.
290 char *dbuf_detach_c_str(struct dbuf_s
*dbuf
)
293 return dbuf_detach(dbuf
);
298 * Destroy the dbuf structure and
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.
318 void dbuf_delete(struct dbuf_s
*dbuf
)
326 * Free detached buffer.
331 void dbuf_free(const void *buf
)