T-SIMD v31.1.0
A C++ template SIMD library
Loading...
Searching...
No Matches
alloc.H
1// ===========================================================================
2//
3// allocation code
4//
5// This source code file is part of the following software:
6//
7// - the low-level C++ template SIMD library
8// - the SIMD implementation of the MinWarping and the 2D-Warping methods
9// for local visual homing.
10//
11// The software is provided based on the accompanying license agreement in the
12// file LICENSE.md.
13// The software is provided "as is" without any warranty by the licensor and
14// without any liability of the licensor, and the software may not be
15// distributed by the licensee; see the license agreement for details.
16//
17// (C) Ralf Möller
18// Computer Engineering
19// Faculty of Technology
20// Bielefeld University
21// www.ti.uni-bielefeld.de
22//
23// ===========================================================================
24
25// 30. Aug 22 (Jonas Keller): added simd_aligned_malloc and simd_aligned_free
26
27// 02. Mar 23 (Jonas Keller): added doxygen documentation
28
29#pragma once
30#ifndef SIMDALLOC_H_
31#define SIMDALLOC_H_
32
33#include "defs.H"
34
35#include <cstddef>
36#include <utility>
37
38#ifdef _WIN32
39#include <malloc.h>
40#else
41#include <cstdlib>
42#endif
43
44namespace simd {
45
61inline void *aligned_malloc(size_t alignment, size_t size)
62{
63#ifdef _WIN32
64 return _aligned_malloc(size, alignment);
65#else
66 void *ptr = nullptr;
67 if (posix_memalign(&ptr, alignment, size) != 0) { return nullptr; }
68 return ptr;
69#endif
70}
71
72#if defined(NATIVE_SIMD_WIDTH) || defined(DOXYGEN)
87inline void *aligned_malloc(size_t size)
88{
89 return aligned_malloc(NATIVE_SIMD_WIDTH, size);
90}
91#endif
92
102inline void aligned_free(void *ptr)
103{
104#ifdef _WIN32
105 _aligned_free(ptr);
106#else
107 free(ptr);
108#endif
109}
110
111// 05. Sep 23 (Jonas Keller): added simd_aligned_allocator
112
126#ifdef NATIVE_SIMD_WIDTH
127template <typename T, size_t ALIGN = NATIVE_SIMD_WIDTH>
128#else
129template <typename T, size_t ALIGN>
130#endif
132{
133 // exclude from doxygen (until endcond)
135public:
136 using value_type = T;
137 using pointer = T *;
138 using const_pointer = const T *;
139 using reference = T &;
140 using const_reference = const T &;
141 using size_type = std::size_t;
142 using difference_type = std::ptrdiff_t;
143
144 template <typename U>
145 struct rebind
146 {
147 using other = aligned_allocator<U, ALIGN>;
148 };
149
150 aligned_allocator() noexcept {}
151 aligned_allocator(const aligned_allocator &) noexcept {}
152 template <typename U>
154 {}
155 ~aligned_allocator() noexcept {}
156
157 pointer address(reference x) const noexcept { return std::addressof(x); }
158 const_pointer address(const_reference x) const noexcept
159 {
160 return std::addressof(x);
161 }
162
163 pointer allocate(size_type n, const void * = 0)
164 {
165 return static_cast<pointer>(aligned_malloc(ALIGN, n * sizeof(T)));
166 }
167 void deallocate(pointer p, size_type) { aligned_free(p); }
168
169 size_type max_size() const noexcept
170 {
171 return (size_type(-1) - size_type(ALIGN)) / sizeof(T);
172 }
173 template <typename U, typename... Args>
174 void construct(U *p, Args &&...args)
175 {
176 ::new (static_cast<void *>(p)) U(std::forward<Args>(args)...);
177 }
178 void destroy(pointer p) { p->~T(); }
179
180 bool operator==(const aligned_allocator &) const { return true; }
181 bool operator!=(const aligned_allocator &) const { return false; }
183};
184
185} // namespace simd
186
187#endif // SIMDALLOC_H_
Aligned allocator.
Definition alloc.H:132
void * aligned_malloc(size_t alignment, size_t size)
Aligned memory allocation.
Definition alloc.H:61
void aligned_free(void *ptr)
Aligned memory deallocation.
Definition alloc.H:102
static Vec< T, SIMD_WIDTH > operator==(const Vec< T, SIMD_WIDTH > &a, const Vec< T, SIMD_WIDTH > &b)
Equal to operator. Maps to cmpeq().
Definition ext.H:3778
static Vec< T, SIMD_WIDTH > operator!=(const Vec< T, SIMD_WIDTH > &a, const Vec< T, SIMD_WIDTH > &b)
Not equal to operator. Maps to cmpneq().
Definition ext.H:3780
Namespace for T-SIMD.
Definition time_measurement.H:161