Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
ipool.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 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/ipool.h
10 //! @brief Memory pool interface.
11 
12 #ifndef ROC_CORE_IPOOL_H_
13 #define ROC_CORE_IPOOL_H_
14 
15 #include "roc_core/attributes.h"
16 #include "roc_core/panic.h"
17 #include "roc_core/stddefs.h"
18 
19 namespace roc {
20 namespace core {
21 
22 //! Memory pool interface.
23 class IPool {
24 public:
25  virtual ~IPool();
26 
27  //! Get size of the allocation per object.
28  //! Covers all internal overhead, if any.
29  virtual size_t allocation_size() const = 0;
30 
31  //! Get size of the object (without overhead).
32  virtual size_t object_size() const = 0;
33 
34  //! Reserve memory for given number of objects.
35  //! @returns
36  //! false if allocation failed.
37  virtual ROC_ATTR_NODISCARD bool reserve(size_t n_objects) = 0;
38 
39  //! Allocate memory for an object.
40  //! @returns
41  //! pointer to a maximum aligned uninitialized memory for a new object
42  //! or NULL if memory can't be allocated.
43  virtual void* allocate() = 0;
44 
45  //! Return memory to pool.
46  virtual void deallocate(void* memory) = 0;
47 
48  //! Destroy object and deallocate its memory.
49  template <class T> void destroy_object(T& object) {
50  object.~T();
51  deallocate(&object);
52  }
53 };
54 
55 } // namespace core
56 } // namespace roc
57 
58 //! Placement new for core::IPool.
59 //! @note
60 //! nothrow forces compiler to check for NULL return value before calling ctor.
61 inline void* operator new(size_t size, roc::core::IPool& pool) throw() {
62  roc_panic_if(pool.object_size() < size);
63  return pool.allocate();
64 }
65 
66 //! Placement delete for core::IPool.
67 //! @note
68 //! Compiler calls this if ctor throws in a placement new expression.
69 inline void operator delete(void* ptr, roc::core::IPool& pool) throw() {
70  pool.deallocate(ptr);
71 }
72 
73 #endif // ROC_CORE_IPOOL_H_
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Memory pool interface.
Definition: ipool.h:23
virtual ROC_ATTR_NODISCARD bool reserve(size_t n_objects)=0
Reserve memory for given number of objects.
virtual size_t allocation_size() const =0
Get size of the allocation per object. Covers all internal overhead, if any.
virtual size_t object_size() const =0
Get size of the object (without overhead).
virtual void deallocate(void *memory)=0
Return memory to pool.
virtual void * allocate()=0
Allocate memory for an object.
void destroy_object(T &object)
Destroy object and deallocate its memory.
Definition: ipool.h:49
Root namespace.
Panic.
#define roc_panic_if(x)
Panic if condition is true.
Definition: panic.h:26
Commonly used types and functions.