Manage blocks of dynamically allocated memory. More...
Go to the source code of this file.
Classes | |
struct | memptr |
pointer to a chunk of memory. More... | |
struct | membuffer |
Maintains a block of dynamically allocated memory. More... | |
Functions | |
char * | str_alloc (const char *str, size_t str_len) |
Allocate memory and copy information from the input string to the newly allocated memory. | |
int | memptr_cmp (memptr *m, const char *s) |
Compares characters of strings passed for number of bytes. If equal for the number of bytes, the length of the bytes determines which buffer is shorter. | |
int | memptr_cmp_nocase (memptr *m, const char *s) |
Compares characters of 2 strings irrespective of the case for a specific count of bytes. | |
int | membuffer_set_size (membuffer *m, size_t new_length) |
Increases or decreases buffer cap so that at least 'new_length' bytes can be stored. | |
void | membuffer_init (membuffer *m) |
Wrapper to membuffer_initialize(). | |
void | membuffer_destroy (membuffer *m) |
Free's memory allocated for membuffer* m. | |
int | membuffer_assign (membuffer *m, const void *buf, size_t buf_len) |
Allocate memory to membuffer *m and copy the contents of the in parameter const void *buf. | |
int | membuffer_assign_str (membuffer *m, const char *c_str) |
Wrapper function for membuffer_assign(). | |
int | membuffer_append (membuffer *m, const void *buf, size_t buf_len) |
Invokes function to appends data from a constant buffer to the buffer. | |
int | membuffer_append_str (membuffer *m, const char *c_str) |
Invokes function to appends data from a constant string to the buffer. | |
int | membuffer_insert (membuffer *m, const void *buf, size_t buf_len, size_t index) |
Allocates memory for the new data to be inserted. Does memory management by moving the data from the existing memory to the newly allocated memory and then appending the new data. | |
void | membuffer_delete (membuffer *m, size_t index, size_t num_bytes) |
Shrink the size of the buffer depending on the current size of the buffer and the input parameters. Move contents from the old buffer to the new sized buffer. | |
char * | membuffer_detach (membuffer *m) |
Detaches current buffer and returns it. The caller must free the returned buffer using free(). After this call, length becomes 0. | |
void | membuffer_attach (membuffer *m, char *new_buf, size_t buf_len) |
Free existing memory in membuffer and assign the new buffer in its place. | |
Manage blocks of dynamically allocated memory.
membuffer is only used internal within the library and linked with the source file so there are no exports EXPORT_SPEC for external use.
Definition in file membuffer.hpp.
struct memptr |
pointer to a chunk of memory.
Definition at line 50 of file membuffer.hpp.
Class Members | ||
---|---|---|
char * | buf | start of memory (read/write). |
size_t | length | length of memory (read-only). |
struct membuffer |
Maintains a block of dynamically allocated memory.
Definition at line 61 of file membuffer.hpp.
char * str_alloc | ( | const char * | str, |
size_t | str_len | ||
) |
Allocate memory and copy information from the input string to the newly allocated memory.
[in] | str | Input string object. |
[in] | str_len | Input string length. |
Definition at line 56 of file membuffer.cpp.
int memptr_cmp | ( | memptr * | m, |
const char * | s | ||
) |
Compares characters of strings passed for number of bytes. If equal for the number of bytes, the length of the bytes determines which buffer is shorter.
[in] | m | Input memory object. |
[in] | s | Constatnt string for the memory object to be compared with. |
Definition at line 70 of file membuffer.cpp.
int memptr_cmp_nocase | ( | memptr * | m, |
const char * | s | ||
) |
Compares characters of 2 strings irrespective of the case for a specific count of bytes.
If the character comparison is the same the length of the 2 srings determines the shorter of the 2 strings.
[in] | m | Input memory object. |
[in] | s | Constant string for the memory object to be compared with. |
Definition at line 84 of file membuffer.cpp.
int membuffer_set_size | ( | membuffer * | m, |
size_t | new_length | ||
) |
Increases or decreases buffer cap so that at least 'new_length' bytes can be stored.
[in,out] | m | buffer whose size is to be modified. |
[in] | new_length | new size to which the buffer will be modified. |
Definition at line 115 of file membuffer.cpp.
void membuffer_init | ( | membuffer * | m | ) |
Wrapper to membuffer_initialize().
Set the size of the buffer to MEMBUF_DEF_SIZE_INC and Initializes m->buf to NULL, length = 0.
[in,out] | m | Buffer to be initialized. |
Definition at line 165 of file membuffer.cpp.
void membuffer_destroy | ( | membuffer * | m | ) |
Free's memory allocated for membuffer* m.
[in,out] | m | Buffer to be destroyed. |
Definition at line 173 of file membuffer.cpp.
int membuffer_assign | ( | membuffer * | m, |
const void * | buf, | ||
size_t | buf_len | ||
) |
Allocate memory to membuffer *m and copy the contents of the in parameter const void *buf.
[in,out] | m | Buffer whose memory is to be allocated and assigned. |
[in] | buf | Source buffer whose contents will be copied. |
[in] | buf_len | Length of the source buffer. |
Definition at line 185 of file membuffer.cpp.
int membuffer_assign_str | ( | membuffer * | m, |
const char * | c_str | ||
) |
Wrapper function for membuffer_assign().
[in,out] | m | Buffer to be allocated and assigned. |
[in] | c_str | Source buffer whose contents will be copied. |
Definition at line 209 of file membuffer.cpp.
int membuffer_append | ( | membuffer * | m, |
const void * | buf, | ||
size_t | buf_len | ||
) |
Invokes function to appends data from a constant buffer to the buffer.
[in,out] | m | Buffer whose memory is to be appended. |
[in] | buf | Source buffer whose contents will be copied. |
[in] | buf_len | Length of the source buffer. |
Definition at line 213 of file membuffer.cpp.
int membuffer_append_str | ( | membuffer * | m, |
const char * | c_str | ||
) |
Invokes function to appends data from a constant string to the buffer.
[in,out] | m | Buffer whose memory is to be appended. |
[in] | c_str | Source buffer whose contents will be copied. |
Definition at line 219 of file membuffer.cpp.
int membuffer_insert | ( | membuffer * | m, |
const void * | buf, | ||
size_t | buf_len, | ||
size_t | index | ||
) |
Allocates memory for the new data to be inserted. Does memory management by moving the data from the existing memory to the newly allocated memory and then appending the new data.
[in,out] | m | Buffer whose memory size is to be increased and appended. |
[in] | buf | source buffer whose contents will be copied. |
[in] | buf_len | size of the source buffer. |
[in] | index | index to determine the bounds while movinf the data. |
Definition at line 223 of file membuffer.cpp.
void membuffer_delete | ( | membuffer * | m, |
size_t | index, | ||
size_t | num_bytes | ||
) |
Shrink the size of the buffer depending on the current size of the buffer and the input parameters. Move contents from the old buffer to the new sized buffer.
[in,out] | m | Buffer whose memory size is to be decreased and copied to the modified location. |
[in] | index | Index to determine bounds while moving data. |
[in] | num_bytes | Number of bytes that the data needs to shrink by. |
Definition at line 250 of file membuffer.cpp.
char * membuffer_detach | ( | membuffer * | m | ) |
Detaches current buffer and returns it. The caller must free the returned buffer using free(). After this call, length becomes 0.
[in,out] | m | Buffer to be returned and updated. |
Definition at line 282 of file membuffer.cpp.
void membuffer_attach | ( | membuffer * | m, |
char * | new_buf, | ||
size_t | buf_len | ||
) |
Free existing memory in membuffer and assign the new buffer in its place.
[in,out] | m | Buffer to be updated. |
[in] | new_buf | Source buffer which will be assigned to the buffer to be updated. |
[in] | buf_len | Length of the source buffer. |
Definition at line 295 of file membuffer.cpp.