From 10f4d8df32f73e3c5071a6e168a0923a53d8332f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 14 Jul 2021 23:36:23 +0100 Subject: [PATCH] Override libtommath allocation procedures --- src/big_int.cpp | 31 +++++++++++++++++++++++++++++++ src/checker.cpp | 12 ++++++------ src/checker.hpp | 25 +++++++++++++------------ src/libtommath/tommath_private.h | 18 +++++++++--------- 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/big_int.cpp b/src/big_int.cpp index 6386b1d6f..20f940e8e 100644 --- a/src/big_int.cpp +++ b/src/big_int.cpp @@ -1,5 +1,36 @@ #include "libtommath/tommath.h" +#if 0 +void *MP_MALLOC(size_t size) { + return malloc(size); +} +void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize) { + return realloc(mem, newsize); +} +void *MP_CALLOC(size_t nmemb, size_t size) { + return calloc(nmemb, size); +} +void MP_FREE(void *mem, size_t size) { + free(mem); +} +#else + +void *MP_MALLOC(size_t size) { + return gb_alloc(permanent_allocator(), cast(isize)size); +} +void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize) { + return gb_resize(permanent_allocator(), mem, cast(isize)oldsize, cast(isize)newsize); +} +void *MP_CALLOC(size_t nmemb, size_t size) { + size_t total = nmemb*size; + return gb_alloc(permanent_allocator(), cast(isize)total); +} +void MP_FREE(void *mem, size_t size) { + // DO NOTHING +} +#endif + + #ifndef MAX_BIG_INT_SHIFT #define MAX_BIG_INT_SHIFT 1024 #endif diff --git a/src/checker.cpp b/src/checker.cpp index ac783bcca..3800f3e4c 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -849,8 +849,8 @@ void init_checker_info(CheckerInfo *i) { string_map_init(&i->files, a); string_map_init(&i->packages, a); array_init(&i->variable_init_order, a); - array_init(&i->required_foreign_imports_through_force, a); array_init(&i->testing_procedures, a, 0, 0); + array_init(&i->required_foreign_imports_through_force, a, 0, 0); i->allow_identifier_uses = build_context.query_data_set_settings.kind == QueryDataSet_GoToDefinitions; @@ -864,6 +864,7 @@ void init_checker_info(CheckerInfo *i) { mpmc_init(&i->entity_queue, a, 1<<20); mpmc_init(&i->definition_queue, a, 1<<20); mpmc_init(&i->required_global_variable_queue, a, 1<<10); + mpmc_init(&i->required_foreign_imports_through_force_queue, a, 1<<10); TIME_SECTION("checker info: mutexes"); @@ -897,6 +898,7 @@ void destroy_checker_info(CheckerInfo *i) { mpmc_destroy(&i->entity_queue); mpmc_destroy(&i->definition_queue); mpmc_destroy(&i->required_global_variable_queue); + mpmc_destroy(&i->required_foreign_imports_through_force_queue); gb_mutex_destroy(&i->gen_procs_mutex); gb_mutex_destroy(&i->gen_types_mutex); @@ -1893,8 +1895,8 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) { } } - for_array(i, c->info.required_foreign_imports_through_force) { - Entity *e = c->info.required_foreign_imports_through_force[i]; + for (Entity *e; mpmc_dequeue(&c->info.required_foreign_imports_through_force_queue, &e); /**/) { + array_add(&c->info.required_foreign_imports_through_force, e); add_dependency_to_set(c, e); } @@ -3793,9 +3795,7 @@ void check_add_foreign_import_decl(CheckerContext *ctx, Ast *decl) { AttributeContext ac = {}; check_decl_attributes(ctx, fl->attributes, foreign_import_decl_attribute, &ac); if (ac.require_declaration) { - mutex_lock(&ctx->info->foreign_mutex); - array_add(&ctx->info->required_foreign_imports_through_force, e); - mutex_unlock(&ctx->info->foreign_mutex); + mpmc_enqueue(&ctx->info->required_foreign_imports_through_force_queue, e); add_entity_use(ctx, nullptr, e); } } diff --git a/src/checker.hpp b/src/checker.hpp index 147e655f2..2a5ab0f44 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -291,27 +291,27 @@ struct CheckerInfo { Array definitions; Array entities; + Array required_foreign_imports_through_force; // Below are accessed within procedures // NOTE(bill): If the semantic checker (check_proc_body) is to ever to be multithreaded, // these variables will be of contention - gbMutex gen_procs_mutex; // Possibly recursive - gbMutex gen_types_mutex; // Possibly recursive + BlockingMutex deps_mutex; // NOT recursive & Only used in `check_proc_body` + BlockingMutex scope_mutex; // NOT recursive & Only used in `create_scope` + + gbMutex gen_procs_mutex; // Possibly recursive + gbMutex gen_types_mutex; // Possibly recursive + Map > gen_procs; // Key: Ast * | Identifier -> Entity + Map > gen_types; // Key: Type * + BlockingMutex type_info_mutex; // NOT recursive - BlockingMutex deps_mutex; // NOT recursive & Only used in `check_proc_body` - BlockingMutex foreign_mutex; // NOT recursive - BlockingMutex scope_mutex; // NOT recursive & Only used in `create_scope` - - Map > gen_procs; // Key: Ast * | Identifier -> Entity - Map > gen_types; // Key: Type * - - Array type_info_types; - Map type_info_map; // Key: Type * + Array type_info_types; + Map type_info_map; // Key: Type * + BlockingMutex foreign_mutex; // NOT recursive StringMap foreigns; - Array required_foreign_imports_through_force; // only used by 'odin query' bool allow_identifier_uses; @@ -323,6 +323,7 @@ struct CheckerInfo { MPMCQueue definition_queue; MPMCQueue entity_queue; MPMCQueue required_global_variable_queue; + MPMCQueue required_foreign_imports_through_force_queue; }; diff --git a/src/libtommath/tommath_private.h b/src/libtommath/tommath_private.h index 42920519c..21e51bf47 100644 --- a/src/libtommath/tommath_private.h +++ b/src/libtommath/tommath_private.h @@ -89,20 +89,20 @@ do { \ #endif /* define heap macros */ -#ifndef MP_MALLOC -/* default to libc stuff */ -# include -# define MP_MALLOC(size) malloc(size) -# define MP_REALLOC(mem, oldsize, newsize) realloc((mem), (newsize)) -# define MP_CALLOC(nmemb, size) calloc((nmemb), (size)) -# define MP_FREE(mem, size) free(mem) -#else +// #ifndef MP_MALLOC +// /* default to libc stuff */ +// # include +// # define MP_MALLOC(size) malloc(size) +// # define MP_REALLOC(mem, oldsize, newsize) realloc((mem), (newsize)) +// # define MP_CALLOC(nmemb, size) calloc((nmemb), (size)) +// # define MP_FREE(mem, size) free(mem) +// #else /* prototypes for our heap functions */ extern void *MP_MALLOC(size_t size); extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize); extern void *MP_CALLOC(size_t nmemb, size_t size); extern void MP_FREE(void *mem, size_t size); -#endif +// #endif /* feature detection macro */ #ifdef _MSC_VER