Procházet zdrojové kódy

Rename align to align_top and use a better algorithm to align addresses

Daniele Bartolini před 12 roky
rodič
revize
a6b44dfa44

+ 1 - 1
src/core/mem/LinearAllocator.cpp

@@ -54,7 +54,7 @@ void* LinearAllocator::allocate(size_t size, size_t align)
 		return NULL;
 		return NULL;
 	}
 	}
 
 
-	void* user_ptr = memory::align((char*) m_physical_start + m_offset, align);
+	void* user_ptr = memory::align_top((char*) m_physical_start + m_offset, align);
 
 
 	m_offset += actual_size;
 	m_offset += actual_size;
 
 

+ 2 - 2
src/core/mem/MallocAllocator.cpp

@@ -53,7 +53,7 @@ void* MallocAllocator::allocate(size_t size, size_t align)
 	Header* h = (Header*)dlmalloc(actual_size);
 	Header* h = (Header*)dlmalloc(actual_size);
 	h->size = actual_size;
 	h->size = actual_size;
 
 
-	void* data = memory::align(h + 1, align);
+	void* data = memory::align_top(h + 1, align);
 
 
 	pad(h, data);
 	pad(h, data);
 
 
@@ -111,7 +111,7 @@ MallocAllocator::Header* MallocAllocator::header(void* data)
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void* MallocAllocator::data(Header* header, size_t align)
 void* MallocAllocator::data(Header* header, size_t align)
 {
 {
-	return memory::align(header + 1, align);
+	return memory::align_top(header + 1, align);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------

+ 3 - 2
src/core/mem/Memory.h

@@ -38,13 +38,14 @@ const uint32_t	PADDING_VALUE	= 0xFFFFFFFFu;	//!< Value used to fill unused memor
 const size_t	DEFAULT_ALIGN	= 4;			//!< Default memory alignment in bytes
 const size_t	DEFAULT_ALIGN	= 4;			//!< Default memory alignment in bytes
 
 
 /// Returns the pointer @a p aligned to the desired @a align byte
 /// Returns the pointer @a p aligned to the desired @a align byte
-inline void* align(void* p, size_t align)
+inline void* align_top(void* p, size_t align)
 {
 {
+	CE_ASSERT(align > 1, "Alignment must be > 1");
 	CE_ASSERT(align % 2 == 0, "Alignment must be a power of two");
 	CE_ASSERT(align % 2 == 0, "Alignment must be a power of two");
 
 
 	uintptr_t ptr = (uintptr_t)p;
 	uintptr_t ptr = (uintptr_t)p;
 
 
-	return (void*)(ptr + (align - ptr % align));
+	return (void*)(ptr + (ptr & (align - 1)));
 }
 }
 
 
 } // namespace memory
 } // namespace memory