FreeWRL/FreeX3D  3.0.0
cson_buffer Struct Reference

A generic buffer class. More...

#include <cson_amalgamation_core.h>

Data Fields

cson_size_t capacity
 The number of bytes allocated for this object. More...
 
cson_size_t used
 The number of bytes "used" by this object. More...
 
cson_size_t timesExpanded
 This is a debugging/metric-counting value intended to help certain malloc()-conscious clients tweak their memory reservation sizes. More...
 
unsigned char * mem
 The memory allocated for and owned by this buffer. More...
 

Detailed Description

A generic buffer class.

They can be used like this:

cson_buffer b = cson_buffer_empty;
int rc = cson_buffer_reserve( &buf, 100 );
if( 0 != rc ) { ... allocation error ... }
... use buf.mem ...
... then free it up ...
cson_buffer_reserve( &buf, 0 );

To take over ownership of a buffer's memory:

void * mem = b.mem;
// mem is b.capacity bytes long, but only b.used
// bytes of it has been "used" by the API.
b = cson_buffer_empty;

The memory now belongs to the caller and must eventually be free()d.

Definition at line 1826 of file cson_amalgamation_core.h.

Field Documentation

cson_size_t cson_buffer::capacity

The number of bytes allocated for this object.

Use cson_buffer_reserve() to change its value.

Definition at line 1832 of file cson_amalgamation_core.h.

unsigned char* cson_buffer::mem

The memory allocated for and owned by this buffer.

Use cson_buffer_reserve() to change its size or free it. To take over ownership, do:

void * myptr = buf.mem;
buf = cson_buffer_empty;

(You might also need to store buf.used and buf.capacity, depending on what you want to do with the memory.)

When doing so, the memory must eventually be passed to free() to deallocate it.

Definition at line 1868 of file cson_amalgamation_core.h.

cson_size_t cson_buffer::timesExpanded

This is a debugging/metric-counting value intended to help certain malloc()-conscious clients tweak their memory reservation sizes.

Each time cson_buffer_reserve() expands the buffer, it increments this value by 1.

Definition at line 1850 of file cson_amalgamation_core.h.

cson_size_t cson_buffer::used

The number of bytes "used" by this object.

It is not needed for all use cases, and management of this value (if needed) is up to the client. The cson_buffer public API does not use this member. The intention is that this can be used to track the length of strings which are allocated via cson_buffer, since they need an explicit length and/or null terminator.

Definition at line 1841 of file cson_amalgamation_core.h.


The documentation for this struct was generated from the following file: