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.
This commit is contained in:
Kevin Svetlitski 2023-05-18 10:22:58 -07:00 committed by Qi Wang
parent e249d1a2a1
commit 589c63b424
21 changed files with 30 additions and 33 deletions

View File

@ -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;

View File

@ -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. */

View File

@ -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;

View File

@ -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 */

View File

@ -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;

View File

@ -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) {

View File

@ -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,

View File

@ -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);

View File

@ -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];

View File

@ -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"

View File

@ -10,7 +10,7 @@
const char *opt_dss = DSS_DEFAULT;
const char *dss_prec_names[] = {
const char *const dss_prec_names[] = {
"disabled",
"primary",
"secondary",

View File

@ -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,

View File

@ -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;

View File

@ -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",
};

View File

@ -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(&central->hpa, base, hpa_hooks);

View File

@ -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",

View File

@ -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;
/******************************************************************************/

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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);