cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
Memory Safety

Wrappers around string.h functions that force sizeof() safety. More...

Macros

#define MEM_ZERO(ptr, size)
 Zeroes a block of memory. Wraps memset.
#define MEM_ZERO_STRUCT(ptr)
 Zeroes an entire struct via its pointer.
#define MEM_ZERO_ARRAY(arr)
 Zeroes an entire static array.
#define MEM_COPY(dst, src, size)
 Copies memory using memmove (overlap-safe).
#define MEM_COPY_STRUCT(dst, src)
 Copies one struct into another, clamping to the smaller size.
#define MEM_COPY_ARRAY(dst, src)
 Copies one static array into another, clamping to the smaller size.

Detailed Description

Wrappers around string.h functions that force sizeof() safety.

Macro Definition Documentation

◆ MEM_COPY

#define MEM_COPY ( dst,
src,
size )
Value:
memmove((dst), (src), (size))

Copies memory using memmove (overlap-safe).

Definition at line 576 of file base_macros.h.

◆ MEM_COPY_ARRAY

#define MEM_COPY_ARRAY ( dst,
src )
Value:
MEM_COPY((dst), (src), MIN(sizeof(dst), sizeof(src)))
#define MEM_COPY(dst, src, size)
Copies memory using memmove (overlap-safe).
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).

Copies one static array into another, clamping to the smaller size.

Example:

int src[8] = {1,2,3,4,5,6,7,8};
int dst[4];
MEM_COPY_ARRAY(dst, src); // copies only 4 elements — dst is the limit
#define MEM_COPY_ARRAY(dst, src)
Copies one static array into another, clamping to the smaller size.
Parameters
dstDestination array.
srcSource array.

Definition at line 609 of file base_macros.h.

◆ MEM_COPY_STRUCT

#define MEM_COPY_STRUCT ( dst,
src )
Value:
MEM_COPY((dst), (src), MIN(sizeof(*(dst)), sizeof(*(src))))

Copies one struct into another, clamping to the smaller size.

Safe even if the structs are different sizes — copies only up to min(sizeof(*dst), sizeof(*src)) bytes, preventing overflow.

Example:

PlayerV1 old = load_old_save();
PlayerV2 new_player;
MEM_COPY_STRUCT(&new_player, &old); // copies shared prefix safely
#define MEM_COPY_STRUCT(dst, src)
Copies one struct into another, clamping to the smaller size.
Parameters
dstPointer to the destination struct.
srcPointer to the source struct.

Definition at line 594 of file base_macros.h.

◆ MEM_ZERO

#define MEM_ZERO ( ptr,
size )
Value:
memset((ptr), 0, (size))

Zeroes a block of memory. Wraps memset.

Definition at line 547 of file base_macros.h.

◆ MEM_ZERO_ARRAY

#define MEM_ZERO_ARRAY ( arr)
Value:
MEM_ZERO((arr), sizeof(arr))
#define MEM_ZERO(ptr, size)
Zeroes a block of memory. Wraps memset.

Zeroes an entire static array.

Example:

int scores[64];
MEM_ZERO_ARRAY(scores); // zeroes all 64 elements
#define MEM_ZERO_ARRAY(arr)
Zeroes an entire static array.
Parameters
arrThe statically allocated array (not a pointer).

Definition at line 573 of file base_macros.h.

◆ MEM_ZERO_STRUCT

#define MEM_ZERO_STRUCT ( ptr)
Value:
MEM_ZERO((ptr), sizeof(*(ptr)))

Zeroes an entire struct via its pointer.

Example:

Player p;
MEM_ZERO_STRUCT(&p); // safe: uses sizeof(*ptr), not sizeof(ptr)
#define MEM_ZERO_STRUCT(ptr)
Zeroes an entire struct via its pointer.
Parameters
ptrPointer to the struct.

Definition at line 560 of file base_macros.h.