How to Align Pointer

How to free aligned pointer?

If the pointer (and possibly the alignment size) is your only information, you'd need to store some kind of metadata (the original pointer, alignment offset etc.) somewhere. It might be before your allocated memory block, which you can then access when performing the deallocation, or some registry (e.g. hashtable or map) somewhere else, where you will store the metadata and retrieve them according to the address when de-allocating.

One possible (oversimplified) implementation (not tested, just for illustration - in the real case, the stored value will also need to be aligned properly, if align is smaller than the suitable alignment for uintptr_t):

void* allocate_align_1(size_t size, uintptr_t align)
void* addr = malloc(size + align + sizeof(uintptr_t));

uintptr_t aligned_addr = (uintptr_t)addr + sizeof(uintptr_t);
aligned_addr += align - (aligned_addr % align);

// store the original address
*(uintptr_t*)(aligned_addr - sizeof(uintptr_t)) = (uintptr_t)addr;

return (void*)aligned_addr;

Then in deallocate, you can access the data (for proper use there could/should be some check of a guard value or similar to make sure the correct data is really there), an example without checking:

void deallocate(void * ptr)
uintptr_t orig_ptr = *(uintptr_t*)((uintptr_t)ptr - sizeof(uintptr_t));

Correct way for pointer alignment

A pointer is aligned if your quotient is 0 after performing modulo operation pointer mod alignment.

This means that you can check (in C++11) using

#include <cstdint>
bool isAligned = ((reinterpret_cast<std::uintptr_t>(pointer) % alignment) == 0);

You can also do it using bitwise operation (only if alignment is a power of 2)

bool isAligned = ((reinterpret_cast<std::uintptr_t>(pointer) & (alignment - 1)) == 0);

However, the compiler will probably optimize that for you.

Related Topics

Leave a reply