NodeC  0.1
Buffer's

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

Detailed Description

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.


Data Structure Documentation

◆ uv_buf_t

struct uv_buf_t

A libuv buffer.

These are small and always passed by value.

Data Fields
void * base Pointer to the buffer data.
size_t len The length of the buffer data.

Macro Definition Documentation

◆ using_buf

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

Parameters
bufrefa reference to the buffer to free after use. Example
uv_buf_t buf = async_read_buf(stream);
{using_buf(&buf){
async_write_buf(out,buf);
}}

◆ using_buf_on_abort_free

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

Parameters
bufrefa reference to the buffer to free after use.

◆ using_buf_owned

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

Parameters
ownedOnly free the buffer if owned is true.
bufrefa reference to the buffer to free after use.

Function Documentation

◆ nodec_buf_append_into()

uv_buf_t nodec_buf_append_into ( uv_buf_t  buf1,
uv_buf_t  buf2 
)

Append the contents of buf2 into buf1.

Parameters
buf1the destination buffer.
buf2the source buffer.
Returns
a potentially resized buf1 that contains the contents of both.

◆ nodec_buf_ensure()

uv_buf_t nodec_buf_ensure ( uv_buf_t  buf,
size_t  needed 
)

Ensure a buffer has at least needed size.

Parameters
bufThe buffer to potentially reallocate. If buf is null, use 8kb for initial size, and after that use doubling of at most 4mb increments.
neededthe required size of the buffer.
Returns
a new buffer that has at least needed length.

◆ nodec_buf_ensure_ex()

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.

◆ nodec_buf_fit()

uv_buf_t nodec_buf_fit ( uv_buf_t  buf,
size_t  needed 
)

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.

Parameters
bufthe buffer to fit.
neededthe required space needed.
Returns
the new potentially reallocated buffer with a length that fits needed.

◆ nodec_buf_free()

void nodec_buf_free ( uv_buf_t  buf)

Free a buffer.

This is usually not used directly, but through using_buf().

◆ nodec_buf_is_null()

bool nodec_buf_is_null ( uv_buf_t  buf)

Is this a null buffer?.

Parameters
bufthe buffer.
Returns
true if the buffer length is 0 or the base is NULL.

◆ nodec_buf_realloc()

uv_buf_t nodec_buf_realloc ( uv_buf_t  buf,
size_t  len 
)

Reallocate a buffer to a given new length.

See also nodec_buf_fit() and nodec_buf_ensure().

◆ nodec_buf_strdup()

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.

◆ nodec_bufref_free()

void nodec_bufref_free ( uv_buf_t buf)

Free a buffer by reference.

This is usually not used directly, but through using_buf().

◆ nodec_bufref_freev()

void nodec_bufref_freev ( lh_value  bufref)

Free a buffer by reference value.

This is usually not used directly, but through using_buf().

◆ nodec_bufref_nofreev()

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