libgslutil  1.2.3
Functions
Serialised memory buffer functions

Functions

struct membuf_t * membuf_new (void))
 Allocate a new memory buffer.
 
struct membuf_t * membuf_realloc (struct membuf_t *old))
 Allocate new buffer or clear existing buffer.
 
void membuf_use (const struct membuf_t *m, const char **buf, uint32_t *len))
 Get details of memory buffer. More...
 
void membuf_free (struct membuf_t *m)
 Free used memory buffer.
 
void membuf_consume (struct membuf_t *m, uint32_t amt))
 Mark some bytes in buffer as consumed. More...
 
void membuf_truncate (struct membuf_t *m, uint32_t new_len))
 Truncate the memory buffer. More...
 
void writem_i8 (struct membuf_t *m, uint8_t i))
 Write integer to memory buffer (8-bit). More...
 
void writem_i16 (struct membuf_t *m, uint16_t i))
 Write integer to memory buffer (16-bit).
 
void writem_i24 (struct membuf_t *m, uint32_t i))
 Write integer to memory buffer (24-bit).
 
void writem_i32 (struct membuf_t *m, uint32_t i))
 Write integer to memory buffer (32-bit).
 
void writem_i64 (struct membuf_t *m, uint64_t i))
 Write integer to memory buffer (64-bit).
 
void writem_f32 (struct membuf_t *m, float f))
 Write 32-bit IEEE float to memory buffer.
 
void writem_f64 (struct membuf_t *m, double f))
 Write 64-bit IEEE float to memory buffer.
 
void writem_str (struct membuf_t *m, const char *str))
 Write null-terminated string to memory buffer. More...
 
void writem_mem (struct membuf_t *m, const char *mem, uint32_t amt))
 Write raw data to memory buffer. More...
 
int readm_i8 (struct membuf_t *m, uint8_t *out))
 Read 8-bit integer from memory buffer. More...
 
int readm_i16 (struct membuf_t *m, uint16_t *out))
 Read 16-bit integer from memory buffer.
 
int readm_i24 (struct membuf_t *m, uint32_t *out))
 Read 24-bit integer from memory buffer.
 
int readm_i24s (struct membuf_t *m, int32_t *out))
 Read 24-bit integer from memory buffer, and sign-extend it.
 
int readm_i32 (struct membuf_t *m, uint32_t *out))
 Read 32-bit integer from memory buffer.
 
int readm_i64 (struct membuf_t *m, uint64_t *out))
 Read 64-bit integer from memory buffer.
 
int readm_f32 (struct membuf_t *m, float *out))
 Read a 32-bit float from memory buffer.
 
int readm_f64 (struct membuf_t *m, double *out))
 Read a 64-bit float from memory buffer.
 
int readm_str (struct membuf_t *m, char *str, uint32_t str_size, uint32_t *len_out)))
 Read string from memory buffer. More...
 
int readm_mem (struct membuf_t *m, char *mem, uint32_t mem_size))
 Read bytes from memory buffer. More...
 
int readm_stra (struct membuf_t *m, char **str, uint32_t *amt)))
 Read string from memory buffer, allocating space for result.
 
int readm_mema (struct membuf_t *m, char **mem, uint32_t amt))
 Read bytes from memory buffer, allocating space for result.
 

Detailed Description

This set of functions is used to perform architecture-independent reads/writes from/to memory. An opaque type, struct membuf, is used. This must be allocated with membuf_new() or membuf_realloc() and freed with membuf_free().

The write functions will append to the buffer and automatically resize it as required (using safe_realloc()). The read functions will fail if there is not enough data in the buffer for the request to succeed; otherwise, the data will be removed from the buffer. This is implemented with efficient caching/resizing to minimise the number of calls to realloc() or memmove() that occur.

Function Documentation

◆ membuf_use()

void membuf_use ( const struct membuf_t *  m,
const char **  buf,
uint32_t *  len 
)

Get details of memory buffer.

Parameters
mMemory buffer object.
[out]bufPointer to data written here.
[out]lenLength of data in bytes written here.

Use this function once you have written some data into the memory buffer to get at the data. The returned values become invalid as soon as any more data is written into the buffer.

◆ membuf_consume()

void membuf_consume ( struct membuf_t *  m,
uint32_t  amt 
)

Mark some bytes in buffer as consumed.

Parameters
mMemory buffer object.
amtNumber of bytes consumed.

This function will mark amt bytes in the buffer m as consumed. This does not involve moving any data in the buffer; moves only occur if required in one of the writem_* functions. Using this function will invalidate the results of any previous call to membuf_use().

◆ membuf_truncate()

void membuf_truncate ( struct membuf_t *  m,
uint32_t  new_len 
)

Truncate the memory buffer.

Parameters
mMemory buffer object.
new_lenThe new length of the buffer.

This function will truncate the buffer so that it only contains new_len bytes. If the buffer does not contain enough data to be affected, m will not be changed.

◆ writem_i8()

void writem_i8 ( struct membuf_t *  m,
uint8_t  i 
)

Write integer to memory buffer (8-bit).

Parameters
mMemory buffer object.
iInteger to write.

◆ writem_str()

void writem_str ( struct membuf_t *  m,
const char *  str 
)

Write null-terminated string to memory buffer.

Parameters
mMemory buffer object.
strString to write.

The string is written as the length (32-bit integer) followed by the data, without the terminating null.

◆ writem_mem()

void writem_mem ( struct membuf_t *  m,
const char *  mem,
uint32_t  amt 
)

Write raw data to memory buffer.

Parameters
mMemory buffer object.
memPointer to bytes to write.
amtNumber of bytes to write.

◆ readm_i8()

int readm_i8 ( struct membuf_t *  m,
uint8_t *  out 
)

Read 8-bit integer from memory buffer.

Parameters
mMemory buffer to read from.
[out]outInteger to write to.
Return values
0on success.
-1on error (not enough data in buffer).

This function will read and consume an 8-bit integer from a memory buffer. If there is not enough data in the buffer, it will fail and return -1.

See also
readm_i16(), readm_i32(), readm_i64().

◆ readm_str()

int readm_str ( struct membuf_t *  m,
char *  str,
uint32_t  str_size,
uint32_t *  len_out 
)

Read string from memory buffer.

Parameters
mMemory buffer to read from.
strPointer to allocated storage.
str_sizeNumber of bytes in str.
[out]len_outLength of resultant string (may be 0).
Return values
0on success.
-1on error (not enough data in buffer).

This function will read a string from a buffer. It first reads the 32-bit string length as written by writem_str() or writes_str(), and then the string data itself. If the string is larger than the str array, then str will contain a truncated version of the string. str will always be null terminated.

If there is not enough data in the buffer, it will fail and return -1.

◆ readm_mem()

int readm_mem ( struct membuf_t *  m,
char *  mem,
uint32_t  mem_size 
)

Read bytes from memory buffer.

Parameters
mMemory buffer to read from.
memPointer to allocated storage.
mem_sizeNumber of bytes to read into mem.
Return values
0on success.
-1on error.

Reads mem_size bytes of data from m into mem. If there is not enough data in the buffer, it will fail and return -1.