mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
malloc_allocator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cerrno>
5#include <cstdint>
6#include <cstdlib>
7#include <cstring>
9#include <stdexcept>
10
11
12namespace m {
13
15struct malloc_allocator : allocator<malloc_allocator>
16{
20
23 void * allocate(size_type size, size_type alignment = 0) {
24 errno = 0;
25 if (alignment) {
26 alignment = std::max(alignment, sizeof(void*));
27 void *ptr;
28 const int err = posix_memalign(&ptr, alignment, size);
29 if (err) [[unlikely]]
30 throw std::runtime_error(strerror(err));
31 return ptr;
32 } else {
33 void *ptr = malloc(size);
34 if (ptr == nullptr) [[unlikely]] {
35 const auto errsv = errno;
36 throw std::runtime_error(strerror(errsv));
37 }
38 return ptr;
39 }
40 }
41
43 void deallocate(void *ptr, size_type size) { (void) size; free(ptr); }
44};
45
46}
‍mutable namespace
Definition: Backend.hpp:10
std::enable_if_t< not std::is_void_v< T >, T * > allocate()
Allocate space for a single entity of type T that is aligned according to Ts alignment requirement.
void deallocate(void *ptr, size_type size)
Deallocate the allocation at ptr of size size.
This allocator serves allocations using malloc/free.
void deallocate(void *ptr, size_type size)
Deallocate the allocation at ptr of size size.
void * allocate(size_type size, size_type alignment=0)
Allocate size bytes aligned to alignment.