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

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