Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
DynBuffer.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 ****************************************************************************
11 * HEADER
12 *
13 * $Id: DynBuffer.h 5978 2026-09-11 16:13:48Z wini $
14 *
15 * COPYRIGHT: Real Time Logic LLC, 2005-2008
16 *
17 * This software is copyrighted by and is the sole property of Real
18 * Time Logic LLC. All rights, title, ownership, or other interests in
19 * the software remain the property of Real Time Logic LLC. This
20 * software may only be used in accordance with the terms and
21 * conditions stipulated in the corresponding license agreement under
22 * which the software has been supplied. Any unauthorized use,
23 * duplication, transmission, distribution, or disclosure of this
24 * software is expressly forbidden.
25 *
26 * This Copyright notice may not be removed or modified without prior
27 * written consent of Real Time Logic LLC.
28 *
29 * Real Time Logic LLC. reserves the right to modify this software
30 * without notice.
31 *
32 * http://www.realtimelogic.com
33 ****************************************************************************
34 *
35 */
36
39#ifndef _DynBuffer_h
40#define _DynBuffer_h
41
42#include <HttpServer.h>
43#include <BaServerLib.h>
44
45/*
46 eCode:
47 -1: No allocator
48 -2: Malloc failed.
49 -3: Need to realloc buffer, but no realloc provided.
50 -4: Realloc failed.
51 -5: Buffer too large.
52*/
53struct DynBuffer;
60typedef void (*DynBuffer_OnAllocError)(struct DynBuffer* o, int eCode);
61
65typedef struct DynBuffer
66#ifdef __cplusplus
67: public BufPrint
68{
71
84 DynBuffer(int startSize, int expandSize, AllocatorIntf* alloc=0,
85 DynBuffer_OnAllocError onAllocError=0);
86
89 ~DynBuffer();
90
94 void release();
95
103 char* getBuf();
104
107 U32 getBufSize();
108
124 int getECode();
125
134 int expand(int sizeNeeded);
135
143 char* getCurPtr();
144
162 void incrementCursor(int nBytes);
163
169 static const char* ecode2str(int eCode);
170#else
171{
172 BufPrint super; /* inherits from BufPrint */
173#if 0
174}
175#endif
176#endif
177
178 AllocatorIntf* alloc;
179 DynBuffer_OnAllocError onAllocError;
180 int startSize;
181 int expandSize;
183
184#ifdef __cplusplus
185extern "C" {
186#endif
195BA_API void DynBuffer_constructor(DynBuffer* o, int startSize, int expandSize,
196 AllocatorIntf* alloc,
197 DynBuffer_OnAllocError onAllocError);
200#define DynBuffer_destructor(o) DynBuffer_release(o)
211#define DynBuffer_getBufSize(o) BufPrint_getBufSize((BufPrint*)(o))
215#define DynBuffer_getCurPtr(o) (((BufPrint*)(o))->buf+((BufPrint*)(o))->cursor)
219#define DynBuffer_incrementCursor(o,nBytes) ((BufPrint*)(o))->cursor+=nBytes
223#define DynBuffer_getECode(o) ((o)->expandSize < 0 ? (o)->expandSize : 0)
228BA_API int DynBuffer_expand(DynBuffer* o, int sizeNeeded);
232BA_API const char* DynBuffer_ecode2str(int eCode);
233#ifdef __cplusplus
234}
235
237 int startSize, int expandSize, AllocatorIntf* alloc,
238 DynBuffer_OnAllocError onAllocError) {
239 DynBuffer_constructor(this, startSize, expandSize, alloc, onAllocError); }
241 DynBuffer_destructor(this); }
242inline void DynBuffer::release() {
243 DynBuffer_release(this); }
244inline char* DynBuffer::getBuf() {
245 return DynBuffer_getBuf(this); }
247 return DynBuffer_getBufSize(this); }
249 return DynBuffer_getECode(this); }
250inline int DynBuffer::expand(int sizeNeeded) {
251 return DynBuffer_expand(this, sizeNeeded); }
252inline char* DynBuffer::getCurPtr() {
253 return DynBuffer_getCurPtr(this); }
254inline void DynBuffer::incrementCursor(int nBytes) {
255 DynBuffer_incrementCursor(this, nBytes); }
256inline const char* DynBuffer::ecode2str(int eCode) {
257 return DynBuffer_ecode2str(eCode); }
258#endif
259
260#endif
void(* DynBuffer_OnAllocError)(struct DynBuffer *o, int eCode)
Report an allocation error synchronously from a buffer operation.
Definition: DynBuffer.h:60
#define DynBuffer_getECode(o)
Read the allocation-error state.
Definition: DynBuffer.h:223
BA_API void DynBuffer_release(DynBuffer *o)
Release the current storage and invalidate borrowed pointers.
BA_API int DynBuffer_expand(DynBuffer *o, int sizeNeeded)
Reserve additional space without advancing the cursor.
#define DynBuffer_getBufSize(o)
Read the payload length.
Definition: DynBuffer.h:211
BA_API char * DynBuffer_getBuf(DynBuffer *o)
Terminate and obtain the current payload.
DynBuffer DynBuffer
A dynamic buffer.
BA_API void DynBuffer_constructor(DynBuffer *o, int startSize, int expandSize, AllocatorIntf *alloc, DynBuffer_OnAllocError onAllocError)
Initialize a dynamic buffer and optionally allocate its starting storage.
#define DynBuffer_incrementCursor(o, nBytes)
Advance after writing reserved storage.
Definition: DynBuffer.h:219
BA_API const char * DynBuffer_ecode2str(int eCode)
Translate an allocation error.
#define DynBuffer_getCurPtr(o)
Get the current write position.
Definition: DynBuffer.h:215
#define DynBuffer_destructor(o)
Release the current storage.
Definition: DynBuffer.h:200
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:93
Memory allocation and deallocation Interface class.
Definition: AllocatorIntf.h:98
The BufPrint class, which implements a compact printf-style formatter, is a base class used by severa...
Definition: BufPrint.h:132
A dynamic buffer.
Definition: DynBuffer.h:68
void release()
Free the current storage, reset the cursor and capacity, and invalidate all borrowed buffer pointers.
Definition: DynBuffer.h:242
~DynBuffer()
destructor.
Definition: DynBuffer.h:240
int getECode()
Returns the error code if memory allocation failed.
Definition: DynBuffer.h:248
U32 getBufSize()
Definition: DynBuffer.h:246
char * getBuf()
Returns a pointer to the internal buffer.
Definition: DynBuffer.h:244
int expand(int sizeNeeded)
force buffer to expand.
Definition: DynBuffer.h:250
void incrementCursor(int nBytes)
Increments the internal cursor position.
Definition: DynBuffer.h:254
char * getCurPtr()
Return a pointer to the internal cursor position in the internal dynamic buffer.
Definition: DynBuffer.h:252
static const char * ecode2str(int eCode)
Convert a recognized allocation error code to a static message.
Definition: DynBuffer.h:256
DynBuffer()
Leave storage uninitialized; call DynBuffer_constructor before use.
Definition: DynBuffer.h:70