-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibc_allocator.hpp
47 lines (35 loc) · 1.27 KB
/
libc_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
#pragma once
#include <new>
#include <limits>
#include <cstddef>
#include "libc_malloc.hpp"
/**
* This is an allocator using __libc_* functions.
* It can be used in the malloc() function.
*/
template <class T> struct libc_allocator {
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using const_pointer = const T *;
using reference = T &;
using const_reference = const T &;
using value_type = T;
template <class U> struct rebind { typedef libc_allocator<U> other; };
libc_allocator() throw() {}
libc_allocator(const libc_allocator &) throw() {}
template <class U> libc_allocator(const libc_allocator<U> &) throw() {}
~libc_allocator() throw() {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type s, void const * = 0) {
if (s == 0) return nullptr;
pointer temp = (pointer)__libc_malloc(s * sizeof(T));
if (temp == nullptr) throw std::bad_alloc();
return temp;
}
void deallocate(pointer p, size_type) { __libc_free(p); }
size_type max_size() const throw() { return std::numeric_limits<size_t>::max() / sizeof(T); }
void construct(pointer p, const T &val) { new ((void *)p) T(val); }
void destroy(pointer p) { p->~T(); }
};