Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
iarena.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/iarena.h
10 //! @brief Memory arena interface.
11 
12 #ifndef ROC_CORE_IARENA_H_
13 #define ROC_CORE_IARENA_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 arena interface.
23 class IArena {
24 public:
25  virtual ~IArena();
26 
27  //! Allocate memory.
28  //! @returns
29  //! pointer to a maximum aligned uninitialized memory at least of @p size
30  //! bytes or NULL if memory can't be allocated.
31  virtual void* allocate(size_t size) = 0;
32 
33  //! Deallocate previously allocated memory.
34  virtual void deallocate(void* ptr) = 0;
35 
36  //! Computes how many bytes will be actually allocated if allocate() is called with
37  //! given size. Covers all internal overhead, if any.
38  virtual size_t compute_allocated_size(size_t size) const = 0;
39 
40  //! Returns how many bytes was allocated for given pointer returned by allocate().
41  //! Covers all internal overhead, if any.
42  //! Returns same value as computed by compute_allocated_size(size).
43  virtual size_t allocated_size(void* ptr) const = 0;
44 
45  //! Destroy object and deallocate its memory.
46  template <class T> void destroy_object(T& object) {
47  object.~T();
48  deallocate(&object);
49  }
50 };
51 
52 } // namespace core
53 } // namespace roc
54 
55 //! Placement new for core::IArena.
56 //! @note
57 //! nothrow forces compiler to check for NULL return value before calling ctor.
58 inline void* operator new(size_t size, roc::core::IArena& arena) throw() {
59  return arena.allocate(size);
60 }
61 
62 //! Placement new[] for core::IArena.
63 //! @note
64 //! nothrow forces compiler to check for NULL return value before calling ctor.
65 inline void* operator new[](size_t size, roc::core::IArena& arena) throw() {
66  return arena.allocate(size);
67 }
68 
69 //! Placement delete for core::IArena.
70 //! @note
71 //! Compiler calls this if ctor throws in a placement new expression.
72 template <class T>
73 inline void operator delete(void* ptr, roc::core::IArena& arena) throw() {
74  arena.deallocate(ptr);
75 }
76 
77 //! Placement delete[] for core::IArena.
78 //! @note
79 //! Compiler calls this if ctor throws in a placement new[] expression.
80 template <class T>
81 inline void operator delete[](void* ptr, roc::core::IArena& arena) throw() {
82  arena.deallocate(ptr);
83 }
84 
85 #endif // ROC_CORE_IARENA_H_
Compiler attributes.
Memory arena interface.
Definition: iarena.h:23
virtual void deallocate(void *ptr)=0
Deallocate previously allocated memory.
virtual void * allocate(size_t size)=0
Allocate memory.
void destroy_object(T &object)
Destroy object and deallocate its memory.
Definition: iarena.h:46
virtual size_t allocated_size(void *ptr) const =0
Returns how many bytes was allocated for given pointer returned by allocate(). Covers all internal ov...
virtual size_t compute_allocated_size(size_t size) const =0
Computes how many bytes will be actually allocated if allocate() is called with given size....
Root namespace.
Panic.
Commonly used types and functions.