Skip to content

Commit

Permalink
avoid declaring a global variable
Browse files Browse the repository at this point in the history
Declares a variable that is used only in a
function as a local variable.
Declares a global variable that is used only
in one file as a static global variable.
  • Loading branch information
luochunsheng authored and andikleen committed Aug 25, 2022
1 parent 10c277c commit e2d6083
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 50 deletions.
2 changes: 1 addition & 1 deletion memhog.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ enum {
#define MADV_NOHUGEPAGE 15
#endif

int repeat = 1;

void usage(void)
{
Expand Down Expand Up @@ -73,6 +72,7 @@ int main(int ac, char **av)
int i;
int fd = -1;
bool disable_hugepage = false;
int repeat = 1;

nodes = numa_allocate_nodemask();
gnodes = numa_allocate_nodemask();
Expand Down
2 changes: 1 addition & 1 deletion migratepages.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "numaint.h"
#include "util.h"

struct option opts[] = {
static struct option opts[] = {
{"help", 0, 0, 'h' },
{ 0 }
};
Expand Down
19 changes: 7 additions & 12 deletions migspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@
#include <unistd.h>
#include "util.h"

char *memory;

unsigned long pages = 1000;

unsigned long pagesize;

const char *optstr = "hvp:";

char *cmd;

int verbose;
struct timespec start,end;
static const char *optstr = "hvp:";
static char *cmd;
static int verbose;
static unsigned long pages = 1000;

void usage(void)
{
Expand Down Expand Up @@ -70,6 +62,9 @@ int main(int argc, char *argv[])
double duration, mbytes;
struct bitmask *from;
struct bitmask *to;
char *memory = NULL;
unsigned long pagesize;
struct timespec start,end;

pagesize = getpagesize();

Expand Down
21 changes: 11 additions & 10 deletions numactl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

int exitcode;

struct option opts[] = {
static struct option opts[] = {
{"all", 0, 0, 'a'},
{"interleave", 1, 0, 'i' },
{"preferred", 1, 0, 'p' },
Expand Down Expand Up @@ -342,15 +342,10 @@ void nopolicy(void)
usage_msg("specify policy after --shm/--file");
}

int did_cpubind = 0;
int did_strict = 0;
int do_shm = 0;
int do_dump = 0;
int shmattached = 0;
int did_node_cpu_parse = 0;
int parse_all = 0;
int numa_balancing = 0;
char *shmoption;

static int shmattached = 0;
static int did_node_cpu_parse = 0;
static char *shmoption;

void check_cpubind(int flag)
{
Expand Down Expand Up @@ -432,6 +427,12 @@ int main(int ac, char **av)
char *end;
char shortopts[array_len(opts)*2 + 1];
struct bitmask *mask = NULL;
int did_cpubind = 0;
int did_strict = 0;
int do_shm = 0;
int do_dump = 0;
int parse_all = 0;
int numa_balancing = 0;

get_short_opts(opts,shortopts);
while ((c = getopt_long(ac, av, shortopts, opts, NULL)) != -1) {
Expand Down
17 changes: 9 additions & 8 deletions numademo.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ static inline void clearcache(void *a, unsigned size) {}
#endif
#define FRACT_NODES 8
#define FRACT_MASKS 32
int fract_nodes;
int *node_to_use;
unsigned long msize;
static int fract_nodes;
static int *node_to_use;
static unsigned long msize;

/* Should get this from cpuinfo, but on !x86 it's not there */
enum {
Expand All @@ -57,11 +57,10 @@ enum test {
PTRCHASE,
} thistest;

char *delim = " ";
int force;
int regression_testing=0;
static char *delim = " ";
static int regression_testing=0;

char *testname[] = {
static char *testname[] = {
"memset",
"memcpy",
"forward",
Expand Down Expand Up @@ -297,12 +296,13 @@ int popcnt(unsigned long val)
return cnt;
}

int max_node, numnodes;
static int numnodes;

int get_node_list(void)
{
int a, got_nodes = 0;
long long free_node_sizes;
int max_node;

numnodes = numa_num_configured_nodes();
node_to_use = (int *)malloc(numnodes * sizeof(int));
Expand Down Expand Up @@ -499,6 +499,7 @@ int main(int ac, char **av)
{
int simple_tests = 0;
int nr_nodes;
int force = 0;

while (av[1] && av[1][0] == '-') {
ac--;
Expand Down
36 changes: 18 additions & 18 deletions numastat.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef struct meminfo {
#define PROCESS_HUGE_INDEX 0
#define PROCESS_PRIVATE_INDEX 3

meminfo_t process_meminfo[] = {
static meminfo_t process_meminfo[] = {
{ PROCESS_HUGE_INDEX, "huge", "Huge" },
{ 1, "heap", "Heap" },
{ 2, "stack", "Stack" },
Expand All @@ -80,7 +80,7 @@ meminfo_t process_meminfo[] = {

#define PROCESS_MEMINFO_ROWS (sizeof(process_meminfo) / sizeof(process_meminfo[0]))

meminfo_t numastat_meminfo[] = {
static meminfo_t numastat_meminfo[] = {
{ 0, "numa_hit", "Numa_Hit" },
{ 1, "numa_miss", "Numa_Miss" },
{ 2, "numa_foreign", "Numa_Foreign" },
Expand All @@ -91,7 +91,7 @@ meminfo_t numastat_meminfo[] = {

#define NUMASTAT_MEMINFO_ROWS (sizeof(numastat_meminfo) / sizeof(numastat_meminfo[0]))

meminfo_t system_meminfo[] = {
static meminfo_t system_meminfo[] = {
{ 0, "MemTotal", "MemTotal" },
{ 1, "MemFree", "MemFree" },
{ 2, "MemUsed", "MemUsed" },
Expand Down Expand Up @@ -139,7 +139,7 @@ meminfo_t system_meminfo[] = {
// hash algorithms depend on having some unused buckets.

#define HASH_TABLE_SIZE 151
int hash_collisions = 0;
static int hash_collisions = 0;

struct hash_entry {
char *name;
Expand Down Expand Up @@ -693,20 +693,20 @@ void display_table(vtab_p table,
}
}

int verbose = 0;
int num_pids = 0;
int num_nodes = 0;
int screen_width = 0;
int show_zero_data = 1;
int compress_display = 0;
int sort_table = 0;
int sort_table_node = -1;
int compatibility_mode = 0;
int pid_array_max_pids = 0;
int *pid_array = NULL;
char *prog_name = NULL;
double page_size_in_bytes = 0;
double huge_page_size_in_bytes = 0;
static int verbose = 0;
static int num_pids = 0;
static int num_nodes = 0;
static int screen_width = 0;
static int show_zero_data = 1;
static int compress_display = 0;
static int sort_table = 0;
static int sort_table_node = -1;
static int compatibility_mode = 0;
static int pid_array_max_pids = 0;
static int *pid_array = NULL;
static char *prog_name = NULL;
static double page_size_in_bytes = 0;
static double huge_page_size_in_bytes = 0;

void display_version_and_exit(void)
{
Expand Down

0 comments on commit e2d6083

Please sign in to comment.