Ver código fonte

core: ensure structure alignment

Daniele Bartolini 2 anos atrás
pai
commit
eb7b5ee257
1 arquivos alterados com 11 adições e 3 exclusões
  1. 11 3
      src/core/memory/globals.cpp

+ 11 - 3
src/core/memory/globals.cpp

@@ -340,14 +340,22 @@ namespace memory_globals
 {
 	using namespace memory;
 
-	static char _buffer[sizeof(HeapAllocator) + sizeof(ScratchAllocator)];
+	static const u32 _buffer_size = sizeof(HeapAllocator) + alignof(HeapAllocator)
+		+ sizeof(ScratchAllocator) + alignof(ScratchAllocator)
+		;
+	static char _buffer[_buffer_size];
 	static HeapAllocator *_default_allocator;
 	static ScratchAllocator *_default_scratch_allocator;
 
 	void init()
 	{
-		_default_allocator = new (_buffer) HeapAllocator();
-		_default_scratch_allocator = new (_buffer + sizeof(HeapAllocator)) ScratchAllocator(*_default_allocator, 1024*1024);
+		void *buf = _buffer;
+		buf = memory::align_top(buf, alignof(HeapAllocator));
+		_default_allocator = new (buf) HeapAllocator();
+
+		buf = _default_allocator + 1;
+		buf = memory::align_top(buf, alignof(ScratchAllocator));
+		_default_scratch_allocator = new (buf) ScratchAllocator(*_default_allocator, 1024*1024);
 	}
 
 	void shutdown()