UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
membuffer.hpp File Reference

Manage blocks of dynamically allocated memory. More...

+ This graph shows which files directly or indirectly include this file:

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.
 

Detailed Description

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.


Class Documentation

◆ memptr

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).

◆ membuffer

struct membuffer

Maintains a block of dynamically allocated memory.

Note
Total length/capacity should not exceed MAX_INT. It is always a terminating null byte ('\0') appended but not reflected in length and capacity

Definition at line 61 of file membuffer.hpp.

Class Members
char * buf mem buffer; must not write beyond buf[length-1] (read/write).
size_t length length of buffer without terminating null byte (read-only).
size_t capacity total allocated memory without terminating null byte (read-only).
size_t size_inc used to increase size; MUST be > 0; (read/write).

Function Documentation

◆ str_alloc()

char * str_alloc ( const char *  str,
size_t  str_len 
)

Allocate memory and copy information from the input string to the newly allocated memory.

Returns
Pointer to the newly allocated memory. NULL if memory cannot be allocated.
Parameters
[in]strInput string object.
[in]str_lenInput string length.

Definition at line 56 of file membuffer.cpp.

+ Here is the caller graph for this function:

◆ memptr_cmp()

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.

Returns
  • < 0 string1 substring less than string2 substring
  • == 0 string1 substring identical to string2 substring
  • > 0 string1 substring greater than string2 substring
Parameters
[in]mInput memory object.
[in]sConstatnt string for the memory object to be compared with.

Definition at line 70 of file membuffer.cpp.

+ Here is the caller graph for this function:

◆ memptr_cmp_nocase()

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.

Returns
  • < 0 string1 substring less than string2 substring
  • == 0 string1 substring identical to string2 substring
  • > 0 string1 substring greater than string2 substring
Parameters
[in]mInput memory object.
[in]sConstant string for the memory object to be compared with.

Definition at line 84 of file membuffer.cpp.

+ Here is the caller graph for this function:

◆ membuffer_set_size()

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.

Returns
  • UPNP_E_SUCCESS - On Success
  • UPNP_E_OUTOF_MEMORY - On failure to allocate memory.
Parameters
[in,out]mbuffer whose size is to be modified.
[in]new_lengthnew size to which the buffer will be modified.

Definition at line 115 of file membuffer.cpp.

+ Here is the caller graph for this function:

◆ membuffer_init()

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.

Parameters
[in,out]mBuffer to be initialized.

Definition at line 165 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_destroy()

void membuffer_destroy ( membuffer m)

Free's memory allocated for membuffer* m.

Parameters
[in,out]mBuffer to be destroyed.

Definition at line 173 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_assign()

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.

Returns
  • UPNP_E_SUCCESS
  • UPNP_E_OUTOF_MEMORY
Parameters
[in,out]mBuffer whose memory is to be allocated and assigned.
[in]bufSource buffer whose contents will be copied.
[in]buf_lenLength of the source buffer.

Definition at line 185 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_assign_str()

int membuffer_assign_str ( membuffer m,
const char *  c_str 
)

Wrapper function for membuffer_assign().

Returns
  • UPNP_E_SUCCESS
  • UPNP_E_OUTOF_MEMORY
Parameters
[in,out]mBuffer to be allocated and assigned.
[in]c_strSource buffer whose contents will be copied.

Definition at line 209 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_append()

int membuffer_append ( membuffer m,
const void *  buf,
size_t  buf_len 
)

Invokes function to appends data from a constant buffer to the buffer.

Returns
int.
Parameters
[in,out]mBuffer whose memory is to be appended.
[in]bufSource buffer whose contents will be copied.
[in]buf_lenLength of the source buffer.

Definition at line 213 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_append_str()

int membuffer_append_str ( membuffer m,
const char *  c_str 
)

Invokes function to appends data from a constant string to the buffer.

Returns
int.
Parameters
[in,out]mBuffer whose memory is to be appended.
[in]c_strSource buffer whose contents will be copied.

Definition at line 219 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_insert()

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.

Returns
0 if successful, error code if error.
Parameters
[in,out]mBuffer whose memory size is to be increased and appended.
[in]bufsource buffer whose contents will be copied.
[in]buf_lensize of the source buffer.
[in]indexindex to determine the bounds while movinf the data.

Definition at line 223 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_delete()

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.

Parameters
[in,out]mBuffer whose memory size is to be decreased and copied to the modified location.
[in]indexIndex to determine bounds while moving data.
[in]num_bytesNumber of bytes that the data needs to shrink by.

Definition at line 250 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_detach()

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.

Returns
A pointer to the current buffer.
Parameters
[in,out]mBuffer to be returned and updated.

Definition at line 282 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ membuffer_attach()

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.

Note
'new_buf' must be allocted using malloc or realloc so that it can be freed using free().
Parameters
[in,out]mBuffer to be updated.
[in]new_bufSource buffer which will be assigned to the buffer to be updated.
[in]buf_lenLength of the source buffer.

Definition at line 295 of file membuffer.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function: