Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
iallocator.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/iallocator.h
10//! @brief Memory allocator interface.
11
12#ifndef ROC_CORE_IALLOCATOR_H_
13#define ROC_CORE_IALLOCATOR_H_
14
15#include "roc_core/attributes.h"
16#include "roc_core/panic.h"
17#include "roc_core/stddefs.h"
18
19namespace roc {
20namespace core {
21
22//! Memory allocator interface.
24public:
25 virtual ~IAllocator();
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::IAllocator.
47//! @note
48//! nothrow forces compiler to check for NULL return value before calling ctor.
49inline void* operator new(size_t size, roc::core::IAllocator& allocator) throw() {
50 return allocator.allocate(size);
51}
52
53//! Placement new[] for core::IAllocator.
54//! @note
55//! nothrow forces compiler to check for NULL return value before calling ctor.
56inline void* operator new[](size_t size, roc::core::IAllocator& allocator) throw() {
57 return allocator.allocate(size);
58}
59
60//! Placement delete for core::IAllocator.
61//! @note
62//! Compiler calls this if ctor throws in a placement new expression.
63template <class T>
64inline void operator delete(void* ptr, roc::core::IAllocator& allocator) throw() {
65 allocator.deallocate(ptr);
66}
67
68//! Placement delete[] for core::IAllocator.
69//! @note
70//! Compiler calls this if ctor throws in a placement new[] expression.
71template <class T>
72inline void operator delete[](void* ptr, roc::core::IAllocator& allocator) throw() {
73 allocator.deallocate(ptr);
74}
75
76#endif // ROC_CORE_IALLOCATOR_H_
Compiler attributes.
Memory allocator interface.
Definition: iallocator.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: iallocator.h:37
Root namespace.
Panic.
Commonly used types and functions.