Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
heap_arena.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/heap_arena.h
10//! @brief Heap arena implementation.
11
12#ifndef ROC_CORE_HEAP_ARENA_H_
13#define ROC_CORE_HEAP_ARENA_H_
14
15#include "roc_core/align_ops.h"
16#include "roc_core/atomic.h"
17#include "roc_core/iarena.h"
19
20namespace roc {
21namespace core {
22
23//! Heap arena flags.
25 //! Enable panic if leaks detected in arena destructor.
27 //! Enable panic if memory violation detected when deallocating chunk.
29};
30
31//! Default heap arena flags.
32enum { DefaultHeapArenaFlags = (HeapArenaFlag_EnableGuards) };
33
34//! Heap arena implementation.
35//!
36//! Uses malloc() and free().
37//!
38//! The memory is always maximum aligned.
39//!
40//! Implements three safety measures:
41//! - to catch double-free and other logical bugs, inserts link to owning arena before
42//! user data, and panics if it differs when memory is returned to arena
43//! - to catch buffer overflow bugs, inserts "canary guards" before and after user
44//! data, and panics if they are overwritten when memory is returned to arena
45//! - to catch uninitialized-access and use-after-free bugs, "poisons" memory when it
46//! returned to user, and when it returned back to the arena
47//!
48//! Allocated chunks have the following format:
49//! @code
50//! +-------------+-------------+-----------+-------------+
51//! | ChunkHeader | ChunkCanary | user data | ChunkCanary |
52//! +-------------+-------------+-----------+-------------+
53//! @endcode
54//!
55//! ChunkHeader contains pointer to the owning arena, checked when returning memory to
56//! arena. ChunkCanary contains magic bytes filled when returning memory to user, and
57//! checked when returning memory to arena.
58//!
59//! Thread-safe.
60class HeapArena : public IArena, public NonCopyable<> {
61public:
62 //! Initialize.
64 ~HeapArena();
65
66 //! Set flags, for all instances.
67 //!
68 //! @b Parameters
69 //! - @p flags defines options to modify behaviour as indicated in HeapArenaFlags
70 static void set_flags(size_t flags);
71
72 //! Get number of allocated blocks.
73 size_t num_allocations() const;
74
75 //! Get number of guard failures.
76 size_t num_guard_failures() const;
77
78 //! Allocate memory.
79 virtual void* allocate(size_t size);
80
81 //! Deallocate previously allocated memory.
82 virtual void deallocate(void*);
83
84private:
85 struct ChunkHeader {
86 // The heap arena that the chunk belongs to.
87 HeapArena* owner;
88 // Data size, excluding canary guards.
89 size_t size;
90 // Data surrounded with canary guards.
91 AlignMax data[];
92 };
93
94 typedef AlignMax ChunkCanary;
95
96 Atomic<int> num_allocations_;
97 Atomic<size_t> num_guard_failures_;
98
99 static size_t flags_;
100};
101
102} // namespace core
103} // namespace roc
104
105#endif // ROC_CORE_HEAP_ARENA_H_
Alignment operations.
Atomic.
Heap arena implementation.
Definition heap_arena.h:60
static void set_flags(size_t flags)
Set flags, for all instances.
size_t num_guard_failures() const
Get number of guard failures.
size_t num_allocations() const
Get number of allocated blocks.
virtual void * allocate(size_t size)
Allocate memory.
virtual void deallocate(void *)
Deallocate previously allocated memory.
HeapArena()
Initialize.
Memory arena interface.
Definition iarena.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
Memory arena interface.
HeapArenaFlags
Heap arena flags.
Definition heap_arena.h:24
@ HeapArenaFlag_EnableLeakDetection
Enable panic if leaks detected in arena destructor.
Definition heap_arena.h:26
@ HeapArenaFlag_EnableGuards
Enable panic if memory violation detected when deallocating chunk.
Definition heap_arena.h:28
Root namespace.
Non-copyable object.
Maximum aligned data unit.
Definition align_ops.h:21