Repository navigation
Expand file tree
/
Copy pathmemory.h
More file actions
198 lines (174 loc) · 4.29 KB
/
Copy pathmemory.h
File metadata and controls
198 lines (174 loc) · 4.29 KB
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
#ifndef ZFP_MEMORY_H
#define ZFP_MEMORY_H
// Memory management for POD types only. Templated functions are provided only
// to avoid the need for casts to/from void* in pass-by-reference calls.
#ifdef _WIN32
extern "C" {
#ifdef __MINGW32__
#include <x86intrin.h>
#endif
#include <malloc.h>
}
#endif
#include <algorithm>
#include <cstdlib>
#include <stdexcept>
// byte alignment of compressed data
#ifndef ZFP_MEMORY_ALIGNMENT
#define ZFP_MEMORY_ALIGNMENT 0x100u
#endif
#define unused_(x) ((void)(x))
namespace zfp {
// allocate size bytes
inline void*
allocate(size_t size)
{
void* ptr = std::malloc(size);
if (!ptr)
throw std::bad_alloc();
return ptr;
}
// allocate size bytes with suggested alignment
inline void*
allocate_aligned(size_t size, size_t alignment)
{
void* ptr = 0;
#ifdef ZFP_WITH_ALIGNED_ALLOC
#if defined(__INTEL_COMPILER)
ptr = _mm_malloc(size, alignment);
#elif defined(__MINGW32__)
// require: alignment is an integer power of two
ptr = __mingw_aligned_malloc(size, alignment);
#elif defined(_WIN32)
// require: alignment is an integer power of two
ptr = _aligned_malloc(size, alignment);
#elif defined(__MACH__) || (_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)
// require: alignment is an integer power of two >= sizeof(void*)
posix_memalign(&ptr, alignment, size);
#else
// aligned allocation not supported; fall back on unaligned allocation
unused_(alignment);
ptr = allocate(size);
#endif
#else
// aligned allocation not enabled; use unaligned allocation
unused_(alignment);
ptr = allocate(size);
#endif
if (!ptr)
throw std::bad_alloc();
return ptr;
}
// deallocate memory pointed to by ptr
inline void
deallocate(void* ptr)
{
std::free(ptr);
}
// deallocate aligned memory pointed to by ptr
inline void
deallocate_aligned(void* ptr)
{
if (!ptr)
return;
#ifdef ZFP_WITH_ALIGNED_ALLOC
#ifdef __INTEL_COMPILER
_mm_free(ptr);
#elif defined(__MINGW32__)
__mingw_aligned_free(ptr);
#elif defined(_WIN32)
_aligned_free(ptr);
#else
std::free(ptr);
#endif
#else
std::free(ptr);
#endif
}
// reallocate buffer to size bytes
template <typename T>
inline void
reallocate(T*& ptr, size_t size, bool preserve = false)
{
if (preserve)
ptr = static_cast<T*>(std::realloc(ptr, size));
else {
zfp::deallocate(ptr);
ptr = static_cast<T*>(zfp::allocate(size));
}
}
// reallocate buffer to new_size bytes with suggested alignment
template <typename T>
inline void
reallocate_aligned(T*& ptr, size_t new_size, size_t alignment, size_t old_size = 0)
{
void* p = ptr;
reallocate_aligned(p, new_size, alignment, old_size);
ptr = static_cast<T*>(p);
}
// untyped reallocate buffer to new_size bytes with suggested alignment
template <>
inline void
reallocate_aligned(void*& ptr, size_t new_size, size_t alignment, size_t old_size)
{
if (old_size) {
// reallocate while preserving contents
void* dst = zfp::allocate_aligned(new_size, alignment);
std::memcpy(dst, ptr, std::min(old_size, new_size));
zfp::deallocate_aligned(ptr);
ptr = dst;
}
else {
// reallocate without preserving contents
zfp::deallocate_aligned(ptr);
ptr = zfp::allocate_aligned(new_size, alignment);
}
}
// clone array 'T src[count]' to dst
template <typename T>
inline void
clone(T*& dst, const T* src, size_t count)
{
zfp::deallocate(dst);
if (src) {
dst = static_cast<T*>(zfp::allocate(count * sizeof(T)));
std::copy(src, src + count, dst);
}
else
dst = 0;
}
// clone array 'T src[count]' to dst with suggested alignment
template <typename T>
inline void
clone_aligned(T*& dst, const T* src, size_t count, size_t alignment)
{
void* d = dst;
const void* s = src;
clone_aligned(d, s, count * sizeof(T), alignment);
dst = static_cast<T*>(d);
src = static_cast<const T*>(s);
}
// untyped, aligned clone of size bytes
template <>
inline void
clone_aligned(void*& dst, const void* src, size_t size, size_t alignment)
{
zfp::deallocate_aligned(dst);
if (src) {
dst = zfp::allocate_aligned(size, alignment);
std::memcpy(dst, src, size);
}
else
dst = 0;
}
// return smallest multiple of unit greater than or equal to size
inline size_t
round_up(size_t size, size_t unit)
{
size += unit - 1;
size -= size % unit;
return size;
}
}
#undef unused_
#endif