40#ifndef GEOGRAM_BASIC_MEMORY
41#define GEOGRAM_BASIC_MEMORY
80 typedef unsigned char byte;
104 inline void clear(
void* addr,
size_t size) {
105 ::memset(addr, 0, size);
117 inline void copy(
void* to,
const void* from,
size_t size) {
118 ::memcpy(to, from, size);
136 ::memcpy(&result, &fptr,
sizeof(
pointer));
155 ::memcpy(&result, &ptr,
sizeof(
pointer));
172 ::memcpy(&result, &ptr,
sizeof(
pointer));
184#define GEO_MEMORY_ALIGNMENT 64
211 static const size_t value = 16;
220 static const size_t value = 8;
229 static const size_t value = 32;
238 static const size_t value = 16;
247 static const size_t value = 64;
258#define geo_dim_alignment(dim) GEO::Memory::PointAlignment<dim>::value
272#if defined(GEO_OS_ANDROID)
276#elif defined(GEO_COMPILER_INTEL)
277 return _mm_malloc(size, alignment);
278#elif defined(GEO_COMPILER_GCC) || defined(GEO_COMPILER_CLANG)
280 return posix_memalign(&result, alignment, size) == 0
282#elif defined(GEO_COMPILER_MSVC)
283 return _aligned_malloc(size, alignment);
298#if defined(GEO_OS_ANDROID)
301#elif defined(GEO_COMPILER_INTEL)
303#elif defined(GEO_COMPILER_GCC_FAMILY)
305#elif defined(GEO_COMPILER_MSVC)
325#if defined(GEO_OS_ANDROID)
326#define geo_decl_aligned(var) var
327#elif defined(GEO_COMPILER_INTEL)
328#define geo_decl_aligned(var) __declspec(aligned(GEO_MEMORY_ALIGNMENT)) var
329#elif defined(GEO_COMPILER_GCC_FAMILY)
330#define geo_decl_aligned(var) var __attribute__((aligned(GEO_MEMORY_ALIGNMENT)))
331#elif defined(GEO_COMPILER_MSVC)
332#define geo_decl_aligned(var) __declspec(align(GEO_MEMORY_ALIGNMENT)) var
333#elif defined(GEO_COMPILER_EMSCRIPTEN)
334#define geo_decl_aligned(var) var
352#if defined(GEO_OS_ANDROID)
353#define geo_assume_aligned(var, alignment)
354#elif defined(GEO_COMPILER_INTEL)
355#define geo_assume_aligned(var, alignment) \
356 __assume_aligned(var, alignment)
357#elif defined(GEO_COMPILER_CLANG)
358#define geo_assume_aligned(var, alignment)
360#elif defined(GEO_COMPILER_GCC)
361#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
362#define geo_assume_aligned(var, alignment) \
363 *(void**) (&var) = __builtin_assume_aligned(var, alignment)
369#define geo_assume_aligned(var, alignment)
371#elif defined(GEO_COMPILER_MSVC)
372#define geo_assume_aligned(var, alignment)
374#elif defined(GEO_COMPILER_EMSCRIPTEN)
375#define geo_assume_aligned(var, alignment)
376#elif defined(GEO_COMPILER_MINGW)
377#define geo_assume_aligned(var, alignment)
390#if defined(GEO_COMPILER_INTEL)
391#define geo_restrict __restrict
392#elif defined(GEO_COMPILER_GCC_FAMILY)
393#define geo_restrict __restrict__
394#elif defined(GEO_COMPILER_MSVC)
395#define geo_restrict __restrict
396#elif defined(GEO_COMPILER_EMSCRIPTEN)
410 return (
reinterpret_cast<size_t>(p) & (alignment - 1)) == 0;
421 return reinterpret_cast<char*
>(p) + offset;
433#define geo_aligned_alloca(size) \
434 GEO::Memory::align(alloca(size + GEO_MEMORY_ALIGNMENT - 1))
443 template <
class T,
int ALIGN = GEO_MEMORY_ALIGNMENT>
513 size_type nb_elt,
const void* hint =
nullptr
545 ::std::allocator<char> a;
546 return std::allocator_traits<
decltype(a)>
::max_size(a) /
sizeof(T);
575#ifdef GEO_COMPILER_MSVC
593 template <
typename T1,
int A1,
typename T2,
int A2>
604 template <
typename T1,
int A1,
typename T2,
int A2>
623 class vector :
public ::std::vector<T, Memory::aligned_allocator<T> > {
627 typedef ::std::vector<T, Memory::aligned_allocator<T> > baseclass;
655 baseclass(
size, val) {
667 return index_t(baseclass::size());
677 return baseclass::operator[] (i);
688 return baseclass::operator[] (i);
698 return baseclass::operator[] (
index_t(i));
709 return baseclass::operator[] (
index_t(i));
722 return baseclass::operator[] (
index_t(i));
733 return baseclass::operator[] (
index_t(i));
743 return baseclass::operator[] (
index_t(i));
754 return baseclass::operator[] (
index_t(i));
763 return size() == 0 ? nullptr : &(*this)[0];
771 return size() == 0 ? nullptr : &(*this)[0];
783 class vector<bool> :
public ::std::vector<bool> {
784 typedef ::std::vector<bool> baseclass;
799 baseclass(
size, val) {
808 return index_t(baseclass::size());
A function to suppress unused parameters compilation warnings.
Assertion checking mechanism.
#define geo_debug_assert(x)
Verifies that a condition is met.
Functions for atomic operations.
Common include file, providing basic definitions. Should be included before anything else by all head...
An allocator that performs aligned memory allocations.
size_type max_size() const
Gets the maximum size possible to allocate.
T * pointer
Pointer to element.
const T & const_reference
Reference to constant element.
void destroy(pointer p)
Destroys an object.
const T * const_pointer
Pointer to constant element.
pointer address(reference x)
Gets the address of an object.
pointer allocate(size_type nb_elt, const void *hint=nullptr)
Allocates a block of storage.
T value_type
Element type.
const_pointer address(const_reference x)
Gets the address of a object.
T & reference
Reference to element.
void construct(pointer p, const_reference val)
Constructs an object.
::std::size_t size_type
Quantities of elements.
void deallocate(pointer p, size_type nb_elt)
Releases a block of storage.
::std::ptrdiff_t difference_type
Difference between two pointers.
index_t size() const
Gets the number of elements.
vector(index_t size)
Creates a pre-allocated vector.
vector(index_t size, bool val)
Creates a pre-initialized vector.
vector()
Creates an empty vector.
Vector with aligned memory allocation.
vector()
Creates an empty vector.
const T * data() const
Gets a pointer to the array of elements.
vector(index_t size, const T &val)
Creates a pre-initialized vector.
vector(index_t size)
Creates a pre-allocated vector.
T * data()
Gets a pointer to the array of elements.
T & operator[](index_t i)
Gets a vector element.
index_t size() const
Gets the number of elements.
#define GEO_MEMORY_ALIGNMENT
Default memory alignment for efficient vector operations.
void * aligned_malloc(size_t size, size_t alignment=GEO_MEMORY_ALIGNMENT)
Allocates aligned memory.
unsigned short word16
Unsigned 16 bits integer.
unsigned char byte
Unsigned byte type.
void(* function_pointer)()
Generic function pointer.
void * align(void *p)
Returns the smallest aligned memory address from p.
pointer function_pointer_to_generic_pointer(function_pointer fptr)
Converts a function pointer to a generic pointer.
void aligned_free(void *p)
Deallocates aligned memory.
unsigned int word32
Unsigned 32 bits integer.
byte * pointer
Pointer to unsigned byte(s)
unsigned char word8
Unsigned 8 bits integer.
void clear(void *addr, size_t size)
Clears a memory block.
bool is_aligned(void *p, size_t alignment=GEO_MEMORY_ALIGNMENT)
Checks whether a pointer is aligned.
bool operator!=(const aligned_allocator< T1, A1 > &, const aligned_allocator< T2, A2 > &)
Tests whether two aligned_allocators are different.
function_pointer generic_pointer_to_function_pointer(pointer ptr)
Converts a generic pointer to a function pointer.
bool operator==(const aligned_allocator< T1, A1 > &, const aligned_allocator< T2, A2 > &)
Tests whether two aligned_allocators are equal.
void copy(void *to, const void *from, size_t size)
Copies a memory block.
Global Vorpaline namespace.
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
geo_signed_index_t signed_index_t
The type for storing and manipulating indices differences.
geo_index_t index_t
The type for storing and manipulating indices.
Types and functions for numbers manipulation.
Functions for string manipulation.
Defines the memory alignment of points in a vector.
static const size_t value
Alignment value in bytes.
Defines the same allocator for other types.
aligned_allocator< U > other