Scratch Arena

static inline void *lace_scratch_alloc(lace_worker *lw, size_t size)

Allocate size bytes from the calling worker’s scratch arena.

Allocations are aligned to 16 bytes (max_align_t). The returned pointer is valid until the caller releases it with lace_scratch_reset(), or until the task that allocated it returns after a matching reset. If the reservation is exhausted, the program aborts.

Parameters:
  • lw – Pointer to the current worker

  • size – Number of bytes to allocate

Returns:

Pointer to allocated memory

static inline void *lace_scratch_array(lace_worker *lw, size_t count, size_t elem_size)

Allocate an array of count elements of size elem_size from the calling worker’s scratch arena.

This is a convenience wrapper around lace_scratch_alloc(). The allocation has the same lifetime and alignment guarantees as lace_scratch_alloc(). The function checks for multiplication overflow and aborts if the requested array size cannot be represented as a size_t.

Parameters:
  • lw – Pointer to the current worker

  • count – Number of elements to allocate

  • elem_size – Size of each element in bytes

Returns:

Pointer to allocated memory

static inline void *lace_scratch_mark(lace_worker *lw)

Save the current scratch arena position.

Pair every mark with a lace_scratch_reset() before the enclosing function returns, on every return path.

Parameters:

lw – Pointer to the current worker

Returns:

Opaque mark representing the current top-of-arena position

static inline void lace_scratch_reset(lace_worker *lw, void *mark)

Reset the scratch arena to a previously saved mark.

Frees all scratch allocations made after the corresponding lace_scratch_mark() call. The mark must have been obtained from the same worker.

Parameters:
  • lw – Pointer to the current worker

  • mark – Mark previously returned by lace_scratch_mark()