diff --git a/include/bx/allocator.h b/include/bx/allocator.h index 03b4994..a1ab3e3 100644 --- a/include/bx/allocator.h +++ b/include/bx/allocator.h @@ -55,6 +55,9 @@ namespace bx virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0; }; + /// Check if pointer is aligned. _align must be power of two. + bool isAligned(const void* _ptr, size_t _align); + /// Aligns pointer to nearest next aligned address. _align must be power of two. void* alignPtr( void* _ptr diff --git a/include/bx/allocator.inl b/include/bx/allocator.inl index 3bef88f..67a4a53 100644 --- a/include/bx/allocator.inl +++ b/include/bx/allocator.inl @@ -13,13 +13,20 @@ namespace bx { } + inline bool isAligned(const void* _ptr, size_t _align) + { + union { const void* ptr; uintptr_t addr; } un; + un.ptr = _ptr; + return 0 == (un.addr & (_align-1) ); + } + inline void* alignPtr(void* _ptr, size_t _extra, size_t _align) { - union { void* ptr; size_t addr; } un; + union { void* ptr; uintptr_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); + uintptr_t unaligned = un.addr + _extra; // space for header + uintptr_t mask = _align-1; + uintptr_t aligned = BX_ALIGN_MASK(unaligned, mask); un.addr = aligned; return un.ptr; } diff --git a/include/bx/bx.h b/include/bx/bx.h index be16b42..6c1f7b4 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -46,14 +46,6 @@ namespace bx Ty tmp = _a; _a = _b; _b = tmp; } - /// Check if pointer is aligned. _align must be power of two. - inline bool isPtrAligned(const void* _ptr, size_t _align) - { - union { const void* ptr; size_t addr; } un; - un.ptr = _ptr; - return 0 == (un.addr & (_align-1) ); - } - /// Scatter/gather memcpy. inline void memCopy(void* _dst, const void* _src, uint32_t _size, uint32_t _num, uint32_t _srcPitch, uint32_t _dstPitch) { diff --git a/include/bx/hash.h b/include/bx/hash.h index d037c30..3ef223e 100644 --- a/include/bx/hash.h +++ b/include/bx/hash.h @@ -6,7 +6,7 @@ #ifndef BX_HASH_H_HEADER_GUARD #define BX_HASH_H_HEADER_GUARD -#include "bx.h" +#include "allocator.h" // isAligned namespace bx { @@ -30,7 +30,7 @@ namespace bx void add(const void* _data, int _len) { - if (BX_UNLIKELY(!isPtrAligned(_data, 4) ) ) + if (BX_UNLIKELY(!isAligned(_data, 4) ) ) { addUnaligned(_data, _len); return;