Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
Loading...
Searching...
No Matches
slab_pool_impl.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2015 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/slab_pool_impl.h
10
//! @brief Memory pool implementation class.
11
12
#ifndef ROC_CORE_SLAB_POOL_IMPL_H_
13
#define ROC_CORE_SLAB_POOL_IMPL_H_
14
15
#include "
roc_core/align_ops.h
"
16
#include "
roc_core/attributes.h
"
17
#include "
roc_core/iarena.h
"
18
#include "
roc_core/list.h
"
19
#include "
roc_core/mutex.h
"
20
#include "
roc_core/noncopyable.h
"
21
#include "
roc_core/stddefs.h
"
22
23
namespace
roc
{
24
namespace
core
{
25
26
//! Memory pool implementation class.
27
//!
28
//! This is non-template class that implements all pool logic, to avoid
29
//! polluting header file.
30
//!
31
//! Allocated slots have the following format:
32
//! @code
33
//! +------------+------------+-----------+------------+
34
//! | SlotHeader | SlotCanary | user data | SlotCanary |
35
//! +------------+------------+-----------+------------+
36
//! @endcode
37
//!
38
//! SlotHeader contains pointer to the owning pool, checked when returning memory to
39
//! pool. SlotCanary contains magic bytes filled when returning memory to user, and
40
//! checked when returning memory to pool.
41
//!
42
//! If user data requires padding to be maximum-aligned, this padding
43
//! also becomes part of the trailing canary guard.
44
//!
45
//! @see SlabPool.
46
class
SlabPoolImpl
:
public
NonCopyable<> {
47
public
:
48
//! Slot header.
49
struct
SlotHeader
{
50
//! The pool that the slot belongs to.
51
SlabPoolImpl
*
owner
;
52
//! Variable-length data surrounded by canary guard.
53
AlignMax
data
[];
54
};
55
56
//! Canary guard which surrounds variable-length data.
57
typedef
AlignMax
SlotCanary
;
58
59
//! Initialize.
60
SlabPoolImpl
(
const
char
* name,
61
IArena
& arena,
62
size_t
object_size
,
63
size_t
min_alloc_bytes,
64
size_t
max_alloc_bytes,
65
void
* preallocated_data,
66
size_t
preallocated_size,
67
size_t
guards);
68
69
//! Deinitialize.
70
~SlabPoolImpl
();
71
72
//! Reserve memory for given number of objects.
73
ROC_ATTR_NODISCARD
bool
reserve
(
size_t
n_objects);
74
75
//! Allocate memory for an object.
76
void
*
allocate
();
77
78
//! Return memory to pool.
79
void
deallocate
(
void
* memory);
80
81
//! Get size of the allocation per object.
82
size_t
allocation_size
()
const
;
83
84
//! Get size of the object.
85
size_t
object_size
()
const
;
86
87
//! Get number of guard failures.
88
size_t
num_guard_failures
()
const
;
89
90
private
:
91
struct
Slab :
ListNode
<> {};
92
struct
Slot :
ListNode
<> {};
93
94
void
* give_slot_to_user_(Slot* slot);
95
Slot* take_slot_from_user_(
void
* memory);
96
97
Slot* acquire_slot_();
98
void
release_slot_(Slot* slot);
99
bool
reserve_slots_(
size_t
desired_slots);
100
101
void
increase_slab_size_(
size_t
desired_n_slots);
102
bool
allocate_new_slab_();
103
void
deallocate_everything_();
104
105
void
add_preallocated_memory_(
void
* memory,
size_t
memory_size);
106
107
size_t
slots_per_slab_(
size_t
slab_size,
bool
round_up)
const
;
108
size_t
slot_offset_(
size_t
slot_index)
const
;
109
110
bool
report_guard_(
size_t
guard)
const
;
111
112
Mutex mutex_;
113
114
const
char
* name_;
115
IArena& arena_;
116
117
List<Slab, NoOwnership> slabs_;
118
List<Slot, NoOwnership> free_slots_;
119
size_t
n_used_slots_;
120
121
const
size_t
slab_min_bytes_;
122
const
size_t
slab_max_bytes_;
123
124
const
size_t
unaligned_slot_size_;
125
const
size_t
slot_size_;
126
const
size_t
slab_hdr_size_;
127
128
size_t
slab_cur_slots_;
129
const
size_t
slab_max_slots_;
130
131
const
size_t
object_size_;
132
const
size_t
object_size_padding_;
133
134
const
size_t
guards_;
135
mutable
size_t
num_guard_failures_;
136
};
137
138
}
// namespace core
139
}
// namespace roc
140
141
#endif
// ROC_CORE_SLAB_POOL_IMPL_H_
align_ops.h
Alignment operations.
attributes.h
Compiler attributes.
ROC_ATTR_NODISCARD
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition
attributes.h:31
roc::core::IArena
Memory arena interface.
Definition
iarena.h:23
roc::core::ListNode
Base class for List element.
Definition
list_node.h:48
roc::core::SlabPoolImpl::reserve
ROC_ATTR_NODISCARD bool reserve(size_t n_objects)
Reserve memory for given number of objects.
roc::core::SlabPoolImpl::deallocate
void deallocate(void *memory)
Return memory to pool.
roc::core::SlabPoolImpl::SlabPoolImpl
SlabPoolImpl(const char *name, IArena &arena, size_t object_size, size_t min_alloc_bytes, size_t max_alloc_bytes, void *preallocated_data, size_t preallocated_size, size_t guards)
Initialize.
roc::core::SlabPoolImpl::object_size
size_t object_size() const
Get size of the object.
roc::core::SlabPoolImpl::num_guard_failures
size_t num_guard_failures() const
Get number of guard failures.
roc::core::SlabPoolImpl::allocate
void * allocate()
Allocate memory for an object.
roc::core::SlabPoolImpl::allocation_size
size_t allocation_size() const
Get size of the allocation per object.
roc::core::SlabPoolImpl::SlotCanary
AlignMax SlotCanary
Canary guard which surrounds variable-length data.
Definition
slab_pool_impl.h:57
roc::core::SlabPoolImpl::~SlabPoolImpl
~SlabPoolImpl()
Deinitialize.
iarena.h
Memory arena interface.
list.h
Intrusive doubly-linked list.
mutex.h
Mutex.
roc::core
General-purpose building blocks and platform abstraction layer.
roc
Root namespace.
noncopyable.h
Non-copyable object.
stddefs.h
Commonly used types and functions.
roc::core::SlabPoolImpl::SlotHeader
Slot header.
Definition
slab_pool_impl.h:49
roc::core::SlabPoolImpl::SlotHeader::data
AlignMax data[]
Variable-length data surrounded by canary guard.
Definition
slab_pool_impl.h:53
roc::core::SlabPoolImpl::SlotHeader::owner
SlabPoolImpl * owner
The pool that the slot belongs to.
Definition
slab_pool_impl.h:51
roc::core::AlignMax
Maximum aligned data unit.
Definition
align_ops.h:21
roc_core
slab_pool_impl.h
Generated by
1.17.0