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 *)
 Deallocate previously allocated memory. 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_flags (size_t flags)
 Set flags, for all instances. 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 60 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.

◆ deallocate()

virtual void roc::core::HeapArena::deallocate ( void *  )
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_flags()

static void roc::core::HeapArena::set_flags ( size_t  flags)
static

Set flags, for all instances.

Parameters

  • flags defines options to modify behaviour as indicated in HeapArenaFlags

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