Forráskód Böngészése

Use heap_allocator for all hash set types

gingerBill 2 éve
szülő
commit
600f2b7284

+ 1 - 1
src/build_settings.cpp

@@ -1369,7 +1369,7 @@ gb_internal bool init_build_paths(String init_filename) {
 	// NOTE(Jeroen): We're pre-allocating BuildPathCOUNT slots so that certain paths are always at the same enumerated index.
 	array_init(&bc->build_paths, permanent_allocator(), BuildPathCOUNT);
 
-	string_set_init(&bc->target_features_set, heap_allocator(), 1024);
+	string_set_init(&bc->target_features_set, 1024);
 
 	// [BuildPathMainPackage] Turn given init path into a `Path`, which includes normalizing it into a full path.
 	bc->build_paths[BuildPath_Main_Package] = path_from_string(ha, init_filename);

+ 1 - 1
src/check_builtin.cpp

@@ -3086,7 +3086,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As
 			}
 		}
 		StringSet name_set = {};
-		string_set_init(&name_set, heap_allocator(), 2*ce->args.count);
+		string_set_init(&name_set, 2*ce->args.count);
 
 		for_array(i, ce->args) {
 			String name = {};

+ 1 - 1
src/check_decl.cpp

@@ -1235,7 +1235,7 @@ gb_internal void check_proc_group_decl(CheckerContext *ctx, Entity *&pg_entity,
 	pg_entity->type = t_invalid;
 
 	PtrSet<Entity *> entity_set = {};
-	ptr_set_init(&entity_set, heap_allocator(), 2*pg->args.count);
+	ptr_set_init(&entity_set, 2*pg->args.count);
 
 	for_array(i, pg->args) {
 		Ast *arg = pg->args[i];

+ 0 - 1
src/check_expr.cpp

@@ -196,7 +196,6 @@ gb_internal void check_did_you_mean_objc_entity(String const &name, Entity *e, b
 	MUTEX_GUARD(objc_metadata->mutex);
 
 	StringSet set = {};
-	string_set_init(&set, heap_allocator());
 	defer (string_set_destroy(&set));
 	populate_check_did_you_mean_objc_entity(&set, e, is_type);
 

+ 0 - 1
src/check_stmt.cpp

@@ -1185,7 +1185,6 @@ gb_internal void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_
 	}
 
 	PtrSet<Type *> seen = {};
-	ptr_set_init(&seen, heap_allocator());
 	defer (ptr_set_destroy(&seen));
 
 	for_array(i, bs->stmts) {

+ 7 - 17
src/checker.cpp

@@ -72,7 +72,7 @@ gb_internal void entity_graph_node_set_destroy(EntityGraphNodeSet *s) {
 
 gb_internal void entity_graph_node_set_add(EntityGraphNodeSet *s, EntityGraphNode *n) {
 	if (s->hashes.data == nullptr) {
-		ptr_set_init(s, heap_allocator());
+		ptr_set_init(s);
 	}
 	ptr_set_add(s, n);
 }
@@ -118,15 +118,10 @@ gb_internal void entity_graph_node_swap(EntityGraphNode **data, isize i, isize j
 
 
 gb_internal void import_graph_node_set_destroy(ImportGraphNodeSet *s) {
-	if (s->hashes.data != nullptr) {
-		ptr_set_destroy(s);
-	}
+	ptr_set_destroy(s);
 }
 
 gb_internal void import_graph_node_set_add(ImportGraphNodeSet *s, ImportGraphNode *n) {
-	if (s->hashes.data == nullptr) {
-		ptr_set_init(s, heap_allocator());
-	}
 	ptr_set_add(s, n);
 }
 
@@ -185,8 +180,8 @@ gb_internal void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) {
 	gb_zero_item(d);
 	d->parent = parent;
 	d->scope  = scope;
-	ptr_set_init(&d->deps,           heap_allocator());
-	ptr_set_init(&d->type_info_deps, heap_allocator());
+	ptr_set_init(&d->deps);
+	ptr_set_init(&d->type_info_deps);
 	array_init  (&d->labels,         heap_allocator());
 }
 
@@ -227,7 +222,7 @@ gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_ele
 	Scope *s = gb_alloc_item(permanent_allocator(), Scope);
 	s->parent = parent;
 	string_map_init(&s->elements, heap_allocator(), init_elements_capacity);
-	ptr_set_init(&s->imported, heap_allocator(), 0);
+	ptr_set_init(&s->imported, 0);
 
 	if (parent != nullptr && parent != builtin_pkg->scope) {
 		Scope *prev_head_child = parent->head_child.exchange(s, std::memory_order_acq_rel);
@@ -2270,8 +2265,8 @@ gb_internal void generate_minimum_dependency_set(Checker *c, Entity *start) {
 	isize entity_count = c->info.entities.count;
 	isize min_dep_set_cap = next_pow2_isize(entity_count*4); // empirically determined factor
 
-	ptr_set_init(&c->info.minimum_dependency_set, heap_allocator(), min_dep_set_cap);
-	ptr_set_init(&c->info.minimum_dependency_type_info_set, heap_allocator());
+	ptr_set_init(&c->info.minimum_dependency_set, min_dep_set_cap);
+	ptr_set_init(&c->info.minimum_dependency_type_info_set);
 
 #define FORCE_ADD_RUNTIME_ENTITIES(condition, ...) do {                                              \
 	if (condition) {                                                                             \
@@ -3388,7 +3383,6 @@ gb_internal void check_decl_attributes(CheckerContext *c, Array<Ast *> const &at
 	}
 
 	StringSet set = {};
-	string_set_init(&set, heap_allocator());
 	defer (string_set_destroy(&set));
 
 	for_array(i, attributes) {
@@ -4759,7 +4753,6 @@ gb_internal void check_import_entities(Checker *c) {
 	auto pq = priority_queue_create(dep_graph, import_graph_node_cmp, import_graph_node_swap);
 
 	PtrSet<AstPackage *> emitted = {};
-	ptr_set_init(&emitted, heap_allocator());
 	defer (ptr_set_destroy(&emitted));
 
 	Array<ImportGraphNode *> package_order = {};
@@ -4773,7 +4766,6 @@ gb_internal void check_import_entities(Checker *c) {
 
 		if (n->dep_count > 0) {
 			PtrSet<AstPackage *> visited = {};
-			ptr_set_init(&visited, heap_allocator());
 			defer (ptr_set_destroy(&visited));
 
 			auto path = find_import_path(c, pkg, pkg, &visited);
@@ -4927,7 +4919,6 @@ gb_internal Array<Entity *> find_entity_path(Entity *start, Entity *end, PtrSet<
 	bool made_visited = false;
 	if (visited == nullptr) {
 		made_visited = true;
-		ptr_set_init(&visited_, heap_allocator());
 		visited = &visited_;
 	}
 	defer (if (made_visited) {
@@ -4990,7 +4981,6 @@ gb_internal void calculate_global_init_order(Checker *c) {
 	auto pq = priority_queue_create(dep_graph, entity_graph_node_cmp, entity_graph_node_swap);
 
 	PtrSet<DeclInfo *> emitted = {};
-	ptr_set_init(&emitted, heap_allocator());
 	defer (ptr_set_destroy(&emitted));
 
 	TIME_SECTION("calculate_global_init_order: queue sort");

+ 1 - 1
src/llvm_backend_general.cpp

@@ -133,7 +133,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) {
 
 
 	array_init(&gen->foreign_libraries,       heap_allocator(), 0, 1024);
-	ptr_set_init(&gen->foreign_libraries_set, heap_allocator(), 1024);
+	ptr_set_init(&gen->foreign_libraries_set, 1024);
 
 	if (USE_SEPARATE_MODULES) {
 		for (auto const &entry : gen->info->packages) {

+ 1 - 1
src/llvm_backend_type.cpp

@@ -2,7 +2,7 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_
 	auto *set = &info->minimum_dependency_type_info_set;
 	isize index = type_info_index(info, type, err_on_not_found);
 	if (index >= 0) {
-		isize i = ptr_entry_index(set, index);
+		isize i = ptr_set_entry_index(set, index);
 		if (i >= 0) {
 			return i+1;
 		}

+ 4 - 4
src/main.cpp

@@ -212,11 +212,11 @@ gb_internal i32 linker_stage(lbGenerator *gen) {
 
 
 			StringSet libs = {};
-			string_set_init(&libs, heap_allocator(), 64);
+			string_set_init(&libs, 64);
 			defer (string_set_destroy(&libs));
 
 			StringSet asm_files = {};
-			string_set_init(&asm_files, heap_allocator(), 64);
+			string_set_init(&asm_files, 64);
 			defer (string_set_destroy(&asm_files));
 
 			for_array(j, gen->foreign_libraries) {
@@ -371,7 +371,7 @@ gb_internal i32 linker_stage(lbGenerator *gen) {
 			defer (gb_string_free(lib_str));
 
 			StringSet libs = {};
-			string_set_init(&libs, heap_allocator(), 64);
+			string_set_init(&libs, 64);
 			defer (string_set_destroy(&libs));
 
 			for_array(j, gen->foreign_libraries) {
@@ -2518,7 +2518,7 @@ int main(int arg_count, char const **arg_ptr) {
 
 	map_init(&build_context.defined_values, heap_allocator());
 	build_context.extra_packages.allocator = heap_allocator();
-	string_set_init(&build_context.test_names, heap_allocator());
+	string_set_init(&build_context.test_names);
 
 	Array<String> args = setup_args(arg_count, arg_ptr);
 

+ 1 - 1
src/parser.cpp

@@ -4854,7 +4854,7 @@ gb_internal void destroy_ast_file(AstFile *f) {
 
 gb_internal bool init_parser(Parser *p) {
 	GB_ASSERT(p != nullptr);
-	string_set_init(&p->imported_files, heap_allocator());
+	string_set_init(&p->imported_files);
 	array_init(&p->packages, heap_allocator());
 	return true;
 }

+ 15 - 6
src/ptr_set.cpp

@@ -6,11 +6,11 @@ struct PtrSetEntry {
 
 template <typename T>
 struct PtrSet {
-	Slice<MapIndex>    hashes;
+	Slice<MapIndex>       hashes;
 	Array<PtrSetEntry<T>> entries;
 };
 
-template <typename T> gb_internal void ptr_set_init   (PtrSet<T> *s, gbAllocator a, isize capacity = 16);
+template <typename T> gb_internal void ptr_set_init   (PtrSet<T> *s, isize capacity = 16);
 template <typename T> gb_internal void ptr_set_destroy(PtrSet<T> *s);
 template <typename T> gb_internal T    ptr_set_add    (PtrSet<T> *s, T ptr);
 template <typename T> gb_internal bool ptr_set_update (PtrSet<T> *s, T ptr); // returns true if it previously existed
@@ -21,15 +21,18 @@ template <typename T> gb_internal void ptr_set_grow   (PtrSet<T> *s);
 template <typename T> gb_internal void ptr_set_rehash (PtrSet<T> *s, isize new_count);
 template <typename T> gb_internal void ptr_set_reserve(PtrSet<T> *h, isize cap);
 
+gb_internal gbAllocator ptr_set_allocator(void) {
+	return heap_allocator();
+}
 
 template <typename T>
-gb_internal void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) {
+gb_internal void ptr_set_init(PtrSet<T> *s, isize capacity) {
 	if (capacity != 0) {
 		capacity = next_pow2_isize(gb_max(16, capacity));
 	}
 
-	slice_init(&s->hashes,  a, capacity);
-	array_init(&s->entries, a, 0, capacity);
+	slice_init(&s->hashes,  ptr_set_allocator(), capacity);
+	array_init(&s->entries, ptr_set_allocator(), 0, capacity);
 	for (isize i = 0; i < capacity; i++) {
 		s->hashes.data[i] = MAP_SENTINEL;
 	}
@@ -37,6 +40,9 @@ gb_internal void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) {
 
 template <typename T>
 gb_internal void ptr_set_destroy(PtrSet<T> *s) {
+	if (s->entries.allocator.proc == nullptr) {
+		s->entries.allocator = ptr_set_allocator();
+	}
 	slice_free(&s->hashes, s->entries.allocator);
 	array_free(&s->entries);
 }
@@ -118,6 +124,9 @@ gb_internal void ptr_set_reset_entries(PtrSet<T> *s) {
 
 template <typename T>
 gb_internal void ptr_set_reserve(PtrSet<T> *s, isize cap) {
+	if (s->entries.allocator.proc == nullptr) {
+		s->entries.allocator = ptr_set_allocator();
+	}
 	array_reserve(&s->entries, cap);
 	if (s->entries.count*2 < s->hashes.count) {
 		return;
@@ -139,7 +148,7 @@ gb_internal gb_inline bool ptr_set_exists(PtrSet<T> *s, T ptr) {
 }
 
 template <typename T>
-gb_internal gb_inline isize ptr_entry_index(PtrSet<T> *s, T ptr) {
+gb_internal gb_inline isize ptr_set_entry_index(PtrSet<T> *s, T ptr) {
 	isize index = ptr_set__find(s, ptr).entry_index;
 	if (index != MAP_SENTINEL) {
 		return index;

+ 13 - 5
src/string_set.cpp

@@ -10,7 +10,7 @@ struct StringSet {
 };
 
 
-gb_internal void string_set_init   (StringSet *s, gbAllocator a, isize capacity = 16);
+gb_internal void string_set_init   (StringSet *s, isize capacity = 16);
 gb_internal void string_set_destroy(StringSet *s);
 gb_internal void string_set_add    (StringSet *s, String const &str);
 gb_internal bool string_set_update (StringSet *s, String const &str); // returns true if it previously existed
@@ -20,18 +20,24 @@ gb_internal void string_set_clear  (StringSet *s);
 gb_internal void string_set_grow   (StringSet *s);
 gb_internal void string_set_rehash (StringSet *s, isize new_count);
 
+gb_internal gbAllocator string_set_allocator(void) {
+	return heap_allocator();
+}
 
-gb_internal gb_inline void string_set_init(StringSet *s, gbAllocator a, isize capacity) {
+gb_internal gb_inline void string_set_init(StringSet *s, isize capacity) {
 	capacity = next_pow2_isize(gb_max(16, capacity));
 	
-	slice_init(&s->hashes,  a, capacity);
-	array_init(&s->entries, a, 0, capacity);
+	slice_init(&s->hashes,  string_set_allocator(), capacity);
+	array_init(&s->entries, string_set_allocator(), 0, capacity);
 	for (isize i = 0; i < capacity; i++) {
 		s->hashes.data[i] = MAP_SENTINEL;
 	}
 }
 
 gb_internal gb_inline void string_set_destroy(StringSet *s) {
+	if (s->entries.allocator.proc == nullptr) {
+		s->entries.allocator = string_set_allocator();
+	}
 	slice_free(&s->hashes, s->entries.allocator);
 	array_free(&s->entries);
 }
@@ -106,6 +112,9 @@ gb_internal void string_set_reset_entries(StringSet *s) {
 }
 
 gb_internal void string_set_reserve(StringSet *s, isize cap) {
+	if (s->entries.allocator.proc == nullptr) {
+		s->entries.allocator = string_set_allocator();
+	}
 	array_reserve(&s->entries, cap);
 	if (s->entries.count*2 < s->hashes.count) {
 		return;
@@ -217,7 +226,6 @@ gb_internal gb_inline void string_set_clear(StringSet *s) {
 }
 
 
-
 gb_internal StringSetEntry *begin(StringSet &m) {
 	return m.entries.data;
 }