Explorar o código

core: use single big buffer instead of separated allocations

Daniele Bartolini %!s(int64=7) %!d(string=hai) anos
pai
achega
c937e09464
Modificáronse 2 ficheiros con 7 adicións e 4 borrados
  1. 6 4
      src/core/containers/hash_map.h
  2. 1 0
      src/core/containers/types.h

+ 6 - 4
src/core/containers/hash_map.h

@@ -149,8 +149,10 @@ namespace hash_map_internal
 		typedef typename HashMap<TKey, TValue, Hash, KeyEqual>::Index Index;
 
 		HashMap<TKey, TValue, Hash, KeyEqual> nm(*m._allocator);
-		nm._index = (Index*)nm._allocator->allocate(new_capacity*sizeof(Index), alignof(Index));
-		nm._data = (Entry*)nm._allocator->allocate(new_capacity*sizeof(Entry), alignof(Entry));
+		const u32 size = new_capacity*(sizeof(Index) + sizeof(Entry)) + alignof(Index) + alignof(Entry);
+		nm._buffer = (char*)nm._allocator->allocate(size);
+		nm._index = (Index*)memory::align_top(nm._buffer, alignof(Index));
+		nm._data = (Entry*)memory::align_top(nm._index + new_capacity, alignof(Entry));
 
 		// Flag all elements as free
 		for (u32 i = 0; i < new_capacity; ++i)
@@ -280,6 +282,7 @@ HashMap<TKey, TValue, Hash, KeyEqual>::HashMap(Allocator& a)
 	, _mask(0)
 	, _index(NULL)
 	, _data(NULL)
+	, _buffer(NULL)
 {
 }
 
@@ -292,8 +295,7 @@ HashMap<TKey, TValue, Hash, KeyEqual>::~HashMap()
 			_data[i].~Pair();
 	}
 
-	_allocator->deallocate(_index);
-	_allocator->deallocate(_data);
+	_allocator->deallocate(_buffer);
 }
 
 } // namespace crown

+ 1 - 0
src/core/containers/types.h

@@ -138,6 +138,7 @@ struct HashMap
 	u32 _mask;
 	Index* _index;
 	Entry* _data;
+	char* _buffer;
 
 	HashMap(Allocator& a);
 	~HashMap();