Memory Management
libevpl provides a high-performance memory management system optimized for I/O operations. Memory is allocated from large slabs and managed via reference-counted iovecs.
Allocation Flags
The flags parameter to evpl_iovec_alloc controls the type of allocation:
-
0(LOCAL) - Default. Uses non-atomic reference counting. Use when the iovec will only be accessed from a single thread. -
EVPL_IOVEC_FLAG_SHARED- Uses atomic reference counting. Use when the iovec may be accessed from multiple threads.
Functions
Buffer Allocation
evpl_iovec_alloc
int evpl_iovec_alloc(
struct evpl *evpl,
unsigned int length,
unsigned int alignment,
unsigned int max_iovecs,
unsigned int flags,
struct evpl_iovec *r_iovec);
Allocate one or more iovecs to hold the requested length of data.
Parameters:
evpl- Event looplength- Total bytes to allocatealignment- Memory alignment requirement in bytes, 0 if nonemax_iovecs- Maximum number of iovecs to returnflags- Allocation flags (0 for local, EVPL_IOVEC_FLAG_SHARED for multi-threaded)r_iovec- [OUT] Array to receive allocated iovecs
Returns: Number of iovecs allocated (1 to max_iovecs), or -1 on error
evpl_iovec_reserve
int evpl_iovec_reserve(
struct evpl *evpl,
unsigned int length,
unsigned int alignment,
unsigned int max_vec,
struct evpl_iovec *r_iovec);
Reserve iovecs for two-phase allocation. Use when the final data size is unknown at allocation time.
Parameters:
evpl- Event looplength- Maximum bytes to reservealignment- Alignment requirementmax_vec- Maximum iovecsr_iovec- [OUT] Reserved iovecs
Returns: Number of iovecs reserved, or -1 on error
Usage: Call evpl_iovec_reserve(), fill with data, then call evpl_iovec_commit() to finalize.
evpl_iovec_commit
void evpl_iovec_commit(
struct evpl *evpl,
unsigned int alignment,
struct evpl_iovec *iovecs,
int niovs);
Finalize a two-phase allocation after filling data into reserved iovecs.
Parameters:
evpl- Event loopalignment- Alignment used in reserveiovecs- Array of reserved iovecs (withlengthfields updated)niovs- Number of iovecs
Reference Counting
evpl_iovec_release
static inline void evpl_iovec_release(
struct evpl *evpl,
struct evpl_iovec *iovec);
Release a reference to an iovec.
Parameters:
evpl- Event loop context (may be NULL during shutdown)iovec- iovec to release
evpl_iovecs_release
static inline void evpl_iovecs_release(
struct evpl *evpl,
struct evpl_iovec *iov,
int niov);
Release references to multiple iovecs.
Parameters:
evpl- Event loop contextiov- Array of iovecs to releaseniov- Number of iovecs
evpl_iovec_clone
static inline void evpl_iovec_clone(
struct evpl_iovec *dst,
struct evpl_iovec *src);
Clone an iovec, creating a new reference to the same underlying buffer.
Parameters:
dst- Destination iovec to initializesrc- Source iovec to clone
evpl_iovec_clone_segment
static inline void evpl_iovec_clone_segment(
struct evpl_iovec *dst,
const struct evpl_iovec *src,
unsigned int offset,
unsigned int length);
Clone a portion of an iovec.
Parameters:
dst- Destination iovec to initializesrc- Source iovecoffset- Offset from the start of src’s datalength- Length of the segment
evpl_iovec_move
static inline void evpl_iovec_move(
struct evpl_iovec *dst,
struct evpl_iovec *src);
Move ownership of an iovec from source to destination. The source iovec should not be used after this call.
Parameters:
dst- Destination iovecsrc- Source iovec (invalid after call)
evpl_iovec_move_segment
static inline void evpl_iovec_move_segment(
struct evpl_iovec *dst,
struct evpl_iovec *src,
unsigned int offset,
unsigned int length);
Move ownership of a segment of an iovec.
Parameters:
dst- Destination iovecsrc- Source iovec (invalid after call)offset- Offset from the start of src’s datalength- Length of the segment
Accessor Functions
evpl_iovec_data
static inline void *evpl_iovec_data(const struct evpl_iovec *iovec);
Get the data pointer from an iovec.
Returns: Pointer to buffer data
evpl_iovec_length
static inline unsigned int evpl_iovec_length(const struct evpl_iovec *iovec);
Get the length of valid data in an iovec.
Returns: Length in bytes
evpl_iovec_set_length
static inline void evpl_iovec_set_length(
struct evpl_iovec *iovec,
unsigned int length);
Set the length of valid data in an iovec.
Parameters:
iovec- iovec to modifylength- New length
Iovec Tracing Mode
libevpl includes a debugging mode to detect memory management bugs such as use-after-free and double-release.
Enable at compile time:
cmake -DEVPL_IOVEC_TRACE=1 ...
When enabled, iovec operations are validated and will abort with a descriptive error message if a bug is detected. Use only during development due to performance overhead.
See Also
- Binds & Connections API - Using iovecs with send/receive
- Architecture - Memory management overview