Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
memory.h File Reference

Types and functions for memory manipulation. More...

#include <geogram/basic/common.h>
#include <geogram/basic/assert.h>
#include <geogram/basic/numeric.h>
#include <geogram/basic/argused.h>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  GEO::Memory::PointAlignment< DIM >
 Defines the memory alignment of points in a vector. More...
 
struct  GEO::Memory::PointAlignment< 2 >
 PointAlignment specialization for points of dimension 2. More...
 
struct  GEO::Memory::PointAlignment< 3 >
 PointAlignment specialization for points of dimension 3. More...
 
struct  GEO::Memory::PointAlignment< 4 >
 PointAlignment specialization for points of dimension 4. More...
 
struct  GEO::Memory::PointAlignment< 6 >
 PointAlignment specialization for points of dimension 6. More...
 
struct  GEO::Memory::PointAlignment< 8 >
 PointAlignment specialization for points of dimension 8. More...
 
class  GEO::Memory::aligned_allocator< T, ALIGN >
 An allocator that performs aligned memory allocations. More...
 
struct  GEO::Memory::aligned_allocator< T, ALIGN >::rebind< U >
 Defines the same allocator for other types. More...
 
class  GEO::vector< T >
 Vector with aligned memory allocation. More...
 
class  GEO::vector< bool >
 Specialization of vector for elements of type bool. More...
 

Namespaces

 GEO
 Global Vorpaline namespace.
 
 GEO::Memory
 Utilities for memory management.
 

Macros

#define GEO_HAS_BIG_STACK
 
#define GEO_MEMORY_ALIGNMENT   64
 Default memory alignment for efficient vector operations. More...
 
#define geo_dim_alignment(dim)   GEO::Memory::PointAlignment<dim>::value
 Gets a point alignment. More...
 
#define geo_assume_aligned(var, alignment)    *(void**) (&var) = __builtin_assume_aligned(var, alignment)
 Informs the compiler that a given pointer is memory-aligned. More...
 
#define geo_aligned_alloca(size)    GEO::Memory::align(alloca(size + GEO_MEMORY_ALIGNMENT - 1))
 Allocates aligned memory on the stack. More...
 

Typedefs

typedef unsigned char GEO::Memory::byte
 Unsigned byte type.
 
typedef unsigned char GEO::Memory::word8
 Unsigned 8 bits integer.
 
typedef unsigned short GEO::Memory::word16
 Unsigned 16 bits integer.
 
typedef unsigned int GEO::Memory::word32
 Unsigned 32 bits integer.
 
typedef byte * GEO::Memory::pointer
 Pointer to unsigned byte(s)
 
typedef void(* GEO::Memory::function_pointer) ()
 Generic function pointer.
 

Functions

void GEO::Memory::clear (void *addr, size_t size)
 Clears a memory block. More...
 
void GEO::Memory::copy (void *to, const void *from, size_t size)
 Copies a memory block. More...
 
pointer GEO::Memory::function_pointer_to_generic_pointer (function_pointer fptr)
 Converts a function pointer to a generic pointer. More...
 
function_pointer GEO::Memory::generic_pointer_to_function_pointer (pointer ptr)
 Converts a generic pointer to a function pointer. More...
 
function_pointer GEO::Memory::generic_pointer_to_function_pointer (void *ptr)
 Converts a generic pointer to a function pointer. More...
 
void * GEO::Memory::aligned_malloc (size_t size, size_t alignment=GEO_MEMORY_ALIGNMENT)
 Allocates aligned memory. More...
 
void GEO::Memory::aligned_free (void *p)
 Deallocates aligned memory. More...
 
bool GEO::Memory::is_aligned (void *p, size_t alignment=GEO_MEMORY_ALIGNMENT)
 Checks whether a pointer is aligned. More...
 
void * GEO::Memory::align (void *p)
 Returns the smallest aligned memory address from p.
 
template<typename T1 , int A1, typename T2 , int A2>
bool GEO::Memory::operator== (const aligned_allocator< T1, A1 > &, const aligned_allocator< T2, A2 > &)
 Tests whether two aligned_allocators are equal. More...
 
template<typename T1 , int A1, typename T2 , int A2>
bool GEO::Memory::operator!= (const aligned_allocator< T1, A1 > &, const aligned_allocator< T2, A2 > &)
 Tests whether two aligned_allocators are different. More...
 

Detailed Description

Types and functions for memory manipulation.

Definition in file memory.h.

Macro Definition Documentation

◆ geo_aligned_alloca

#define geo_aligned_alloca (   size)     GEO::Memory::align(alloca(size + GEO_MEMORY_ALIGNMENT - 1))

Allocates aligned memory on the stack.

Allocates size bytes on the stack. The returned address is guaranteed to be aligned on GEO_MEMORY_ALIGNMENT bytes. To guarantee the memory alignment, the function may allocate more than size, but not more than GEO_MEMORY_ALIGNMENT - 1.

Parameters
[in]sizeNumber of bytes to allocate.
Returns
An aligned pointer to a memory block of size bytes.

Definition at line 445 of file memory.h.

◆ geo_assume_aligned

#define geo_assume_aligned (   var,
  alignment 
)     *(void**) (&var) = __builtin_assume_aligned(var, alignment)

Informs the compiler that a given pointer is memory-aligned.

It helps the compiler vectorizing loops, i.e. generating SSE/AVX/... code.

Parameters
[in]vara pointer variable in the current scope
[in]alignmentthe memory alignment (must be a power of 2)
Example
double* p = ...;
geo_assume_aligned(p,alignment);
#define geo_assume_aligned(var, alignment)
Informs the compiler that a given pointer is memory-aligned.
Definition: memory.h:374
Note
Memory alignment is not supported under Android.

Definition at line 374 of file memory.h.

◆ geo_dim_alignment

#define geo_dim_alignment (   dim)    GEO::Memory::PointAlignment<dim>::value

Gets a point alignment.

This gives the alignment of a point of dimension dim within an array of points aligned on GEO_MEMORY_ALIGNMENT bytes

Parameters
[in]dimthe dimension of the point
See also
GEO::Memory::PointAlignment
GEO_MEMORY_ALIGNMENT

Definition at line 270 of file memory.h.

◆ GEO_MEMORY_ALIGNMENT

#define GEO_MEMORY_ALIGNMENT   64

Default memory alignment for efficient vector operations.

The memory alignment is given in bytes. Here is a list of commonly used alignment values for various architectures:

  • SSE: 16
  • AVX: 32
  • AVX-512: 64

Definition at line 196 of file memory.h.