-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmalloc_allocator.hpp
55 lines (43 loc) · 1.03 KB
/
malloc_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
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <memory>
template<typename T>
struct malloc_allocator_t : std::allocator<T>
{
malloc_allocator_t() = default;
template<class U>
malloc_allocator_t(const malloc_allocator_t<U>&) noexcept {}
T* allocate(std::size_t n)
{
T* ptr = (T*)std::malloc(n * sizeof(T));
if (!ptr) throw std::bad_alloc();
return ptr;
}
void deallocate(T* ptr, std::size_t) { std::free(ptr); }
template<typename U>
struct rebind { typedef malloc_allocator_t<U> other; };
};
/*
void* raw = std::malloc(sizeof(entry_set_t));
if (!raw) throw std::bad_alloc();
entry_table = new (raw) entry_set_t;
*/
template <typename T, typename ...Args>
T* malloc_new(Args&& ...args)
{
void* raw = std::malloc(sizeof(T));
if (!raw) throw std::bad_alloc();
return new(raw) T{std::forward<Args>(args)...};
}
template <typename T, typename ...Args>
void malloc_create(T** ptr, Args&& ...args)
{
*ptr = malloc_new<T>(std::forward<Args>(args)...);
}
template <typename T>
void free_new(T* ptr)
{
if (!ptr)
return;
ptr->~T();
std::free(ptr);
}