Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::core::HeapArena Class Reference

Heap arena implementation. More...

#include <heap_arena.h>

Inheritance diagram for roc::core::HeapArena:
Collaboration diagram for roc::core::HeapArena:

Public Member Functions

 HeapArena ()
 Initialize. More...
 
size_t num_allocations () const
 Get number of allocated blocks. More...
 
size_t num_guard_failures () const
 Get number of guard failures. More...
 
virtual void * allocate (size_t size)
 Allocate memory. More...
 
virtual void deallocate (void *ptr)
 Deallocate previously allocated memory. More...
 
virtual size_t compute_allocated_size (size_t size) const
 Computes how many bytes will be actually allocated if allocate() is called with given size. Covers all internal overhead, if any. More...
 
virtual size_t allocated_size (void *ptr) const
 Returns how many bytes was allocated for given pointer returned by allocate(). Covers all internal overhead, if any. Returns same value as computed by compute_allocated_size(size). More...
 
- Public Member Functions inherited from roc::core::IArena
template<class T >
void destroy_object (T &object)
 Destroy object and deallocate its memory. More...
 

Static Public Member Functions

static void set_guards (size_t guards)
 Set enabled guards, for all instances. Parameters. More...
 

Detailed Description

Heap arena implementation.

Uses malloc() and free().

The memory is always maximum aligned.

Implements three safety measures:

  • to catch double-free and other logical bugs, inserts link to owning arena before user data, and panics if it differs when memory is returned to arena
  • to catch buffer overflow bugs, inserts "canary guards" before and after user data, and panics if they are overwritten when memory is returned to arena
  • to catch uninitialized-access and use-after-free bugs, "poisons" memory when it returned to user, and when it returned back to the arena

Allocated chunks have the following format:

+-------------+-------------+-----------+-------------+
| ChunkHeader | ChunkCanary | user data | ChunkCanary |
+-------------+-------------+-----------+-------------+

ChunkHeader contains pointer to the owning arena, checked when returning memory to arena. ChunkCanary contains magic bytes filled when returning memory to user, and checked when returning memory to arena.

Thread-safe.

Definition at line 65 of file heap_arena.h.

Constructor & Destructor Documentation

◆ HeapArena()

roc::core::HeapArena::HeapArena ( )

Initialize.

Member Function Documentation

◆ allocate()

virtual void* roc::core::HeapArena::allocate ( size_t  size)
virtual

Allocate memory.

Implements roc::core::IArena.

◆ allocated_size()

virtual size_t roc::core::HeapArena::allocated_size ( void *  ptr) const
virtual

Returns how many bytes was allocated for given pointer returned by allocate(). Covers all internal overhead, if any. Returns same value as computed by compute_allocated_size(size).

Implements roc::core::IArena.

◆ compute_allocated_size()

virtual size_t roc::core::HeapArena::compute_allocated_size ( size_t  size) const
virtual

Computes how many bytes will be actually allocated if allocate() is called with given size. Covers all internal overhead, if any.

Implements roc::core::IArena.

◆ deallocate()

virtual void roc::core::HeapArena::deallocate ( void *  ptr)
virtual

Deallocate previously allocated memory.

Implements roc::core::IArena.

◆ num_allocations()

size_t roc::core::HeapArena::num_allocations ( ) const

Get number of allocated blocks.

◆ num_guard_failures()

size_t roc::core::HeapArena::num_guard_failures ( ) const

Get number of guard failures.

◆ set_guards()

static void roc::core::HeapArena::set_guards ( size_t  guards)
static

Set enabled guards, for all instances. Parameters.

  • guards defines options to modify behaviour as indicated in HeapArenaGuard

The documentation for this class was generated from the following file: