-
Notifications
You must be signed in to change notification settings - Fork 7
/
Allocator.hpp
40 lines (28 loc) · 852 Bytes
/
Allocator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef ALLOCATOR_HPP
#define ALLOCATOR_HPP
#include <cstdlib>
#include "SIMDSupport.hpp"
namespace impl
{
typedef void *(*allocate_function)(size_t);
typedef void (*free_function)(void *);
};
// A template for wrapping functions as an allocator
template<impl::allocate_function alloc, impl::free_function dealloc>
struct function_allocator
{
template <typename T>
T* allocate(size_t size) { return reinterpret_cast<T*>(alloc(size * sizeof(T))); }
template <typename T>
void deallocate(T *ptr) { dealloc(ptr); }
};
using malloc_allocator = function_allocator<malloc, free>;
// Aligned allocator
struct aligned_allocator
{
template <typename T>
T* allocate(size_t size) { return allocate_aligned<T>(size); }
template <typename T>
void deallocate(T *ptr) { deallocate_aligned(ptr); }
};
#endif