Geogram Version 1.10.2
A programming library of geometric algorithms
Loading...
Searching...
No Matches
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 <new>
#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

namespace  GEO
 Global Vorpaline namespace.
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.
#define geo_dim_alignment(dim)
 Gets a point alignment.
#define geo_assume_aligned(var, alignment)
 Informs the compiler that a given pointer is memory-aligned.
#define geo_aligned_alloca(size)
 Allocates aligned memory on the stack.

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 const byte * GEO::Memory::const_pointer
 Const 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.
void GEO::Memory::copy (void *to, const void *from, size_t size)
 Copies a memory block.
template<class FPTR = function_pointer>
pointer GEO::Memory::function_pointer_to_generic_pointer (FPTR fptr)
 Converts a function pointer to a generic pointer.
template<class FPTR = function_pointer>
FPTR GEO::Memory::generic_pointer_to_function_pointer (pointer ptr)
 Converts a generic pointer to a function pointer.
template<class FPTR = function_pointer>
FPTR GEO::Memory::generic_pointer_to_function_pointer (void *ptr)
 Converts a generic pointer to a function pointer.
template<class T>
T & GEO::Memory::pointer_as_reference (void *ptr)
 Converts a pointer to a reference.
template<class T>
const T & GEO::Memory::pointer_as_reference (const void *ptr)
 Converts a const pointer to a reference.
void * GEO::Memory::aligned_malloc (size_t size, size_t alignment=GEO_MEMORY_ALIGNMENT)
 Allocates aligned memory.
void GEO::Memory::aligned_free (void *p)
 Deallocates aligned memory.
bool GEO::Memory::is_aligned (void *p, size_t alignment=GEO_MEMORY_ALIGNMENT)
 Checks whether a pointer is aligned.
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.
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.

Detailed Description

Types and functions for memory manipulation.

Definition in file memory.h.

Macro Definition Documentation

◆ geo_aligned_alloca

#define geo_aligned_alloca ( size)
Value:
#define GEO_MEMORY_ALIGNMENT
Default memory alignment for efficient vector operations.
Definition memory.h:240
void * align(void *p)
Returns the smallest aligned memory address from p.
Definition memory.h:473

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 490 of file memory.h.

◆ geo_assume_aligned

#define geo_assume_aligned ( var,
alignment )
Value:
*(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:419
Note
Memory alignment is not supported under Android.
C++20 has std::assume_aligned()

Definition at line 419 of file memory.h.

◆ geo_dim_alignment

#define geo_dim_alignment ( dim)
Value:
static const size_t value
Alignment value in bytes.
Definition memory.h:258

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 314 of file memory.h.

◆ GEO_HAS_BIG_STACK

#define GEO_HAS_BIG_STACK

Definition at line 78 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 240 of file memory.h.