Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
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.
24 public:
25  //! Initialize.
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.
47 public:
48  //! Initialize.
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.
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 automatical deallocation.
93 public:
94  //! No-op.
95  //! When SharedPtr or ScopedPtr "destroys" object, nothing happens.
96  //! The user is reponsible 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_
Allocation policy for objects allocated using IArena.
ArenaAllocation(IArena &arena)
Initialize.
void destroy(T &object)
Destroy object and return memory to arena.
IArena & arena() const
Get arena.
Allocation policy for objects with custom deallocation function.
CustomAllocation(void(*destroy_func)(T *))
Initialize.
void destroy(T &object)
Invoke custom destruction function.
Memory arena interface.
Definition: iarena.h:23
void destroy_object(T &object)
Destroy object and deallocate its memory.
Definition: iarena.h:37
Memory pool interface.
Definition: ipool.h:23
void destroy_object(T &object)
Destroy object and deallocate its memory.
Definition: ipool.h:45
Allocation policy for objects that does not have automatical deallocation.
void destroy(T &)
No-op. When SharedPtr or ScopedPtr "destroys" object, nothing happens. The user is reponsible for des...
Allocation policy for objects allocated using IPool.
PoolAllocation(IPool &pool)
Initialize.
IPool & pool() const
Get pool.
void destroy(T &object)
Destroy object and return memory to pool.
Memory arena interface.
Memory pool interface.
Root namespace.
Panic.
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:50