From 589c63b4244e60dcfe74861a2b110b545182216f Mon Sep 17 00:00:00 2001 From: Kevin Svetlitski Date: Thu, 18 May 2023 10:22:58 -0700 Subject: [PATCH] Make eligible global variables `static` and/or `const` For better or worse, Jemalloc has a significant number of global variables. Making all eligible global variables `static` and/or `const` at least makes it slightly easier to reason about them, as these qualifications communicate to the programmer restrictions on their use without having to `grep` the whole codebase. --- include/jemalloc/internal/arena_externs.h | 3 +-- include/jemalloc/internal/base.h | 2 +- include/jemalloc/internal/extent_dss.h | 2 +- include/jemalloc/internal/hpa_hooks.h | 2 +- include/jemalloc/internal/jemalloc_internal_externs.h | 6 +++--- include/jemalloc/internal/nstime.h | 2 +- include/jemalloc/internal/pa.h | 2 +- include/jemalloc/internal/pages.h | 2 +- src/arena.c | 4 ++-- src/base.c | 2 +- src/extent_dss.c | 2 +- src/hpa_hooks.c | 2 +- src/jemalloc.c | 8 ++++---- src/nstime.c | 2 +- src/pa.c | 2 +- src/pages.c | 2 +- src/prof.c | 8 ++++---- src/prof_log.c | 2 +- src/prof_sys.c | 2 -- src/stats.c | 4 ++-- test/unit/hpa.c | 2 +- 21 files changed, 30 insertions(+), 33 deletions(-) diff --git a/include/jemalloc/internal/arena_externs.h b/include/jemalloc/internal/arena_externs.h index 2d82ad8f..8e323639 100644 --- a/include/jemalloc/internal/arena_externs.h +++ b/include/jemalloc/internal/arena_externs.h @@ -18,11 +18,10 @@ extern ssize_t opt_dirty_decay_ms; extern ssize_t opt_muzzy_decay_ms; extern percpu_arena_mode_t opt_percpu_arena; -extern const char *percpu_arena_mode_names[]; +extern const char *const percpu_arena_mode_names[]; extern div_info_t arena_binind_div_info[SC_NBINS]; -extern malloc_mutex_t arenas_lock; extern emap_t arena_emap_global; extern size_t opt_oversize_threshold; diff --git a/include/jemalloc/internal/base.h b/include/jemalloc/internal/base.h index 9b2c9fb1..23207563 100644 --- a/include/jemalloc/internal/base.h +++ b/include/jemalloc/internal/base.h @@ -23,7 +23,7 @@ typedef enum metadata_thp_mode_e metadata_thp_mode_t; #define METADATA_THP_DEFAULT metadata_thp_disabled extern metadata_thp_mode_t opt_metadata_thp; -extern const char *metadata_thp_mode_names[]; +extern const char *const metadata_thp_mode_names[]; /* Embedded at the beginning of every block of base-managed virtual memory. */ diff --git a/include/jemalloc/internal/extent_dss.h b/include/jemalloc/internal/extent_dss.h index e8f02ce2..38f04340 100644 --- a/include/jemalloc/internal/extent_dss.h +++ b/include/jemalloc/internal/extent_dss.h @@ -11,7 +11,7 @@ typedef enum { #define DSS_PREC_DEFAULT dss_prec_secondary #define DSS_DEFAULT "secondary" -extern const char *dss_prec_names[]; +extern const char *const dss_prec_names[]; extern const char *opt_dss; diff --git a/include/jemalloc/internal/hpa_hooks.h b/include/jemalloc/internal/hpa_hooks.h index 4ea221cb..841f529e 100644 --- a/include/jemalloc/internal/hpa_hooks.h +++ b/include/jemalloc/internal/hpa_hooks.h @@ -12,6 +12,6 @@ struct hpa_hooks_s { uint64_t (*ms_since)(nstime_t *r_time); }; -extern hpa_hooks_t hpa_hooks_default; +extern const hpa_hooks_t hpa_hooks_default; #endif /* JEMALLOC_INTERNAL_HPA_HOOKS_H */ diff --git a/include/jemalloc/internal/jemalloc_internal_externs.h b/include/jemalloc/internal/jemalloc_internal_externs.h index d90f6ddb..b1e5bde9 100644 --- a/include/jemalloc/internal/jemalloc_internal_externs.h +++ b/include/jemalloc/internal/jemalloc_internal_externs.h @@ -22,8 +22,8 @@ extern sec_opts_t opt_hpa_sec_opts; extern const char *opt_junk; extern bool opt_junk_alloc; extern bool opt_junk_free; -extern void (*junk_free_callback)(void *ptr, size_t size); -extern void (*junk_alloc_callback)(void *ptr, size_t size); +extern void (*JET_MUTABLE junk_free_callback)(void *ptr, size_t size); +extern void (*JET_MUTABLE junk_alloc_callback)(void *ptr, size_t size); extern bool opt_utrace; extern bool opt_xmalloc; extern bool opt_experimental_infallible_new; @@ -31,7 +31,7 @@ extern bool opt_zero; extern unsigned opt_narenas; extern zero_realloc_action_t opt_zero_realloc_action; extern malloc_init_t malloc_init_state; -extern const char *zero_realloc_mode_names[]; +extern const char *const zero_realloc_mode_names[]; extern atomic_zu_t zero_realloc_count; extern bool opt_cache_oblivious; extern unsigned opt_debug_double_free_max_scan; diff --git a/include/jemalloc/internal/nstime.h b/include/jemalloc/internal/nstime.h index 486e5cca..ad1ae532 100644 --- a/include/jemalloc/internal/nstime.h +++ b/include/jemalloc/internal/nstime.h @@ -56,7 +56,7 @@ enum prof_time_res_e { typedef enum prof_time_res_e prof_time_res_t; extern prof_time_res_t opt_prof_time_res; -extern const char *prof_time_res_mode_names[]; +extern const char *const prof_time_res_mode_names[]; JEMALLOC_ALWAYS_INLINE void nstime_init_zero(nstime_t *time) { diff --git a/include/jemalloc/internal/pa.h b/include/jemalloc/internal/pa.h index 4748a05b..c8aed932 100644 --- a/include/jemalloc/internal/pa.h +++ b/include/jemalloc/internal/pa.h @@ -131,7 +131,7 @@ pa_shard_ehooks_get(pa_shard_t *shard) { /* Returns true on error. */ bool pa_central_init(pa_central_t *central, base_t *base, bool hpa, - hpa_hooks_t *hpa_hooks); + const hpa_hooks_t *hpa_hooks); /* Returns true on error. */ bool pa_shard_init(tsdn_t *tsdn, pa_shard_t *shard, pa_central_t *central, diff --git a/include/jemalloc/internal/pages.h b/include/jemalloc/internal/pages.h index 361de587..c9d10ce2 100644 --- a/include/jemalloc/internal/pages.h +++ b/include/jemalloc/internal/pages.h @@ -102,7 +102,7 @@ typedef enum { #define THP_MODE_DEFAULT thp_mode_default extern thp_mode_t opt_thp; extern thp_mode_t init_system_thp_mode; /* Initial system wide state. */ -extern const char *thp_mode_names[]; +extern const char *const thp_mode_names[]; void *pages_map(void *addr, size_t size, size_t alignment, bool *commit); void pages_unmap(void *addr, size_t size); diff --git a/src/arena.c b/src/arena.c index b154b7a5..a8890e57 100644 --- a/src/arena.c +++ b/src/arena.c @@ -21,7 +21,7 @@ JEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS * Define names for both unininitialized and initialized phases, so that * options and mallctl processing are straightforward. */ -const char *percpu_arena_mode_names[] = { +const char *const percpu_arena_mode_names[] = { "percpu", "phycpu", "disabled", @@ -37,7 +37,7 @@ static atomic_zd_t dirty_decay_ms_default; static atomic_zd_t muzzy_decay_ms_default; emap_t arena_emap_global; -pa_central_t arena_pa_central_global; +static pa_central_t arena_pa_central_global; div_info_t arena_binind_div_info[SC_NBINS]; diff --git a/src/base.c b/src/base.c index 7f4d6756..16f90495 100644 --- a/src/base.c +++ b/src/base.c @@ -22,7 +22,7 @@ static base_t *b0; metadata_thp_mode_t opt_metadata_thp = METADATA_THP_DEFAULT; -const char *metadata_thp_mode_names[] = { +const char *const metadata_thp_mode_names[] = { "disabled", "auto", "always" diff --git a/src/extent_dss.c b/src/extent_dss.c index 9a35bacf..0b846296 100644 --- a/src/extent_dss.c +++ b/src/extent_dss.c @@ -10,7 +10,7 @@ const char *opt_dss = DSS_DEFAULT; -const char *dss_prec_names[] = { +const char *const dss_prec_names[] = { "disabled", "primary", "secondary", diff --git a/src/hpa_hooks.c b/src/hpa_hooks.c index ade581e8..6048f382 100644 --- a/src/hpa_hooks.c +++ b/src/hpa_hooks.c @@ -11,7 +11,7 @@ static void hpa_hooks_dehugify(void *ptr, size_t size); static void hpa_hooks_curtime(nstime_t *r_nstime, bool first_reading); static uint64_t hpa_hooks_ms_since(nstime_t *past_nstime); -hpa_hooks_t hpa_hooks_default = { +const hpa_hooks_t hpa_hooks_default = { &hpa_hooks_map, &hpa_hooks_unmap, &hpa_hooks_purge, diff --git a/src/jemalloc.c b/src/jemalloc.c index 7771a731..8de30279 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -121,7 +121,7 @@ zero_realloc_action_t opt_zero_realloc_action = atomic_zu_t zero_realloc_count = ATOMIC_INIT(0); -const char *zero_realloc_mode_names[] = { +const char *const zero_realloc_mode_names[] = { "alloc", "free", "abort", @@ -142,8 +142,8 @@ static void default_junk_free(void *ptr, size_t usize) { memset(ptr, junk_free_byte, usize); } -void (*junk_alloc_callback)(void *ptr, size_t size) = &default_junk_alloc; -void (*junk_free_callback)(void *ptr, size_t size) = &default_junk_free; +void (*JET_MUTABLE junk_alloc_callback)(void *ptr, size_t size) = &default_junk_alloc; +void (*JET_MUTABLE junk_free_callback)(void *ptr, size_t size) = &default_junk_free; bool opt_utrace = false; bool opt_xmalloc = false; @@ -158,7 +158,7 @@ unsigned opt_debug_double_free_max_scan = SAFETY_CHECK_DOUBLE_FREE_MAX_SCAN_DEFAULT; /* Protects arenas initialization. */ -malloc_mutex_t arenas_lock; +static malloc_mutex_t arenas_lock; /* The global hpa, and whether it's on. */ bool opt_hpa = false; diff --git a/src/nstime.c b/src/nstime.c index a1a53777..7fb9100e 100644 --- a/src/nstime.c +++ b/src/nstime.c @@ -228,7 +228,7 @@ nstime_monotonic_t *JET_MUTABLE nstime_monotonic = nstime_monotonic_impl; prof_time_res_t opt_prof_time_res = prof_time_res_default; -const char *prof_time_res_mode_names[] = { +const char *const prof_time_res_mode_names[] = { "default", "high", }; diff --git a/src/pa.c b/src/pa.c index 18c850d7..63eef2b5 100644 --- a/src/pa.c +++ b/src/pa.c @@ -17,7 +17,7 @@ pa_nactive_sub(pa_shard_t *shard, size_t sub_pages) { bool pa_central_init(pa_central_t *central, base_t *base, bool hpa, - hpa_hooks_t *hpa_hooks) { + const hpa_hooks_t *hpa_hooks) { bool err; if (hpa) { err = hpa_central_init(¢ral->hpa, base, hpa_hooks); diff --git a/src/pages.c b/src/pages.c index e70c6e92..249d7c5b 100644 --- a/src/pages.c +++ b/src/pages.c @@ -42,7 +42,7 @@ static int mmap_flags; #endif static bool os_overcommits; -const char *thp_mode_names[] = { +const char *const thp_mode_names[] = { "default", "always", "never", diff --git a/src/prof.c b/src/prof.c index e958349e..9986a329 100644 --- a/src/prof.c +++ b/src/prof.c @@ -73,16 +73,16 @@ static malloc_mutex_t next_thr_uid_mtx; bool prof_booted = false; /* Logically a prof_backtrace_hook_t. */ -atomic_p_t prof_backtrace_hook; +static atomic_p_t prof_backtrace_hook; /* Logically a prof_dump_hook_t. */ -atomic_p_t prof_dump_hook; +static atomic_p_t prof_dump_hook; /* Logically a prof_sample_hook_t. */ -atomic_p_t prof_sample_hook; +static atomic_p_t prof_sample_hook; /* Logically a prof_sample_free_hook_t. */ -atomic_p_t prof_sample_free_hook; +static atomic_p_t prof_sample_free_hook; /******************************************************************************/ diff --git a/src/prof_log.c b/src/prof_log.c index 384d5e38..f4000aec 100644 --- a/src/prof_log.c +++ b/src/prof_log.c @@ -25,7 +25,7 @@ enum prof_logging_state_e { * - started: log_start called, log_stop not called yet. Allocations are logged. * - dumping: log_stop called but not finished; samples are not logged anymore. */ -prof_logging_state_t prof_logging_state = prof_logging_state_stopped; +static prof_logging_state_t prof_logging_state = prof_logging_state_stopped; /* Used in unit tests. */ static bool prof_log_dummy = false; diff --git a/src/prof_sys.c b/src/prof_sys.c index 467394a5..1f8ecb62 100644 --- a/src/prof_sys.c +++ b/src/prof_sys.c @@ -27,8 +27,6 @@ malloc_mutex_t prof_dump_filename_mtx; -bool prof_do_mock = false; - static uint64_t prof_dump_seq; static uint64_t prof_dump_iseq; static uint64_t prof_dump_mseq; diff --git a/src/stats.c b/src/stats.c index 59db4f8e..d80af226 100644 --- a/src/stats.c +++ b/src/stats.c @@ -9,13 +9,13 @@ #include "jemalloc/internal/mutex_prof.h" #include "jemalloc/internal/prof_stats.h" -const char *global_mutex_names[mutex_prof_num_global_mutexes] = { +static const char *const global_mutex_names[mutex_prof_num_global_mutexes] = { #define OP(mtx) #mtx, MUTEX_PROF_GLOBAL_MUTEXES #undef OP }; -const char *arena_mutex_names[mutex_prof_num_arena_mutexes] = { +static const char *const arena_mutex_names[mutex_prof_num_arena_mutexes] = { #define OP(mtx) #mtx, MUTEX_PROF_ARENA_MUTEXES #undef OP diff --git a/test/unit/hpa.c b/test/unit/hpa.c index f7874281..64aef59e 100644 --- a/test/unit/hpa.c +++ b/test/unit/hpa.c @@ -37,7 +37,7 @@ static hpa_shard_opts_t test_hpa_shard_opts_default = { }; static hpa_shard_t * -create_test_data(hpa_hooks_t *hooks, hpa_shard_opts_t *opts) { +create_test_data(const hpa_hooks_t *hooks, hpa_shard_opts_t *opts) { bool err; base_t *base = base_new(TSDN_NULL, /* ind */ SHARD_IND, &ehooks_default_extent_hooks, /* metadata_use_hooks */ true);