mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-18 04:53:06 +01:00
Added function to check if pointer is aligned.
This commit is contained in:
@@ -34,6 +34,26 @@
|
||||
|
||||
namespace bx
|
||||
{
|
||||
/// Aligns pointer to nearest next aligned address. _align
|
||||
inline void* alignPtr(void* _ptr, size_t _extra, size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT)
|
||||
{
|
||||
union { void* ptr; size_t addr; } un;
|
||||
un.ptr = _ptr;
|
||||
size_t unaligned = un.addr + _extra; // space for header
|
||||
size_t mask = _align-1;
|
||||
size_t aligned = BX_ALIGN_MASK(unaligned, mask);
|
||||
un.addr = aligned;
|
||||
return un.ptr;
|
||||
}
|
||||
|
||||
/// Check if pointer is aligned. _align must be power of two value.
|
||||
inline bool isPtrAligned(const void* _ptr, size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT)
|
||||
{
|
||||
union { const void* ptr; size_t addr; } un;
|
||||
un.ptr = _ptr;
|
||||
return 0 == (un.addr & (_align-1) );
|
||||
}
|
||||
|
||||
struct BX_NO_VTABLE AllocatorI
|
||||
{
|
||||
virtual ~AllocatorI() = 0;
|
||||
@@ -91,17 +111,6 @@ namespace bx
|
||||
return _allocator->alignedRealloc(_ptr, _size, _align, _file, _line);
|
||||
}
|
||||
|
||||
inline void* alignPtr(void* _ptr, size_t _extra, size_t _align = BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT)
|
||||
{
|
||||
union { void* ptr; size_t addr; } un;
|
||||
un.ptr = _ptr;
|
||||
size_t unaligned = un.addr + _extra; // space for header
|
||||
size_t mask = _align-1;
|
||||
size_t aligned = BX_ALIGN_MASK(unaligned, mask);
|
||||
un.addr = aligned;
|
||||
return un.ptr;
|
||||
}
|
||||
|
||||
static inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file = NULL, uint32_t _line = 0)
|
||||
{
|
||||
size_t total = _size + _align;
|
||||
|
||||
Reference in New Issue
Block a user