1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_dbufbase.c - dynamicaly allocated buffer
6 (C) Copyright 2001-2004 by Pavel Pisa - Originator
7 (C) Copyright 2003-2004 by Frantisek Vacek - Originator
9 The uLan utilities library can be used, copied and modified under
11 - GPL - GNU General Public License
12 - LGPL - GNU Lesser General Public License
13 - MPL - Mozilla Public License
14 - and other licenses added by project originators
15 Code can be modified and re-distributed under any combination
16 of the above listed licenses. If contributor does not agree with
17 some of the licenses, he/she can delete appropriate line.
18 Warning, if you delete all lines, you are not allowed to
19 distribute source code and/or binaries utilizing code.
21 See files COPYING and README for details.
23 *******************************************************************/
25 #include "ul_utmalloc.h"
28 typedef unsigned char byte;
31 * ul_dbuff_init - init memory allocated for dynamic buffer
32 * @buf: buffer structure
33 * @flags: flags describing behaviour of the buffer
34 * only UL_DBUFF_IS_STATIC flag is supported.
35 * in this case buffer use unly static array sbuf
37 * Returns capacity of initialised buffer
39 int ul_dbuff_init(ul_dbuff_t *buf, int flags)
41 buf->capacity = UL_DBUFF_SLEN;
42 buf->data = buf->sbuff;
49 * ul_dbuff_destroy - frees all resources allocated by buf
50 * @buf: buffer structure
52 void ul_dbuff_destroy(ul_dbuff_t *buf)
54 ul_dbuff_prep(buf, 0);
58 * ul_dbuff_prep - sets a new len and capacity of the buffer
59 * @buf: buffer structure
60 * @new_len: new desired buffer length
62 * Returns new buffer length
64 int ul_dbuff_prep(ul_dbuff_t *buf, int new_len)
66 if(buf->flags & UL_DBUFF_IS_STATIC) {
67 if(!buf->data || !buf->capacity) {
68 buf->capacity = sizeof(buf->sbuff);
69 buf->data = buf->sbuff;
71 if(new_len > buf->capacity) {
76 if(new_len > UL_DBUFF_SLEN) {
77 if(new_len != buf->capacity){
78 if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
81 buf->data=malloc(new_len);
87 buf->capacity=new_len;
90 if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
92 buf->data = &(buf->sbuff[0]);
94 buf->capacity=UL_DBUFF_SLEN;
98 memset(buf->data,0,new_len);