rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rmem.h File Reference

Memory allocation, byte-buffer operations, and pattern scanning. More...

#include <rlib/rtypes.h>
#include <stdlib.h>
#include <stdarg.h>

Go to the source code of this file.

Data Structures

struct  RMemChunk
 Pointer + length view of a memory region. More...
 
struct  RMemVTable
 Allocator backend slots routed through by every rlib heap helper. More...
 
struct  RMemScanToken
 One token of a parsed scan pattern. More...
 
struct  RMemScanResult
 Result of a successful r_mem_scan_pattern call. More...
 

Macros

#define r_alloca(size)   alloca (size)
 Allocate size bytes on the stack frame of the caller.
 
#define r_alloca0(size)   r_memclear (r_alloca (size), size)
 Stack-allocate size bytes and zero-fill them.
 
#define r_mem_newa_n(type, n)   ((type*) r_alloca (sizeof (type) * (rsize) (n)))
 Stack-allocate an array of n elements of type.
 
#define r_mem_newa0_n(type, n)   ((type*) r_alloca0 (sizeof (type) * (rsize) (n)))
 Stack-allocate a zeroed array of n elements of type.
 
#define r_mem_newa(type)   r_mem_newa_n (type, 1)
 Stack-allocate a single instance of type.
 
#define r_mem_newa0(type)   r_mem_newa0_n (type, 1)
 Stack-allocate a single zeroed instance of type.
 
#define r_mem_new_n(type, n)   ((type*) r_malloc (sizeof (type) * (rsize) (n)))
 Allocate an array of n elements of type.
 
#define r_mem_new0_n(type, n)   ((type*) r_malloc0 (sizeof (type) * (rsize) (n)))
 Allocate a zeroed array of n elements of type.
 
#define r_mem_new(type)   r_mem_new_n (type, 1)
 Allocate a single instance of type.
 
#define r_mem_new0(type)   r_mem_new0_n (type, 1)
 Allocate a single zeroed instance of type.
 

Enumerations

enum  RMemTokenType {
  R_MEM_TOKEN_NONE = -1 , R_MEM_TOKEN_BYTES , R_MEM_TOKEN_WILDCARD , R_MEM_TOKEN_WILDCARD_SIZED ,
  R_MEM_TOKEN_COUNT
}
 Token classes produced by the r_mem_scan_pattern parser. More...
 
enum  RMemScanResultType {
  R_MEM_SCAN_RESULT_INVAL = -4 , R_MEM_SCAN_RESULT_OOM = -3 , R_MEM_SCAN_RESULT_INVALID_PATTERN = -2 , R_MEM_SCAN_RESULT_PATTERN_NOT_IMPL = -1 ,
  R_MEM_SCAN_RESULT_OK = 0 , R_MEM_SCAN_RESULT_NOT_FOUND
}
 Status codes returned by r_mem_scan_pattern. More...
 

Functions

char * alloca ()
 
void r_free (rpointer ptr)
 Release a heap allocation obtained from r_malloc / r_malloc0 / r_calloc / r_realloc.
 
rpointer r_malloc (rsize size) R_ATTR_ALLOC_SIZE_ARG(1)
 Allocate size uninitialised bytes on the heap.
 
rpointer r_malloc0 (rsize size) R_ATTR_ALLOC_SIZE_ARG(1)
 Allocate size zero-initialised bytes on the heap.
 
rpointer r_calloc (rsize count, rsize size) R_ATTR_ALLOC_SIZE_ARGS(1
 Allocate count zero-initialised elements of size bytes.
 
rpointer rpointer r_realloc (rpointer ptr, rsize size) R_ATTR_WARN_UNUSED_RESULT
 Resize an existing allocation to size bytes.
 
void r_mem_set_vtable (RMemVTable *vtable)
 Replace the active allocator vtable.
 
void r_mem_get_vtable (RMemVTable *out)
 Copy the active vtable into *out.
 
rboolean r_mem_using_system_default (void)
 Whether the active vtable still points at libc.
 
int r_memcmp_ct (rconstpointer a, rconstpointer b, rsize size)
 Constant-time variant of r_memcmp.
 
void r_memclear_secure (rpointer ptr, rsize size)
 Zero size bytes at ptr in a way the compiler can't elide.
 
rpointer r_memdup (rconstpointer src, rsize size)
 Heap-allocate a copy of size bytes at src.
 
rsize r_memagg (rpointer dst, rsize size, rsize *out,...)
 Aggregate (concatenate) a NULL-terminated list of byte chunks into dst.
 
rsize r_memaggv (rpointer dst, rsize size, rsize *out, va_list args)
 va_list variant of r_memagg.
 
rpointer r_memdup_agg (rsize *out,...)
 Aggregate a NULL-terminated list of byte chunks into a freshly-allocated buffer.
 
rpointer r_memdup_aggv (rsize *out, va_list args)
 va_list variant of r_memdup_agg.
 
rpointer r_mem_scan_byte (rconstpointer mem, rsize size, ruint8 byte)
 Find the first occurrence of byte byte in mem.
 
rpointer r_mem_scan_byte_any (rconstpointer mem, rsize size, const ruint8 *byte, rsize bytes)
 Find the first byte in mem that matches any byte in the byte set.
 
rpointer r_mem_scan_data (rconstpointer mem, rsize size, rconstpointer data, rsize datasize)
 Find the first occurrence of datasize-byte sequence data within mem.
 
rpointer r_mem_scan_simple_pattern (rconstpointer mem, rsize size, const rchar *pattern, rpointer *end)
 Find the first match for pattern in mem (simple form).
 
RMemScanResultType r_mem_scan_pattern (rconstpointer mem, rsize size, const rchar *pattern, RMemScanResult **result)
 Find the first match for pattern in mem, returning full per-token detail.
 

Detailed Description

Memory allocation, byte-buffer operations, and pattern scanning.

Contains:

  • Heap allocator wrappers (r_malloc, r_calloc, ...) routed through an installable RMemVTable so a process can redirect every rlib allocation to a custom backend (test harnesses, leak trackers, debug allocators).
  • Stack allocator macros (r_alloca, r_mem_newa, ...) that paper over the platform-specific alloca / _alloca / __builtin_alloca declarations.
  • Byte-buffer ops (r_memcpy, r_memset, ...) as static inlines around the libc intrinsics, with NULL-pointer guards. The constant-time variant r_memcmp_ct and the optimiser- resistant r_memclear_secure live here too.
  • Variadic aggregator helpers (r_memagg, r_memdup_agg) that concatenate a NULL-terminated list of (ptr, size) pairs.
  • Byte / pattern scanners for searching memory regions.