alloc.h

Allocation that exits on failure.

When an allocation is refused, every function here prints the reason to stderr and exits the process with EXIT_FAILURE.

Note

Thread-safe.

Functions

void *safe_malloc(size_t size)

Allocates uninitialised memory.

Warning

Exits the process with EXIT_FAILURE if the allocation is refused.

Parameters:

size – [in] Bytes to allocate.

Returns:

Memory to release with free. May be NULL for a zero size; free accepts that.

void *safe_realloc(void *ptr, size_t size)

Resizes an allocation, keeping its leading bytes.

Warning

Exits the process with EXIT_FAILURE if the allocation is refused.

Parameters:
  • ptr – [in] Memory to resize. NULL makes this safe_malloc().

  • size – [in] Bytes the result holds.

Returns:

Memory to release with free, holding the first min(old size, size) bytes of ptr. May be NULL for a zero size; free accepts that.

Post:

ptr is invalid afterwards, even when the result has the same address.

void *safe_aligned_malloc(size_t size)

Allocates uninitialised memory aligned to 64 bytes.

64 bytes is what the SIMD kernels require. On Linux, allocations of 8 MiB and above also request huge pages, which only affects speed.

Warning

Exits the process with EXIT_FAILURE if the allocation is refused.

Parameters:

size – [in] Bytes to allocate.

Returns:

Memory to release with free. May be NULL for a zero size; free accepts that.