omg_buffer â Dynamic Resizable Byte & Double Buffer Management Library
Overview & Purpose in the OMG Architecture
omg_buffer (code/subr/omg_buffer.c, code/subr/omg_buffer.h), written by Jonathan Beaudoin and extended by Anand Hiroji (July 2015), provides lightweight, dynamically resizable heap buffers for raw binary packet I/O (OMG_Buffer) and double-precision statistical sample accumulation (OMG_Buffer_double).
When parsing variable-length sonar datagrams (such as Reson 7k, Kongsberg EM/KMALL, or GSF records) from disk, allocating a fixed buffer on the stack risks buffer overflow on unusually large water-column or snippet packets, while calling malloc()/free() on every single ping incurs heavy heap fragmentation overhead. omg_buffer solves this by maintaining a persistent heap allocation (size_buffer) that only calls realloc() when a newly encountered record (new_size) exceeds the current high-water mark, while tracking the active payload length in size_record.
In July 2015, OMG_Buffer_double was added to apply the same grow-on-demand pattern to double-precision floating-point arrays in normalization.c (storing individual linear backscatter intensities inside each 4D [Sector][ATBSA][GRA][SRA] bin for Rayleigh/outlier statistical filtering).
Key Data Structures (code/subr/omg_buffer.h)
OMG_Buffer
Dynamic byte buffer for raw binary I/O:
typedef struct {
unsigned char *buffer; /* Pointer to heap-allocated byte array */
unsigned int size_buffer; /* Total allocated capacity in bytes */
unsigned int size_record; /* Size of active data currently stored (<= size_buffer) */
} OMG_Buffer;
OMG_Buffer_double
Dynamic double-precision floating-point array buffer:
typedef struct {
double *buffer; /* Pointer to heap-allocated double array */
unsigned int size_buffer; /* Total allocated capacity (number of doubles) */
unsigned int size_record; /* Number of active doubles currently stored */
} OMG_Buffer_double;
Public Functions & Signatures
Raw Byte Buffer API (OMG_Buffer)
int init_OMG_Buffer(OMG_Buffer *buffer)Zero-initializes theOMG_Bufferstruct (buffer = NULL,size_buffer = 0,size_record = 0) usingmemset(). Returns0.int grow_OMG_Buffer(OMG_Buffer *buffer, unsigned int new_size)Ensures thatbuffer->bufferhas at leastnew_sizebytes of allocated capacity:- If
new_size <= buffer->size_buffer, returns0immediately without reallocating. - Otherwise, resizes
buffer->bufferviarealloc(buffer->buffer, new_size), aborts viaerror("Puked on buffer resize!")(support.h) on out-of-memory failure, updatesbuffer->size_buffer = new_size, and returns0.
- If
int wipe_OMG_Buffer(OMG_Buffer *buffer)Zeroes out allbuffer->size_bufferallocated bytes viamemset(buffer->buffer, 0, buffer->size_buffer)without freeing the underlying allocation. Returns0.int free_OMG_Buffer(OMG_Buffer *buffer)Freesbuffer->bufferif non-null and resets all struct fields to zero viainit_OMG_Buffer(). Returns0.
Double-Precision Array Buffer API (OMG_Buffer_double)
int init_OMG_Buffer_double(OMG_Buffer_double *buffer)Zero-initializes theOMG_Buffer_doublestruct viamemset(). Returns0.int grow_OMG_Buffer_double(OMG_Buffer_double *buffer, unsigned int new_size)Ifnew_size > buffer->size_buffer, resizesbuffer->buffertosizeof(double) * new_sizebytes viarealloc(), updatesbuffer->size_buffer = new_size, and returns0.int wipe_OMG_Buffer_double(OMG_Buffer_double *buffer)Zeroes the buffer contents viamemset(). Returns0.int free_OMG_Buffer_double(OMG_Buffer_double *buffer)Freesbuffer->bufferif non-null and re-initializes the struct to zero. Returns0.
How It Fits Into the OMG Architecture
omg_buffer is a foundational memory-management helper used across OMG binary format readers and the 4D backscatter normalization engine (normalization.h / normalization.c).