Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
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.
52  //! Variable-length data surrounded by canary guard.
54  };
55 
56  //! Canary guard which surrounds variable-length data.
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.
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_
Alignment operations.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Memory arena interface.
Definition: iarena.h:23
Base class for List element.
Definition: list_node.h:48
Base class for non-copyable objects.
Definition: noncopyable.h:23
Memory pool implementation class.
ROC_ATTR_NODISCARD bool reserve(size_t n_objects)
Reserve memory for given number of objects.
void deallocate(void *memory)
Return memory to pool.
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.
size_t object_size() const
Get size of the object.
size_t num_guard_failures() const
Get number of guard failures.
void * allocate()
Allocate memory for an object.
size_t allocation_size() const
Get size of the allocation per object.
AlignMax SlotCanary
Canary guard which surrounds variable-length data.
~SlabPoolImpl()
Deinitialize.
Memory arena interface.
Intrusive doubly-linked list.
Mutex.
Root namespace.
Non-copyable object.
Commonly used types and functions.
AlignMax data[]
Variable-length data surrounded by canary guard.
SlabPoolImpl * owner
The pool that the slot belongs to.
Maximum aligned data unit.
Definition: align_ops.h:21