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
365#if defined(GEO_OS_ANDROID)
366#define geo_assume_aligned(var, alignment)
367#elif defined(GEO_COMPILER_INTEL)
368#define geo_assume_aligned(var, alignment) \
369 __assume_aligned(var, alignment)
370#elif defined(GEO_COMPILER_CLANG)
371#define geo_assume_aligned(var, alignment)
373#elif defined(GEO_COMPILER_GCC)
374#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
375#define geo_assume_aligned(var, alignment) \
376 *(void**) (&var) = __builtin_assume_aligned(var, alignment)
382#define geo_assume_aligned(var, alignment)
384#elif defined(GEO_COMPILER_MSVC)
385#define geo_assume_aligned(var, alignment)
387#elif defined(GEO_COMPILER_EMSCRIPTEN)
388#define geo_assume_aligned(var, alignment)
389#elif defined(GEO_COMPILER_MINGW)
390#define geo_assume_aligned(var, alignment)
403#if defined(GEO_COMPILER_INTEL)
404#define geo_restrict __restrict
405#elif defined(GEO_COMPILER_GCC_FAMILY)
406#define geo_restrict __restrict__
407#elif defined(GEO_COMPILER_MSVC)
408#define geo_restrict __restrict
409#elif defined(GEO_COMPILER_EMSCRIPTEN)
423 return (
reinterpret_cast<size_t>(p) & (alignment - 1)) == 0;
434 return reinterpret_cast<char*
>(p) + offset;
446#define geo_aligned_alloca(size) \
447 GEO::Memory::align(alloca(size + GEO_MEMORY_ALIGNMENT - 1))
456 template <
class T,
int ALIGN = GEO_MEMORY_ALIGNMENT>
529 size_type nb_elt,
const void* hint =
nullptr
537 if(result !=
nullptr) {
549 std::new_handler handler = std::get_new_handler();
550 if(handler !=
nullptr) {
553 throw std::bad_alloc();
581 ::std::allocator<char> a;
582 return std::allocator_traits<
decltype(a)>
::max_size(a) /
612#ifdef GEO_COMPILER_MSVC
630 template <
typename T1,
int A1,
typename T2,
int A2>
641 template <
typename T1,
int A1,
typename T2,
int A2>
660 class vector :
public ::std::vector<T, Memory::aligned_allocator<T> > {
669 typedef ::std::vector<T, Memory::aligned_allocator<T> > baseclass;
699 baseclass(
size, val) {
711 return index_t(baseclass::size());
721 return baseclass::operator[] (i);
732 return baseclass::operator[] (i);
742 return baseclass::operator[] (
index_t(i));
753 return baseclass::operator[] (
index_t(i));
766 return baseclass::operator[] (
index_t(i));
777 return baseclass::operator[] (
index_t(i));
787 return baseclass::operator[] (
index_t(i));
798 return baseclass::operator[] (
index_t(i));
807 T* result = baseclass::data();
821 const T* result = baseclass::data();
849 class vector<bool> :
public ::std::vector<bool> {
850 typedef ::std::vector<bool> baseclass;
865 baseclass(
size, val) {
874 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.
static constexpr int ALIGNMENT
Alignment in bytes.
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.
void clear_and_deallocate()
Resizes this vector to zero and deallocated all the memory.
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.
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.
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