| Mercury Monitor Reference Manual |
|---|
Buffer HandlingBuffer Handling — Buffer handling functions |
#include <monitor/monitor.h> #define MON_BUFFER_BLOCK_SIZE #define MON_BUFFER_UNIT mon_buffer; enum mon_buffer_type; void buf_init (mon_buffer *buf); int buf_init_fixed (mon_buffer *buf, size_t size); void buf_init_full (mon_buffer *buf, const char *data, size_t len); void buf_init_empty (mon_buffer *buf, char *data, size_t size); void buf_destroy (mon_buffer *buf); int buf_ensure_free (mon_buffer *buf, size_t len); void buf_normalize (mon_buffer *buf); void buf_normalize_read (mon_buffer *buf); void buf_normalize_write (mon_buffer *buf); int buf_append (mon_buffer *dst, mon_buffer *src); int buf_append_data (mon_buffer *dst, void *src, size_t len); int buf_extract_data (mon_buffer *src, void *dst, size_t len); size_t buf_avail (mon_buffer *buf); size_t buf_len (mon_buffer *buf); char* buf_start (mon_buffer *buf); char* buf_end (mon_buffer *buf); size_t buf_padsize (size_t size); void buf_mark_size (mon_buffer *buf); void buf_subst_size (mon_buffer *buf); void buf_add_int32 (mon_buffer *buf, int32_t val); void buf_add_uint32 (mon_buffer *buf, uint32_t val); void buf_add_int64 (mon_buffer *buf, int64_t val); void buf_add_uint64 (mon_buffer *buf, uint64_t val); void buf_add_double (mon_buffer *buf, double val); void buf_add_string (mon_buffer *buf, const char *val); void buf_add_opaque (mon_buffer *buf, const void *val, uint32_t len); void buf_get_int32 (mon_buffer *buf, int32_t *val); void buf_get_uint32 (mon_buffer *buf, uint32_t *val); void buf_get_int64 (mon_buffer *buf, int64_t *val); void buf_get_uint64 (mon_buffer *buf, uint64_t *val); void buf_get_double (mon_buffer *buf, double *val); int32_t buf_read_int32 (mon_buffer *buf); uint32_t buf_read_uint32 (mon_buffer *buf); int64_t buf_read_int64 (mon_buffer *buf); uint64_t buf_read_uint64 (mon_buffer *buf); double buf_read_double (mon_buffer *buf); int buf_get_string (mon_buffer *buf, char **s, uint32_t *slen); int buf_get_opaque (mon_buffer *buf, void **s, uint32_t *slen); int32_t buf_peek_int32 (mon_buffer *buf); uint32_t buf_peek_uint32 (mon_buffer *buf); int buf_read (mon_buffer *buf, int sd); int buf_write (mon_buffer *buf, int sd); int buf_write_all (mon_buffer *buf, int sd); int buf_printf (mon_buffer *buf, const char *fmt, ...); int buf_vprintf (mon_buffer *buf, const char *fmt, va_list ap); int buf_scanf (mon_buffer *buf, const char *fmt, ...); int buf_vscanf (mon_buffer *buf, const char *fmt, va_list ap);
#define MON_BUFFER_BLOCK_SIZE 64
Defines the size of chunks in which memory will be allocated. Must be a power of 2.
#define MON_BUFFER_UNIT 4
Defines the smallest memory unit that can be added to the buffer. Currently this is 4 octets (32 bits).
typedef struct {
mon_buffer_type type;
char *data; /* pointer to the data */
size_t size; /* size of allocated data */
size_t start; /* start of unprocessed data */
size_t end; /* end of used data */
} mon_buffer;
The mon_buffer struct represents a generic buffer.
| mon_buffer_type type; | a mon_buffer_type specifying the memory allocation strategy for this buffer. |
| char *data; | pointer to the buffer area. |
| size_t size; | size of data. |
| size_t start; | offset of the first used byte in data. |
| size_t end; | offset of the first free byte in data. |
typedef enum {
MON_BUFFER_DYNAMIC,
MON_BUFFER_FIXED,
MON_BUFFER_STATIC
} mon_buffer_type;
Memory allocation strategy for a mon_buffer.
| MON_BUFFER_DYNAMIC | Memory will be allocated dynamically when needed. |
| MON_BUFFER_FIXED | buf_ensure_free() will return an error instead of growing the buffer if there are not enough free space left. |
| MON_BUFFER_STATIC | The buffer points to static memory. buf_ensure_free() will not try to enlarge it and buf_destroy() will not free it. |
void buf_init (mon_buffer *buf);
Initializes a buffer. The buffer type will be MON_BUFFER_DYNAMIC by default.
| buf : | a mon_buffer to initialize. |
int buf_init_fixed (mon_buffer *buf, size_t size);
Initializes a fixed-size buffer. The buffer type will be MON_BUFFER_FIXED.
| buf : | a mon_buffer to initialize. |
| size : | the requested size of the buffer. |
| Returns : | 0 if successful or ENOMEM if there was not enough memory. |
void buf_init_full (mon_buffer *buf, const char *data, size_t len);
Initializes a buffer pointing to static memory. The buffer is initialized as being full. The buffer type will be MON_BUFFER_STATIC.
| buf : | a mon_buffer. |
| data : | the contents of the buffer. |
| len : | the length of the buffer contents. |
void buf_init_empty (mon_buffer *buf, char *data, size_t size);
Initializes a buffer pointing to static memory. The buffer is initialized as being empty. The buffer type will be MON_BUFFER_STATIC.
| buf : | a mon_buffer. |
| data : | pointer that will hold the contents of the buffer. |
| size : | the size of the data that can be used by the buffer. |
void buf_destroy (mon_buffer *buf);
Destroys the contents of a buffer.
| buf : | a mon_buffer to destroy. |
int buf_ensure_free (mon_buffer *buf, size_t len);
Checks that the buffer buf is big enough to hold at least len bytes of additional data. If the buffer is smaller then it is enlarged to ensure this.
| buf : | a mon_buffer to be checked/enlarged. |
| len : | number of free bytes requested. |
| Returns : | 0 is returned if there is enough room in the buffer. ENOMEM is returned if memory allocation has failed. MON_ERR_BUFFER_FULL is returned if there is not enough room and the buffer is either MON_BUFFER_FIXED or MON_BUFFER_STATIC. |
void buf_normalize (mon_buffer *buf);
Normalizes the contents of a buffer, move used data to the beginning.
| buf : | a mon_buffer to normailze. |
void buf_normalize_read (mon_buffer *buf);
Normalizes the contents of a buffer if needed before a read-like operation. An operation is considered read-like if it puts new data into the buffer.
| buf : | a mon_buffer to normalize. |
void buf_normalize_write (mon_buffer *buf);
Normalizes the contents of a buffer if needed after a write-like operation. An operation is considered write-like if it moves data out of the buffer.
| buf : | a mon_buffer to normalize. |
int buf_append (mon_buffer *dst, mon_buffer *src);
Appends the contents of the buffer src to buffer dst. Buffer dst is enlarged appropriately if it is too small.
| dst : | the destination mon_buffer. |
| src : | the source mon_buffer. |
| Returns : | 0 or error code. If memory allocation failed ENOMEM is returned. |
int buf_append_data (mon_buffer *dst, void *src, size_t len);
Appends src of size len to buffer dst. Buffer dst is enlarged appropriately if it is too small.
| dst : | the destination mon_buffer. |
| src : | the source data. |
| len : | length of src. |
| Returns : | 0 or error code. If memory allocation failed ENOMEM is returned. |
int buf_extract_data (mon_buffer *src, void *dst, size_t len);
Extracts len bytes of raw data from the start of buffer src to dst.
| src : | a mon_buffer to copy data from. |
| dst : | pointer to the start of the memory to hold copied data. Must be big enough to hold at least len bytes. |
| len : | number of bytes to extract. |
| Returns : | 0 or error code. If the buffer has less than len bytes of data EINVAL is returned. |
size_t buf_avail (mon_buffer *buf);
Returns the amount of free space in a mon_buffer.
| buf : | a mon_buffer. |
| Returns : | the amount of free space. |
size_t buf_len (mon_buffer *buf);
Returns the amount of data in a mon_buffer.
| buf : | a mon_buffer. |
| Returns : | the amount of data in the buffer. |
char* buf_start (mon_buffer *buf);
Returns a pointer to the beginning of the buffer's contents.
| buf : | a mon_buffer. |
| Returns : | a pointer to the beginning of the buffer. |
char* buf_end (mon_buffer *buf);
Returns a pointer to the end of the buffer's contents.
| buf : | a mon_buffer. |
| Returns : | a pointer to the end of the buffer. |
size_t buf_padsize (size_t size);
Pads size to be a multiple of MON_BUFFER_UNIT.
| size : | the size to pad. |
| Returns : | the padded size. |
void buf_mark_size (mon_buffer *buf);
Adds a 4-byte dummy value to a buffer that will hold the length of the data added after it. This function should always be paired exactly with buf_subst_size and the two functions should be on the same blocking level.
| buf : | a mon_buffer. |
void buf_subst_size (mon_buffer *buf);
Calculates the length of data added to a buffer since the last call to buf_mark_size and stores it in the space reserved by buf_mark_size. This function must be on the same blocking level as buf_mark_size.
| buf : | a mon_buffer. |
void buf_add_int32 (mon_buffer *buf, int32_t val);
Appends the signed 32 bit integer value val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | the value to be added. |
void buf_add_uint32 (mon_buffer *buf, uint32_t val);
Appends the unsigned 32 bit integer value val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | the value to be added. |
void buf_add_int64 (mon_buffer *buf, int64_t val);
Appends the signed 64 bit integer value val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | the value to be added. |
void buf_add_uint64 (mon_buffer *buf, uint64_t val);
Appends the unsigned 64 bit integer value val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | the value to be added. |
void buf_add_double (mon_buffer *buf, double val);
Appends the double precision (64 bit) floating point value val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | the value to be added. |
void buf_add_string (mon_buffer *buf, const char *val);
Appends the string value val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | pointer to a \0 terminated C string to be added. |
void buf_add_opaque (mon_buffer *buf, const void *val, uint32_t len);
Appends len number of bytes opaque data starting at val to buffer buf. The buffer must have enough free space to hold the value.
| buf : | a mon_buffer to add data to. |
| val : | pointer to the data to be added. |
| len : | number of bytes to be added. |
void buf_get_int32 (mon_buffer *buf, int32_t *val);
Gets data from the beginning of the buffer buf converted as a signed 32 bit integer. The value is put in val and removed from the buffer.
| buf : | a mon_buffer to get data from. |
| val : | the value returned. |
void buf_get_uint32 (mon_buffer *buf, uint32_t *val);
Gets data from the beginning of the buffer buf converted as an unsigned 32 bit integer. The value is put in val and removed from the buffer.
| buf : | a mon_buffer to get data from. |
| val : | the value returned. |
void buf_get_int64 (mon_buffer *buf, int64_t *val);
Gets data from the beginning of the buffer buf converted as a signed 64 bit integer. The value is put in val and removed from the buffer.
| buf : | a mon_buffer to get data from. |
| val : | the value returned. |
void buf_get_uint64 (mon_buffer *buf, uint64_t *val);
Gets data from the beginning of the buffer buf converted as an unsigned 64 bit integer. The value is put in val and removed from the buffer.
| buf : | a mon_buffer to get data from. |
| val : | the value returned. |
void buf_get_double (mon_buffer *buf, double *val);
Gets data from the beginning of the buffer buf converted as a double precision (64 bit) floating point value. The value is put in val and removed from the buffer.
| buf : | a mon_buffer to get data from. |
| val : | the value returned. |
int32_t buf_read_int32 (mon_buffer *buf);
Extracts a 32-bit signed integer from the beginning of a buffer.
| buf : | a mon_buffer. |
| Returns : | the extracted value. |
uint32_t buf_read_uint32 (mon_buffer *buf);
Extracts a 32-bit unsigned integer from the beginning of a buffer.
| buf : | a mon_buffer. |
| Returns : | the extracted value. |
int64_t buf_read_int64 (mon_buffer *buf);
Extracts a 64-bit signed integer from the beginning of a buffer.
| buf : | a mon_buffer. |
| Returns : | the extracted value. |
uint64_t buf_read_uint64 (mon_buffer *buf);
Extracts a 64-bit unsigned integer from the beginning of a buffer.
| buf : | a mon_buffer. |
| Returns : | the extracted value. |
double buf_read_double (mon_buffer *buf);
Extracts a 64-bit double precision floating point number from the beginning of a buffer.
| buf : | a mon_buffer. |
| Returns : | the extracted value. |
int buf_get_string (mon_buffer *buf, char **s, uint32_t *slen);
Gets data from the beginning of the buffer buf converted as a string. The string is copied to memory allocated using g_malloc and removed from the buffer. Pointer to the string is returned in s and the length of the string is returned in slen if it is not NULL. If the buffer contained a string of length 0, s will contain a valid pointer to an empty string on return of the function.
| buf : | a mon_buffer to get data from. |
| s : | returned pointer to a \0 terminated C string allocated using g_malloc |
| slen : | length of string returned if not NULL |
| Returns : | 0 or -1 if the buffer has not enough data or memory allocation failed. |
int buf_get_opaque (mon_buffer *buf, void **s, uint32_t *slen);
Gets opaque data bytes from the beginning of the buffer buf. The data is copied to memory allocated using g_malloc and removed from the buffer. Pointer to the data is returned in val and the length of the string is returned in slen. If the buffer contained opaque data of length 0, s will contain NULL on return of the function.
| buf : | a mon_buffer to get data from. |
| s : | returned pointer to memory allocated using g_malloc holding the data |
| slen : | length of data returned. Must not be NULL. |
| Returns : | 0 or -1 if the buffer has not enough data or memory allocation failed. |
int32_t buf_peek_int32 (mon_buffer *buf);
Returns a 32-bit signed integer from the beginning of the buffer without removing it from the buffer. Thus the next buf_read_int32() call will return the same value.
| buf : | a mon_buffer. |
| Returns : | the returned value. |
uint32_t buf_peek_uint32 (mon_buffer *buf);
Returns a 32-bit unsigned integer from the beginning of the buffer without removing it from the buffer. Thus the next buf_read_uint32() call will return the same value.
| buf : | a mon_buffer. |
| Returns : | the returned value. |
int buf_read (mon_buffer *buf, int sd);
Reads data from the specified file descriptor sd into a buffer buf.
| buf : | a mon_buffer to read data into. |
| sd : | a file descriptor to read data from. |
| Returns : | 0 or error code. |
int buf_write (mon_buffer *buf, int sd);
Writes the contents of the buffer buf to the specified file descriptor sd.
| buf : | a mon_buffer to write contents of. |
| sd : | a file descriptor to write data to. |
| Returns : | 0 or error code. |
int buf_write_all (mon_buffer *buf, int sd);
Writes the contents of the buffer buf to the specified file descriptor sd. Like buf_write(), but does not return on partial write.
| buf : | a mon_buffer to write contents of. |
| sd : | a file descriptor to write data to. |
| Returns : | 0 or error code. |
int buf_printf (mon_buffer *buf, const char *fmt, ...);
Fills a buffer in a printf-like manner. The supported format sequences:
%d: INT32
%ld: INT64
%u: UINT32
%lu: UINT64
%g: DOUBLE
%s: STRING
(: Placeholder for the size of the following data till the next ) character in the format string.
Note: this function is quite convinient, however it is about 2 times slower than building the buffer contents manually using the buf_add functions.
| buf : | a mon_buffer. |
| fmt : | the format string. |
| ... : | the list of data to store in the buffer. |
| Returns : | 0 or an error code. |
int buf_vprintf (mon_buffer *buf, const char *fmt, va_list ap);
The same as buf_printf() but called with a va_list instead of a variable number of arguments.
| buf : | a mon_buffer. |
| fmt : | the format string. |
| ap : | the list of data to store in the buffer. |
| Returns : | 0 or an error code. |
int buf_scanf (mon_buffer *buf, const char *fmt, ...);
Parses the contents of a buffer in a scanf-like manner. The supported format sequences:
%d: INT32
%ld: INT64
%u: UINT32
%lu: UINT64
%g: DOUBLE
%as: STRING
(: Placeholder for the size of the following data till the next ) character in the format string.
Please note that while buf_printf() uses %s, buf_scanf requires %as. This behaviour was chosen because this way the GNU C Compiler supports compile-time checking of the arguments.
| buf : | a mon_buffer to interpret. |
| fmt : | the format string. |
| ... : | result pointers to store the interpreted results. |
| Returns : | 0 or an error code. |
int buf_vscanf (mon_buffer *buf, const char *fmt, va_list ap);
The same as buf_scanf() but called with a va_list instead of a variable number of arguments.
| buf : | a mon_buffer to interpret. |
| fmt : | the format string. |
| ap : | result pointers to store the interpreted results. |
| Returns : | 0 or an error code. |
| << Basic IO Functions | Configuration >> |