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;
30 //-----------------------------------------------------------------
32 * ul_dbuff_init - init memory allocated for dynamic buffer
33 * @buf: buffer structure
34 * @flags: flags describing behaviour of the buffer
35 * only UL_DBUFF_IS_STATIC flag is supported.
36 * in this case buffer use unly static array sbuf
38 * Returns capacity of initialised buffer
40 int ul_dbuff_init(ul_dbuff_t *buf, int flags)
42 buf->capacity = UL_DBUFF_SLEN;
43 buf->data = buf->sbuff;
49 //-----------------------------------------------------------------
51 * ul_dbuff_destroy - frees all resources allocated by buf
52 * @buf: buffer structure
54 void ul_dbuff_destroy(ul_dbuff_t *buf)
56 ul_dbuff_prep(buf, 0);
59 //-----------------------------------------------------------------
61 * ul_dbuff_prep - sets a new len and capacity of the buffer
62 * @buf: buffer structure
63 * @new_len: new desired buffer length
65 * Returns new buffer length
67 int ul_dbuff_prep(ul_dbuff_t *buf, int new_len)
69 if(buf->flags & UL_DBUFF_IS_STATIC) {
70 if(!buf->data || !buf->capacity) {
71 buf->capacity = sizeof(buf->sbuff);
72 buf->data = buf->sbuff;
74 if(new_len > buf->capacity) {
79 if(new_len > UL_DBUFF_SLEN) {
80 if(new_len != buf->capacity){
81 if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
84 buf->data=malloc(new_len);
90 buf->capacity=new_len;
93 if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
95 buf->data = &(buf->sbuff[0]);
97 buf->capacity=UL_DBUFF_SLEN;
101 memset(buf->data,0,new_len);