-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory
224 lines (182 loc) · 5.68 KB
/
memory
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* @file "sps/memory"
* @author Jens Munk Hansen <[email protected]>
* @date Sun May 7 10:34:25 2017
*
* @brief
*
*
*/
#pragma once
#include <sps/cenv.h>
#include <sps/mm_malloc.h>
#include <sps/malloc.h>
#ifndef __cplusplus
# error This is a C++ header
#endif
#include <memory>
#include <functional>
#include <type_traits>
#include <sps/align.hpp>
#ifdef WIN32
# include <sps/win32/memory>
#else
# include <sps/unix/memory>
#endif
// TODO: Enable this
#if 0
namespace sps {
template <class T>
void fillmem(T* t, int val, std::size_t size){
static_assert(std::is_trivially_copyable<T>::value, "will trigger UB when calling memset on it");
std::memset(t, val, size);
}
template <typename T, class = typename std::enable_if<!std::is_pointer<T>::value>::type>
void fillmem(T& t, int val = 0, std::size_t size = sizeof(T)){
static_assert(std::is_trivially_copyable<T>::value, "will trigger UB when calling memset on it");
assert(size <= sizeof(T));
fillmem(std::addressof(t), val, size);
}
template <class T>
void fillmem(T&, std::size_t, int) = delete;
#pragma GCC poison memset
template <typename T, class = typename std::enable_if<!std::is_pointer<T>::value>::type>
void fillmem(T& t, int val = 0, std::size_t size = sizeof(T)){
static_assert(std::is_trivially_copyable<T>::value, "will trigger UB when calling memset on it");
assert(size <= sizeof(T));
fillmem(&t, val, size);
}
template<class T>
void fillmem(T* t, int val, std::size_t size){
static_assert(std::is_trivially_copyable<T>::value, "will trigger UB when calling memset on it");
std::fill_n(reinterpret_cast<unsigned char*>(t), size, val);
}
template<class T>
void fillmem(T&, std::size_t, int) = delete;
// MSVC
#pragma deprecated("token")
#pragma warning(error: 4995)
} // namespace sps
#endif
// Just a note: replace memset, memcpy, memcmp, memmove with std::fill(_n), std::copy,
// and std::equal(std::lexicographical_compare), std::move
#ifndef CXX11
// Backport std::make_unique from C++14
namespace std {
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args&& ...args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace std
#endif // CX11
// TODO(JMH): Consider requiring descendant to be non-copyconstructible
template<bool B, typename T = void> using disable_if = std::enable_if<!B, T>;
namespace sps {
#ifdef CXX11
template <typename T>
T* malloc(size_t nData) {
void *ptr = ::malloc(nData*sizeof(T));
if (!ptr) {
fprintf(stderr, "Could not allocate: %zu bytes\n", nData*sizeof(T));
exit(EXIT_FAILURE);
}
return static_cast<T*>(ptr);
}
template <typename T>
class padded {
public:
template <typename... Args>
padded(Args&&... args) {
new (&_.t) T(std::forward<Args>(args)...);
}
~padded() { _.t.~T(); }
// Accessors
T& access() { return _.t; }
T const& get() const { return _.t; }
private:
union U {
U() { memset(this, 0, sizeof(T)); }
char buffer[sizeof(T)];
T t;
} _;
}; // class padded
#endif
#if (defined(__GNUG__) && __cplusplus >= 201103L)
using sps::nix::unique_aligned_array;
using sps::nix::unique_aligned_array_create;
using sps::nix::unique_aligned_multi_array;
using sps::nix::unique_aligned_multi_array_create;
// Alias type to unique_multi_array
template <class T, size_t I, class H = typename std::enable_if<std::is_pod<T>::value>::type>
using unique_multi_array = sps::nix::unique_aligned_multi_array<T, I, H>;
// Alias function to unique_multi_array_create (TODO: Do this correctly)
#if 0
template <class T, size_t I>
inline typename std::enable_if<I==2, unique_aligned_multi_array<T,2> >::type
unique_multi_array_create(size_t m, size_t n) {
return sps::nix::unique_aligned_multi_array_create<T,I>(m,n);
}
#elif 1
// Works
template <class T, size_t I, typename... Args>
auto unique_multi_array_create(Args&&... args) -> decltype(sps::nix::unique_aligned_multi_array_create<T,I>(std::forward<Args>(args)...)) {
return sps::nix::unique_aligned_multi_array_create<T,I>(args...);
}
#else
// How
template <typename ...ExplicitArgs, typename... Args>
auto unique_multi_array_create(Args&&... args) -> decltype(sps::nix::unique_aligned_multi_array_create<ExplicitArgs...>(std::forward<Args>(args)...)) {
return sps::nix::unique_aligned_multi_array_create<ExplicitArgs...>(args...);
}
#endif
// Leveled pointers and pointer levels
template <typename T>
struct ptr_depth_impl
{
enum { level = 0 };
};
template <typename T>
struct ptr_depth_impl<T* const volatile>
{
enum { level = ptr_depth_impl<T const volatile>::level + 1 };
};
template <typename T>
struct ptr_depth
{
enum { level = ptr_depth_impl<T const volatile>::level };
};
template <typename T, size_t lvl>
struct depth_ptr
{
using value_type = typename depth_ptr<T*,lvl-1>::value_type;
};
#ifndef __clang__
// Can we do this in general
// template <>
#endif
template <typename T>
struct depth_ptr<T,0> {
using value_type = T;
};
#elif defined(_MSC_VER)
// TODO: Figure out how Microsoft reveals true C++11
using sps::win32::unique_aligned_array;
using sps::win32::unique_aligned_array_create;
using sps::win32::unique_aligned_multi_array;
using sps::win32::unique_aligned_multi_array_create;
#if 0
using sps::nix::unique_aligned_array;
using sps::nix::unique_aligned_array_create;
using sps::nix::unique_aligned_multi_array;
using sps::nix::unique_aligned_multi_array_create;
#endif
#else
#error This library needs at least a C++11 compliant compiler
#endif
} // namespace std
/* Local variables: */
/* mode: c++ */
/* indent-tabs-mode: nil */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* End: */