This commit is contained in:
Branimir Karadžić
2017-01-19 15:09:53 -08:00
parent c9ffb1e01d
commit e98a802f09
4 changed files with 16 additions and 14 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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)
{

View File

@@ -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;