Browse Source

Minor changes to `StringMap` allocation

gingerBill 2 years ago
parent
commit
868aa4c14a
3 changed files with 11 additions and 5 deletions
  1. 2 2
      src/common_memory.cpp
  2. 1 1
      src/parser.cpp
  3. 8 2
      src/string_map.cpp

+ 2 - 2
src/common_memory.cpp

@@ -509,7 +509,7 @@ gb_internal GB_ALLOCATOR_PROC(heap_allocator_proc) {
 
 
 
 
 template <typename T>
 template <typename T>
-gb_internal isize resize_array_raw(T **array, gbAllocator const &a, isize old_count, isize new_count) {
+gb_internal isize resize_array_raw(T **array, gbAllocator const &a, isize old_count, isize new_count, isize custom_alignment=1) {
 	GB_ASSERT(new_count >= 0);
 	GB_ASSERT(new_count >= 0);
 	if (new_count == 0) {
 	if (new_count == 0) {
 		gb_free(a, *array);
 		gb_free(a, *array);
@@ -521,7 +521,7 @@ gb_internal isize resize_array_raw(T **array, gbAllocator const &a, isize old_co
 	}
 	}
 	isize old_size = old_count * gb_size_of(T);
 	isize old_size = old_count * gb_size_of(T);
 	isize new_size = new_count * gb_size_of(T);
 	isize new_size = new_count * gb_size_of(T);
-	isize alignment = gb_align_of(T);
+	isize alignment = gb_max(gb_align_of(T), custom_alignment);
 	auto new_data = cast(T *)gb_resize_align(a, *array, old_size, new_size, alignment);
 	auto new_data = cast(T *)gb_resize_align(a, *array, old_size, new_size, alignment);
 	GB_ASSERT(new_data != nullptr);
 	GB_ASSERT(new_data != nullptr);
 	*array = new_data;
 	*array = new_data;

+ 1 - 1
src/parser.cpp

@@ -89,7 +89,7 @@ gb_internal Array<Ast *> clone_ast_array(Array<Ast *> const &array, AstFile *f)
 gb_internal Slice<Ast *> clone_ast_array(Slice<Ast *> const &array, AstFile *f) {
 gb_internal Slice<Ast *> clone_ast_array(Slice<Ast *> const &array, AstFile *f) {
 	Slice<Ast *> result = {};
 	Slice<Ast *> result = {};
 	if (array.count > 0) {
 	if (array.count > 0) {
-		result = slice_clone(permanent_allocator(), array);
+		result = slice_clone(ast_allocator(nullptr), array);
 		for_array(i, array) {
 		for_array(i, array) {
 			result[i] = clone_ast(array[i], f);
 			result[i] = clone_ast(array[i], f);
 		}
 		}

+ 8 - 2
src/string_map.cpp

@@ -1,5 +1,11 @@
 GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32));
 GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32));
 
 
+enum {
+	STRING_MAP_CACHE_LINE_SIZE_POW = 6,
+	STRING_MAP_CACHE_LINE_SIZE = 1<<STRING_MAP_CACHE_LINE_SIZE_POW,
+	STRING_MAP_CACHE_LINE_MASK = STRING_MAP_CACHE_LINE_SIZE-1,
+};
+
 struct StringHashKey {
 struct StringHashKey {
 	u32    hash;
 	u32    hash;
 	String string;
 	String string;
@@ -79,13 +85,13 @@ gb_internal gb_inline void string_map_destroy(StringMap<T> *h) {
 
 
 template <typename T>
 template <typename T>
 gb_internal void string_map__resize_hashes(StringMap<T> *h, usize count) {
 gb_internal void string_map__resize_hashes(StringMap<T> *h, usize count) {
-	h->hashes_count = cast(u32)resize_array_raw(&h->hashes, string_map_allocator(), h->hashes_count, count);
+	h->hashes_count = cast(u32)resize_array_raw(&h->hashes, string_map_allocator(), h->hashes_count, count, STRING_MAP_CACHE_LINE_SIZE);
 }
 }
 
 
 
 
 template <typename T>
 template <typename T>
 gb_internal void string_map__reserve_entries(StringMap<T> *h, usize capacity) {
 gb_internal void string_map__reserve_entries(StringMap<T> *h, usize capacity) {
-	h->entries_capacity = cast(u32)resize_array_raw(&h->entries, string_map_allocator(), h->entries_capacity, capacity);
+	h->entries_capacity = cast(u32)resize_array_raw(&h->entries, string_map_allocator(), h->entries_capacity, capacity, STRING_MAP_CACHE_LINE_SIZE);
 }
 }