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));
free((void*)orig_ptr);
}
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
Random Number Generator That Produces a Power-Law Distribution
Understanding the Dangers of Sprintf(...)
Static Analysis Tool to Detect Abi Breaks in C++
Why Is Copy Constructor Called Instead of Conversion Constructor
How to Atomically Update a Maximum Value
Why Is My Object Not Being Copied When Move Constructor Is Deleted
Pointer Interconvertibility VS Having the Same Address
Best Way for Interprocess Communication in C++
Portable Unused Parameter MACro Used on Function Signature for C and C++
Stl Rope - When and Where to Use
One or More Multiply Defined Symbols Found
Visual Studio: Run C++ Project Post-Build Event Even If Project Is Up-To-Date
With a Private Modifier, Why Can the Member in Other Objects Be Accessed Directly
How to Target Windows Xp in Microsoft Visual Studio C++