VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
vae_allocators.hpp
Go to the documentation of this file.
1#ifndef _VAE_ALLOCATORS
2#define _VAE_ALLOCATORS
3
4#include <cstdlib>
5#include <cstddef>
6#include <new>
7#include <cstddef>
8#include <limits>
9#include "../wrapped/vae_profiler.hpp"
10
11namespace vae { namespace core { namespace memory {
12 /**
13 * @brief Allocator used for all heapbuffers in VAE
14 * @tparam T
15 */
16 template <class T, class NAME>
17 struct Allocator {
18 typedef T value_type;
19
20 Allocator() = default;
21 Allocator(const Allocator&) = default;
22 template <class U, class J >
24
25 T* allocate(size_t n) noexcept {
26 if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) {
27 return nullptr;
28 }
29
30 if (auto ptr = static_cast<T*>(malloc(n * sizeof(T)))) {
31 VAE_PROFILER_MALLOC_L(ptr, n * sizeof(T), NAME::name)
32 return ptr;
33 }
34
35 return nullptr;
36 }
37
38 void deallocate(T* ptr, std::size_t n) noexcept {
39 VAE_PROFILER_FREE_L(ptr, NAME::name)
40 free(ptr);
41 }
42 };
43
44 /**
45 * @brief I don't even know what this does, but it has to be here for some classes using
46 */
47 template <class T, class U, class J >
49 return true;
50 }
51
52 template <class T, class U, class J >
54 return false;
55 }
56
57 /**
58 * @brief This is here to get a name into the allocator without writing the whole thing again
59 */
60 struct FsAllocatorName { static constexpr const char* name = "FS Allocator"; };
61 struct MainAllocatorName { static constexpr const char* name = "Main Allocator"; };
62 struct AudioAllocatorName { static constexpr const char* name = "Audio Allocator"; };
63 struct ScratchAllocatorName { static constexpr const char* name = "Scratch Allocator"; };
64
65 template <class T> using AllocatorFS = Allocator<T, FsAllocatorName>;
69
70} } } // vae::core::memory
71
72#endif // _VAE_ALLOCATORS
#define malloc(size)
Force all other standart allocation operators to use the ones provided by tklb.
#define free(ptr)
T max(const T &v1, const T &v2)
Definition: TMath.hpp:21
bool operator==(const Allocator< T, J > &, const Allocator< U, J > &)
I don't even know what this does, but it has to be here for some classes using.
bool operator!=(const Allocator< T, J > &, const Allocator< U, J > &)
Contains Typedefinitions and basic structures use by the public API and internally.
Definition: vae.hpp:31
Allocator used for all heapbuffers in VAE.
T * allocate(size_t n) noexcept
Allocator(const Allocator &)=default
void deallocate(T *ptr, std::size_t n) noexcept
Allocator(const Allocator< U, J > &)
static constexpr const char * name
This is here to get a name into the allocator without writing the whole thing again.
static constexpr const char * name
static constexpr const char * name
static constexpr const char * name
#define VAE_PROFILER_FREE_L(ptr, name)
Track named allocaions.
#define VAE_PROFILER_MALLOC_L(ptr, size, name)
Track named allocaions.