40 #ifndef GEOGRAM_BASIC_MEMORY
41 #define GEOGRAM_BASIC_MEMORY
77 #define GEO_HAS_BIG_STACK
92 typedef unsigned char byte;
116 inline void clear(
void* addr,
size_t size) {
117 ::memset(addr, 0, size);
129 inline void copy(
void* to,
const void* from,
size_t size) {
130 ::memcpy(to, from, size);
148 ::memcpy(&result, &fptr,
sizeof(
pointer));
167 ::memcpy(&result, &ptr,
sizeof(
pointer));
184 ::memcpy(&result, &ptr,
sizeof(
pointer));
196 #define GEO_MEMORY_ALIGNMENT 64
223 static const size_t value = 16;
232 static const size_t value = 8;
241 static const size_t value = 32;
250 static const size_t value = 16;
259 static const size_t value = 64;
270 #define geo_dim_alignment(dim) GEO::Memory::PointAlignment<dim>::value
284 #if defined(GEO_OS_ANDROID)
288 #elif defined(GEO_COMPILER_INTEL)
289 return _mm_malloc(size, alignment);
290 #elif defined(GEO_COMPILER_GCC) || defined(GEO_COMPILER_CLANG)
292 return posix_memalign(&result, alignment, size) == 0
294 #elif defined(GEO_COMPILER_MSVC)
295 return _aligned_malloc(size, alignment);
310 #if defined(GEO_OS_ANDROID)
313 #elif defined(GEO_COMPILER_INTEL)
315 #elif defined(GEO_COMPILER_GCC_FAMILY)
317 #elif defined(GEO_COMPILER_MSVC)
337 #if defined(GEO_OS_ANDROID)
338 #define geo_decl_aligned(var) var
339 #elif defined(GEO_COMPILER_INTEL)
340 #define geo_decl_aligned(var) __declspec(aligned(GEO_MEMORY_ALIGNMENT)) var
341 #elif defined(GEO_COMPILER_GCC_FAMILY)
342 #define geo_decl_aligned(var) var __attribute__((aligned(GEO_MEMORY_ALIGNMENT)))
343 #elif defined(GEO_COMPILER_MSVC)
344 #define geo_decl_aligned(var) __declspec(align(GEO_MEMORY_ALIGNMENT)) var
345 #elif defined(GEO_COMPILER_EMSCRIPTEN)
346 #define geo_decl_aligned(var) var
364 #if defined(GEO_OS_ANDROID)
365 #define geo_assume_aligned(var, alignment)
366 #elif defined(GEO_COMPILER_INTEL)
367 #define geo_assume_aligned(var, alignment) \
368 __assume_aligned(var, alignment)
369 #elif defined(GEO_COMPILER_CLANG)
370 #define geo_assume_aligned(var, alignment)
372 #elif defined(GEO_COMPILER_GCC)
373 #if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
374 #define geo_assume_aligned(var, alignment) \
375 *(void**) (&var) = __builtin_assume_aligned(var, alignment)
381 #define geo_assume_aligned(var, alignment)
383 #elif defined(GEO_COMPILER_MSVC)
384 #define geo_assume_aligned(var, alignment)
386 #elif defined(GEO_COMPILER_EMSCRIPTEN)
387 #define geo_assume_aligned(var, alignment)
388 #elif defined(GEO_COMPILER_MINGW)
389 #define geo_assume_aligned(var, alignment)
402 #if defined(GEO_COMPILER_INTEL)
403 #define geo_restrict __restrict
404 #elif defined(GEO_COMPILER_GCC_FAMILY)
405 #define geo_restrict __restrict__
406 #elif defined(GEO_COMPILER_MSVC)
407 #define geo_restrict __restrict
408 #elif defined(GEO_COMPILER_EMSCRIPTEN)
422 return (
reinterpret_cast<size_t>(p) & (alignment - 1)) == 0;
433 return reinterpret_cast<char*
>(p) + offset;
445 #define geo_aligned_alloca(size) \
446 GEO::Memory::align(alloca(size + GEO_MEMORY_ALIGNMENT - 1))
455 template <
class T,
int ALIGN = GEO_MEMORY_ALIGNMENT>
525 size_type nb_elt,
const void* hint =
nullptr
557 ::std::allocator<char> a;
558 return std::allocator_traits<decltype(a)>
::max_size(a) /
sizeof(T);
587 #ifdef GEO_COMPILER_MSVC
605 template <
typename T1,
int A1,
typename T2,
int A2>
616 template <
typename T1,
int A1,
typename T2,
int A2>
635 class vector :
public ::std::vector<T, Memory::aligned_allocator<T> > {
639 typedef ::std::vector<T, Memory::aligned_allocator<T> > baseclass;
667 baseclass(
size, val) {
679 return index_t(baseclass::size());
689 return baseclass::operator[] (i);
700 return baseclass::operator[] (i);
710 return baseclass::operator[] (
index_t(i));
721 return baseclass::operator[] (
index_t(i));
734 return baseclass::operator[] (
index_t(i));
745 return baseclass::operator[] (
index_t(i));
755 return baseclass::operator[] (
index_t(i));
766 return baseclass::operator[] (
index_t(i));
775 return size() == 0 ? nullptr : &(*this)[0];
783 return size() == 0 ? nullptr : &(*this)[0];
795 class vector<bool> :
public ::std::vector<bool> {
796 typedef ::std::vector<bool> baseclass;
811 baseclass(
size, val) {
820 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.
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.
const T * data() const
Gets a pointer to the array of elements.
vector()
Creates an empty vector.
vector(index_t size, const T &val)
Creates a pre-initialized vector.
T * data()
Gets a pointer to the array of elements.
vector(index_t size)
Creates a pre-allocated vector.
T & operator[](index_t i)
Gets a vector element.
index_t size() const
Gets the number of elements.
Common include file, providing basic definitions. Should be included before anything else by all head...
#define GEO_MEMORY_ALIGNMENT
Default memory alignment for efficient vector operations.
unsigned short word16
Unsigned 16 bits integer.
unsigned char byte
Unsigned byte type.
void(* function_pointer)()
Generic function pointer.
void * aligned_malloc(size_t size, size_t alignment=GEO_MEMORY_ALIGNMENT)
Allocates aligned memory.
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