Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
Loading...
Searching...
No Matches
allocation_policy.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2022 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/allocation_policy.h
10
//! @brief Allocation policies.
11
12
#ifndef ROC_CORE_ALLOCATION_POLICY_H_
13
#define ROC_CORE_ALLOCATION_POLICY_H_
14
15
#include "
roc_core/iarena.h
"
16
#include "
roc_core/ipool.h
"
17
#include "
roc_core/panic.h
"
18
19
namespace
roc
{
20
namespace
core
{
21
22
//! Allocation policy for objects allocated using IArena.
23
class
ArenaAllocation
{
24
public
:
25
//! Initialize.
26
ArenaAllocation
(
IArena
&
arena
)
27
: arena_(&
arena
) {
28
}
29
30
//! Destroy object and return memory to arena.
31
template
<
class
T>
void
destroy
(T&
object
) {
32
arena_->destroy_object(
object
);
33
}
34
35
protected
:
36
//! Get arena.
37
IArena
&
arena
()
const
{
38
return
*arena_;
39
}
40
41
private
:
42
IArena
* arena_;
43
};
44
45
//! Allocation policy for objects allocated using IPool.
46
class
PoolAllocation
{
47
public
:
48
//! Initialize.
49
PoolAllocation
(
IPool
&
pool
)
50
: pool_(&
pool
) {
51
}
52
53
//! Destroy object and return memory to pool.
54
template
<
class
T>
void
destroy
(T&
object
) {
55
pool_->destroy_object(
object
);
56
}
57
58
protected
:
59
//! Get pool.
60
IPool
&
pool
()
const
{
61
return
*pool_;
62
}
63
64
private
:
65
IPool
* pool_;
66
};
67
68
//! Allocation policy for objects with custom deallocation function.
69
class
CustomAllocation
{
70
typedef
void (*DestroyFunc)(
void
*);
71
72
public
:
73
//! Initialize.
74
template
<
class
T>
75
CustomAllocation
(
void
(*destroy_func)(T*))
76
: destroy_func_((DestroyFunc)destroy_func) {
77
if
(!destroy_func_) {
78
roc_panic
(
"allocation policy: null function"
);
79
}
80
}
81
82
//! Invoke custom destruction function.
83
template
<
class
T>
void
destroy
(T&
object
) {
84
destroy_func_(&
object
);
85
}
86
87
private
:
88
DestroyFunc destroy_func_;
89
};
90
91
//! Allocation policy for objects that does not have automatic deallocation.
92
class
ManualAllocation
{
93
public
:
94
//! No-op.
95
//! When SharedPtr or ScopedPtr "destroys" object, nothing happens.
96
//! The user is responsible for destroying it manually.
97
template
<
class
T>
void
destroy
(T&) {
98
}
99
};
100
101
}
// namespace core
102
}
// namespace roc
103
104
#endif
// ROC_CORE_ALLOCATION_POLICY_H_
roc::core::ArenaAllocation::ArenaAllocation
ArenaAllocation(IArena &arena)
Initialize.
Definition
allocation_policy.h:26
roc::core::ArenaAllocation::destroy
void destroy(T &object)
Destroy object and return memory to arena.
Definition
allocation_policy.h:31
roc::core::ArenaAllocation::arena
IArena & arena() const
Get arena.
Definition
allocation_policy.h:37
roc::core::CustomAllocation::CustomAllocation
CustomAllocation(void(*destroy_func)(T *))
Initialize.
Definition
allocation_policy.h:75
roc::core::CustomAllocation::destroy
void destroy(T &object)
Invoke custom destruction function.
Definition
allocation_policy.h:83
roc::core::IArena
Memory arena interface.
Definition
iarena.h:23
roc::core::IPool
Memory pool interface.
Definition
ipool.h:23
roc::core::ManualAllocation
Allocation policy for objects that does not have automatic deallocation.
Definition
allocation_policy.h:92
roc::core::ManualAllocation::destroy
void destroy(T &)
No-op. When SharedPtr or ScopedPtr "destroys" object, nothing happens. The user is responsible for de...
Definition
allocation_policy.h:97
roc::core::PoolAllocation::PoolAllocation
PoolAllocation(IPool &pool)
Initialize.
Definition
allocation_policy.h:49
roc::core::PoolAllocation::pool
IPool & pool() const
Get pool.
Definition
allocation_policy.h:60
roc::core::PoolAllocation::destroy
void destroy(T &object)
Destroy object and return memory to pool.
Definition
allocation_policy.h:54
iarena.h
Memory arena interface.
ipool.h
Memory pool interface.
roc::core
General-purpose building blocks and platform abstraction layer.
roc
Root namespace.
panic.h
Panic.
roc_panic
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition
panic.h:50
roc_core
allocation_policy.h
Generated by
1.17.0