Browse Source

Added function to check if pointer is aligned.

bkaradzic 12 years ago
parent
commit
5fe2150bd3
1 changed files with 20 additions and 11 deletions
  1. 20 11
      include/bx/allocator.h

+ 20 - 11
include/bx/allocator.h

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