Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
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/iallocator.h"
16#include "roc_core/panic.h"
17
18namespace roc {
19namespace core {
20
21//! Allocation policy for objects (de)allocated using IAllocator.
23public:
24 //! Initialize in invalid state.
25 //! Such instance wont be usable.
27 : allocator_(NULL) {
28 }
29
30 //! Initialize with given allocator.
31 //! Such instance will use allocator to destroy objects.
33 : allocator_(&allocator) {
34 }
35
36 //! Destroy object and deallocate its memory.
37 template <class T> void destroy(T& object) {
38 if (!allocator_) {
39 roc_panic("allocation policy: null allocator");
40 }
41 allocator_->destroy_object(object);
42 }
43
44protected:
45 //! Get allocator.
47 if (!allocator_) {
48 roc_panic("allocation policy: null allocator");
49 }
50 return *allocator_;
51 }
52
53private:
54 IAllocator* allocator_;
55};
56
57//! Allocation policy for objects (de)allocated using speciailized factory.
58template <class Factory> class FactoryAllocation {
59public:
60 //! Initialize in invalid state.
61 //! Such instance wont be usable.
63 : factory_(NULL) {
64 }
65
66 //! Initialize with given factory.
67 //! Such instance will use factory to destroy objects.
69 : factory_(&factory) {
70 }
71
72 //! Destroy object and deallocate its memory.
73 template <class T> void destroy(T& object) {
74 if (!factory_) {
75 roc_panic("allocation policy: null factory");
76 }
77 factory_->destroy(object);
78 }
79
80protected:
81 //! Get factory.
82 Factory& factory() const {
83 if (!factory_) {
84 roc_panic("allocation policy: null factory");
85 }
86 return *factory_;
87 }
88
89private:
90 Factory* factory_;
91};
92
93//! Allocation policy for objects (de)allocated using custom functions.
95 typedef void (*DestroyFunc)(void*);
96
97public:
98 //! Initialize in invalid state.
99 //! Such instance wont be usable.
101 : destroy_func_(NULL) {
102 }
103
104 //! Initialize with given function.
105 //! Such instance will use function to destroy objects.
106 template <class T>
107 CustomAllocation(void (*destroy_func)(T*))
108 : destroy_func_((DestroyFunc)destroy_func) {
109 }
110
111 //! Destroy object and deallocate its memory.
112 template <class T> void destroy(T& object) {
113 if (!destroy_func_) {
114 roc_panic("allocation policy: null func");
115 }
116 destroy_func_(&object);
117 }
118
119private:
120 DestroyFunc destroy_func_;
121};
122
123} // namespace core
124} // namespace roc
125
126#endif // ROC_CORE_ALLOCATION_POLICY_H_
Allocation policy for objects (de)allocated using custom functions.
CustomAllocation()
Initialize in invalid state. Such instance wont be usable.
CustomAllocation(void(*destroy_func)(T *))
Initialize with given function. Such instance will use function to destroy objects.
void destroy(T &object)
Destroy object and deallocate its memory.
Allocation policy for objects (de)allocated using speciailized factory.
Factory & factory() const
Get factory.
FactoryAllocation()
Initialize in invalid state. Such instance wont be usable.
void destroy(T &object)
Destroy object and deallocate its memory.
FactoryAllocation(Factory &factory)
Initialize with given factory. Such instance will use factory to destroy objects.
Memory allocator interface.
Definition: iallocator.h:23
void destroy_object(T &object)
Destroy object and deallocate its memory.
Definition: iallocator.h:37
Allocation policy for objects (de)allocated using IAllocator.
void destroy(T &object)
Destroy object and deallocate its memory.
StandardAllocation(IAllocator &allocator)
Initialize with given allocator. Such instance will use allocator to destroy objects.
IAllocator & allocator() const
Get allocator.
StandardAllocation()
Initialize in invalid state. Such instance wont be usable.
Memory allocator interface.
Root namespace.
Panic.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:50