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 flags);
68 
69  //! Deinitialize.
71 
72  //! Get size of objects in pool.
73  size_t object_size() const;
74 
75  //! Reserve memory for given number of objects.
76  ROC_ATTR_NODISCARD bool reserve(size_t n_objects);
77 
78  //! Allocate memory for an object.
79  void* allocate();
80 
81  //! Return memory to pool.
82  void deallocate(void* memory);
83 
84  //! Get number of guard failures.
85  size_t num_guard_failures() const;
86 
87 private:
88  struct Slab : ListNode {};
89  struct Slot : ListNode {};
90 
91  void* give_slot_to_user_(Slot* slot);
92  Slot* take_slot_from_user_(void* memory);
93 
94  Slot* acquire_slot_();
95  void release_slot_(Slot* slot);
96  bool reserve_slots_(size_t desired_slots);
97 
98  void increase_slab_size_(size_t desired_n_slots);
99  bool allocate_new_slab_();
100  void deallocate_everything_();
101 
102  void add_preallocated_memory_(void* memory, size_t memory_size);
103 
104  size_t slots_per_slab_(size_t slab_size, bool round_up) const;
105  size_t slot_offset_(size_t slot_index) const;
106 
107  Mutex mutex_;
108 
109  const char* name_;
110  IArena& arena_;
111 
112  List<Slab, NoOwnership> slabs_;
113  List<Slot, NoOwnership> free_slots_;
114  size_t n_used_slots_;
115 
116  const size_t slab_min_bytes_;
117  const size_t slab_max_bytes_;
118 
119  const size_t unaligned_slot_size_;
120  const size_t slot_size_;
121  const size_t slab_hdr_size_;
122 
123  size_t slab_cur_slots_;
124  const size_t slab_max_slots_;
125 
126  const size_t object_size_;
127  const size_t object_size_padding_;
128 
129  const size_t flags_;
130  size_t num_guard_failures_;
131 };
132 
133 } // namespace core
134 } // namespace roc
135 
136 #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:26
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 flags)
Initialize.
size_t object_size() const
Get size of objects in pool.
size_t num_guard_failures() const
Get number of guard failures.
void * allocate()
Allocate memory for an 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