PLaSK library
Loading...
Searching...
No Matches
memalloc.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__MEMALLOC_H
15#define PLASK__MEMALLOC_H
16
17#include <cstdlib>
18#include <cstring>
19#include <memory>
20
21#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
22#include <malloc.h>
23#endif
24
25#include <utility>
26#include <limits>
27#include <new>
28
29namespace plask {
30
31#if (defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) && defined(__LP64__)) || \
32 defined(__APPLE__) || defined(_WIN64) || (defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__))
33# define PLASK_MALLOC_ALIGNED 1
34#else
35# define PLASK_MALLOC_ALIGNED 0
36#endif
37
38
39#if !PLASK_MALLOC_ALIGNED && !defined(_MSC_VER)
40namespace detail {
41
45 inline void* custom_aligned_malloc(std::size_t size)
46 {
47 if (size > (std::numeric_limits<std::size_t>::max)() - 16) return nullptr;
48 void *original = std::malloc(size+16);
49 if (original == 0) return 0;
50 void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
51 *(reinterpret_cast<void**>(aligned) - 1) = original;
52 return aligned;
53 }
54
58 inline void custom_aligned_free(void *ptr)
59 {
60 if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
61 }
62
66 inline void* custom_aligned_realloc(void* ptr, std::size_t size, std::size_t old_size=0)
67 {
68 if (size > (std::numeric_limits<std::size_t>::max)() - 16) return nullptr;
69 if (ptr == 0) return custom_aligned_malloc(size);
70 void *original = *(reinterpret_cast<void**>(ptr) - 1);
71 const auto offset = static_cast<char*>(ptr) - static_cast<char*>(original);
72 original = std::realloc(original,size+16);
73 if (original == 0) return 0;
74 void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
75 char* payload = static_cast<char*>(original) + offset;
76 if (payload != aligned) {
77 // Without an old size, the resized block still has room for size bytes at either offset.
78 const std::size_t count = old_size && old_size < size ? old_size : size;
79 std::memmove(aligned, payload, count);
80 }
81 *(reinterpret_cast<void**>(aligned) - 1) = original;
82 return aligned;
83 }
84
85}
86#endif
87
93inline void* aligned_malloc(std::size_t size)
94{
95 void *result;
96#if PLASK_MALLOC_ALIGNED
97 result = std::malloc(size);
98#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
99 result = _aligned_malloc(size, 16);
100#else
102#endif
103 if(!result && size) throw std::bad_alloc();
104 return result;
105}
106
111inline void aligned_free(void *ptr)
112{
113 if (!ptr) return;
114#if PLASK_MALLOC_ALIGNED
115 std::free(ptr);
116#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
117 _aligned_free(ptr);
118#else
120#endif
121}
122
130inline void* aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size=0)
131{
132 (void) old_size; // don't warn about unused old_size
133 void *result;
134#if PLASK_MALLOC_ALIGNED
135 result = std::realloc(ptr,new_size);
136#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
138#else
140#endif
141 if (!result && new_size) throw std::bad_alloc();
142 return result;
143}
144
145namespace detail {
146 template <typename T>
147 constexpr std::size_t max_aligned_count() {
148 return (std::numeric_limits<std::size_t>::max)() / sizeof(T);
149 }
150}
151
158template <typename T>
159inline T* aligned_malloc(std::size_t num=1) {
160 if (num > detail::max_aligned_count<T>()) throw std::bad_alloc();
161 return static_cast<T*>(aligned_malloc(num * sizeof(T)));
162}
163
169template <typename T>
170inline void aligned_free(T* ptr) {
171 aligned_free(reinterpret_cast<void*>(const_cast<typename std::remove_const<T>::type*>(ptr)));
172}
173
177template <typename T>
182
183 void operator()(T* ptr) const {
184 aligned_free<T>(ptr);
185 }
186};
187
188template< class T > struct aligned_deleter<T[]> {
189 void operator()(T* ptr) const {
190 aligned_free<T>(ptr);
191 }
192};
193
194template <typename T>
195using aligned_unique_ptr = std::unique_ptr<T, aligned_deleter<T>>;
196
203template <typename T, typename... Args>
204inline T* aligned_new(Args&&... args) {
205 T* mem = reinterpret_cast<T*>(aligned_malloc(sizeof(T)));
207 new(mem) T(std::forward<Args>(args)...);
208 return storage.release();
209}
210
216template <typename T>
217inline void aligned_delete(T* ptr) {
218 ptr->~T();
219 aligned_free(ptr);
220}
221
228template <typename T, typename... Args>
229inline T* aligned_new_array(std::size_t num, Args&&... args) {
230 if (num > detail::max_aligned_count<T>()) throw std::bad_alloc();
231 T* mem = reinterpret_cast<T*>(aligned_malloc(num * sizeof(T)));
232
233 size_t constructed = 0;
234 try {
235 for (; constructed != num; ++constructed)
236 new(mem + constructed) T(std::forward<Args>(args)...);
237 } catch (...) {
238 while (constructed)
239 mem[--constructed].~T();
241 throw;
242 }
243 return mem;
244}
245
252template <typename T>
253inline void aligned_delete_array(std::size_t num, T* ptr) {
254 while (num)
255 ptr[--num].~T();
256 aligned_free(ptr);
257}
258
262template<class T>
264
265 typedef std::size_t size_type;
266 typedef std::ptrdiff_t difference_type;
267 typedef T* pointer;
268 typedef const T* const_pointer;
269 typedef T& reference;
270 typedef const T& const_reference;
271 typedef T value_type;
272
273 template<class U>
274 struct rebind { typedef aligned_allocator<U> other; };
275
276 pointer address(reference value) const { return &value; }
277
278 const_pointer address(const_reference value) const { return &value; }
279
281
283
284 template<class U>
286
288
289 size_type max_size() const { return detail::max_aligned_count<T>(); }
290
292
293 void construct(pointer p, const T& value) { new(p) T(value); }
294
295 template<typename... Args>
296 void construct(pointer p, Args&&... args) { new(p) T(std::forward<Args>(args)...); }
297
298 void destroy(pointer p) { p->~T(); }
299
301
302 bool operator!=(const aligned_allocator<T>&) const { return false; }
303
304 bool operator==(const aligned_allocator<T>&) const { return true; }
305};
306
307
308
309} // namespace plask
310
311
312#endif // PLASK__MEMALLOC_H