data-structures-c
Loading...
Searching...
No Matches
align.h File Reference

Align memory. More...

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include "is_pow2.h"

Go to the source code of this file.

Macros

#define CALC_ALIGNMENT_PADDING(alignment, ptr)
 compile time calc_alignment_padding.
 

Functions

static void * align (const size_t alignment, const size_t size, void **ptr_ptr, size_t *space_ptr)
 Align pointer to the next alignment boundary.
 
static uintptr_t calc_alignment_padding (const size_t alignment, const uintptr_t ptr)
 Calculate the alignment padding required to align a pointer.
 

Detailed Description

Align memory.

This is a direct translation of the std::align implementation in libstdc++.

See:

Macro Definition Documentation

◆ CALC_ALIGNMENT_PADDING

#define CALC_ALIGNMENT_PADDING ( alignment,
ptr )
Value:
((((ptr) - 1u + (alignment)) & -(alignment)) - (ptr))

compile time calc_alignment_padding.

Function Documentation

◆ align()

static void * align ( const size_t alignment,
const size_t size,
void ** ptr_ptr,
size_t * space_ptr )
inlinestatic

Align pointer to the next alignment boundary.

Parameters
[in]alignmentThe desired alignment.
[in]sizeThe size of the storage to be aligned.
[in,out]ptr_ptrPointer to contiguous storage (a buffer) of at least space bytes. Given it's pointer.
[in,out]space_ptrThe size of the buffer in which to operate. Given it's pointer.
Returns
The adjusted value of *ptr_ptr.
Return values
NULLIf the space provided is too small.

◆ calc_alignment_padding()

static uintptr_t calc_alignment_padding ( const size_t alignment,
const uintptr_t ptr )
inlinestatic

Calculate the alignment padding required to align a pointer.

Parameters
[in]alignmentThe desired alignment.
[in]ptrPointer to a buffer as uintptr_t.
Returns
The padding to be added to ptr to align ptr.