-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.hpp
139 lines (114 loc) · 2.42 KB
/
memory.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef _MEMORY_HPP_
#define _MEMORY_HPP_
#include "iterator.hpp"
#include "nano/type_traits.hpp"
#include "nano/uninitialized.hpp"
#include <new>
#include <cstdlib>
#include <cstddef>
namespace nano {
template< class T >
class allocator
{
public:
template <class T1>
struct rebind
{
typedef allocator<T1> other;
};
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef void* void_pointer;
typedef const void* const_void_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t diffrence_type;
static size_type max_size()
{
return size_t(-1) / sizeof(T);
}
static void* allocate(size_type bytes)
{
if (bytes <= max_size())
return malloc(bytes);
else
throw std::bad_alloc();
}
static void deallocate(pointer ptr, size_type = 0)
{
free(ptr);
}
};
template< class T, class Alloc >
class simple_allocator
{
public:
static T* allocate()
{
return (T*) Alloc::allocate(sizeof(T));
}
static T* allocate(size_t n)
{
return n == 0 ? 0 : (T*) Alloc::allocate(n * sizeof(T));
}
static void deallocate(T *p)
{
Alloc::deallocate(p);
}
static void deallocate(T *p, size_t n)
{
return n == 0 ? 0 : Alloc::deallocate(p, n * sizeof(T));
}
};
template< class T >
T* __addressof(T& arg)
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)));
}
template< class InputIt, class ForwardIt >
ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first)
{
return __uninitialized_copy(first, last, d_first, is_POD_type(*first));
}
template< class ForwardIt, class T >
void uninitialized_fill(ForwardIt first, ForwardIt last, const T& value)
{
__uninitialized_fill(first, last, value, is_POD_type(value));
}
template< class ForwardIt, class Size, class T >
void uninitialized_fill_n(ForwardIt first, Size count, const T& value)
{
__uninitialized_fill_n(first, count, value, is_POD_type(value));
}
template< class OutputIt, class T >
class raw_storage_iterator
: public iterator< output_iterator_tag, void, void, void, void >
{
explicit raw_storage_iterator(OutputIt it)
{
//TODO
}
raw_storage_iterator& operator=(const T& el)
{
//TODO
}
raw_storage_iterator& operator*()
{
//TODO
}
raw_storage_iterator& operator++()
{
//TODO
}
raw_storage_iterator operator++(int)
{
//TODO
}
};
}
#endif