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*) = 0;
35 
36  //! Destroy object and deallocate its memory.
37  template <class T> void destroy_object(T& object) {
38  object.~T();
39  deallocate(&object);
40  }
41 };
42 
43 } // namespace core
44 } // namespace roc
45 
46 //! Placement new for core::IArena.
47 //! @note
48 //! nothrow forces compiler to check for NULL return value before calling ctor.
49 inline void* operator new(size_t size, roc::core::IArena& arena) throw() {
50  return arena.allocate(size);
51 }
52 
53 //! Placement new[] for core::IArena.
54 //! @note
55 //! nothrow forces compiler to check for NULL return value before calling ctor.
56 inline void* operator new[](size_t size, roc::core::IArena& arena) throw() {
57  return arena.allocate(size);
58 }
59 
60 //! Placement delete for core::IArena.
61 //! @note
62 //! Compiler calls this if ctor throws in a placement new expression.
63 template <class T>
64 inline void operator delete(void* ptr, roc::core::IArena& arena) throw() {
65  arena.deallocate(ptr);
66 }
67 
68 //! Placement delete[] for core::IArena.
69 //! @note
70 //! Compiler calls this if ctor throws in a placement new[] expression.
71 template <class T>
72 inline void operator delete[](void* ptr, roc::core::IArena& arena) throw() {
73  arena.deallocate(ptr);
74 }
75 
76 #endif // ROC_CORE_IARENA_H_
Compiler attributes.
Memory arena interface.
Definition: iarena.h:23
virtual void deallocate(void *)=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:37
Root namespace.
Panic.
Commonly used types and functions.