Memory buffers. More...
Data Structures | |
struct | uv_buf_t |
A libuv buffer. More... | |
Macros | |
#define | using_buf(bufref) |
Use a buffer in a scope, freeing automatically when exiting. More... | |
#define | using_buf_on_abort_free(bufref) |
Use a buffer in a scope, freeing automatically only when an exception is thrown. More... | |
#define | using_buf_owned(owned, bufref) |
Use a potentially owned buffer in a scope. More... | |
Functions | |
uv_buf_t | nodec_buf (const void *base, size_t len) |
Initialize a libuv buffer, which is a record with a data pointer and its length. | |
uv_buf_t | nodec_buf_str (const char *s) |
Initialize a buffer from a string. | |
uv_buf_t | nodec_buf_strdup (const char *s) |
Initialize a buffer from a string. More... | |
uv_buf_t | nodec_buf_null () |
Create a null buffer, i.e. nodec_buf(NULL,0) . | |
uv_buf_t | nodec_buf_alloc (size_t len) |
Create and allocate a buffer of a certain length. | |
uv_buf_t | nodec_buf_realloc (uv_buf_t buf, size_t len) |
Reallocate a buffer to a given new length. More... | |
uv_buf_t | nodec_buf_ensure (uv_buf_t buf, size_t needed) |
Ensure a buffer has at least needed size. More... | |
uv_buf_t | nodec_buf_ensure_ex (uv_buf_t buf, size_t needed, size_t initial_size, size_t max_increase) |
Ensure a buffer has at least needed size. More... | |
uv_buf_t | nodec_buf_fit (uv_buf_t buf, size_t needed) |
Fit a buffer to needed size. More... | |
uv_buf_t | nodec_buf_append_into (uv_buf_t buf1, uv_buf_t buf2) |
Append the contents of buf2 into buf1 . More... | |
bool | nodec_buf_is_null (uv_buf_t buf) |
Is this a null buffer?. More... | |
void | nodec_buf_free (uv_buf_t buf) |
Free a buffer. More... | |
void | nodec_bufref_free (uv_buf_t *buf) |
Free a buffer by reference. More... | |
void | nodec_bufref_freev (lh_value bufref) |
Free a buffer by reference value. More... | |
void | nodec_bufref_nofreev (lh_value pv) |
Don't free a buffer by reference value. More... | |
Memory buffers.
Buffers are the libuv
uv_buf_t` structure which contain a pointer to the data and their length. Buffer structures are small and always passed and returned by value.
The routines in this module ensure that all buffers actually are allocated with len + 1
where base[len] == 0
, i.e. it is safe to print the contents as a string as it will be zero terminated.
struct uv_buf_t |
#define using_buf | ( | bufref | ) |
Use a buffer in a scope, freeing automatically when exiting.
Pass the buffer by reference so it will free the final memory that bufref->base
points to (and not the initial value).
bufref | a reference to the buffer to free after use. Example |
#define using_buf_on_abort_free | ( | bufref | ) |
Use a buffer in a scope, freeing automatically only when an exception is thrown.
Pass the buffer by reference so it will free the final memory that bufref->base
points to (and not the initial value).
bufref | a reference to the buffer to free after use. |
#define using_buf_owned | ( | owned, | |
bufref | |||
) |
Use a potentially owned buffer in a scope.
This is used for the async_read_bufx()
calls where the returned buffer might be owned (and must be freed), or not (and must not be freed). Pass the buffer by reference so it will free the final memory that bufref->base
points to (and not the initial value).
owned | Only free the buffer if owned is true. |
bufref | a reference to the buffer to free after use. |
Append the contents of buf2
into buf1
.
buf1 | the destination buffer. |
buf2 | the source buffer. |
buf1
that contains the contents of both. Ensure a buffer has at least needed
size.
buf | The buffer to potentially reallocate. If buf is null, use 8kb for initial size, and after that use doubling of at most 4mb increments. |
needed | the required size of the buffer. |
needed
length. uv_buf_t nodec_buf_ensure_ex | ( | uv_buf_t | buf, |
size_t | needed, | ||
size_t | initial_size, | ||
size_t | max_increase | ||
) |
Ensure a buffer has at least needed
size.
Like nodec_buf_ensure() but with customizable initial_size
and max_increase
.
Fit a buffer to needed
size.
Might increase the buffer size, or Reallocate to a smaller size if it was larger than 128 bytes with more than 20% wasted space.
buf | the buffer to fit. |
needed | the required space needed. |
needed
. void nodec_buf_free | ( | uv_buf_t | buf | ) |
Free a buffer.
This is usually not used directly, but through using_buf().
bool nodec_buf_is_null | ( | uv_buf_t | buf | ) |
Is this a null buffer?.
buf | the buffer. |
true
if the buffer length is 0 or the base
is NULL
. Reallocate a buffer to a given new length.
See also nodec_buf_fit() and nodec_buf_ensure().
uv_buf_t nodec_buf_strdup | ( | const char * | s | ) |
Initialize a buffer from a string.
This (re)allocates the string in the heap which is then owned by the buffer.
void nodec_bufref_free | ( | uv_buf_t * | buf | ) |
Free a buffer by reference.
This is usually not used directly, but through using_buf().
void nodec_bufref_freev | ( | lh_value | bufref | ) |
Free a buffer by reference value.
This is usually not used directly, but through using_buf().
void nodec_bufref_nofreev | ( | lh_value | pv | ) |
Don't free a buffer by reference value.
Usually not used directly, but used by using_buf_owned()