From 8d4f8d7eb7ec9c72a27b2600e41dd68cb9f6a6ec Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Wed, 18 Aug 2010 19:31:13 +0300 Subject: [PATCH] Add macro for testing parse option flags The parse option structure parse_mask variable is tested for option bits in quite many places. Add a macro that makes testing easier and moves the duplicate code in one macro. Signed-off-by: Timo Kokkonen --- pagemap.h | 3 +++ parse.c | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pagemap.h b/pagemap.h index d7dacdf..5a58c83 100644 --- a/pagemap.h +++ b/pagemap.h @@ -105,4 +105,7 @@ struct parse_opts { int with_threads; }; +#define is_parse_option(parse_opts, flag) \ + (!!((parse_opts)->parse_mask & (flag))) + #endif diff --git a/parse.c b/parse.c index fc7d69e..d4cae14 100644 --- a/parse.c +++ b/parse.c @@ -154,21 +154,21 @@ static int should_scan_process(struct parse_opts *opts, struct process *process) int match = 0; char *name; - if (opts->parse_mask & PARSE_PROCESS_NAME) { + if (is_parse_option(opts, PARSE_PROCESS_NAME)) { name = get_name_by_pid(process->pid); if (!strcmp(opts->name, name ? name : "")) match = 1; } - if (opts->parse_mask & PARSE_PID) { + if (is_parse_option(opts, PARSE_PID)) { if (opts->pid == process->pid) match = 1; } - if (opts->parse_mask & PARSE_MAP_NAME) + if (is_parse_option(opts, PARSE_MAP_NAME)) match = 1; - if (opts->parse_mask & PARSE_NOADD_TREE) + if (is_parse_option(opts, PARSE_NOADD_TREE)) match = !match; return match; @@ -178,11 +178,11 @@ static int should_scan_mapping(struct parse_opts *opts, struct maps *map) { int match = 0; - if (opts->parse_mask & PARSE_MAP_NAME) { + if (is_parse_option(opts, PARSE_MAP_NAME)) { if (!strcmp(opts->name, map->name)) match = 1; - if (opts->parse_mask & PARSE_NOADD_TREE) + if (is_parse_option(opts, PARSE_NOADD_TREE)) match = !match; } else match = 1; @@ -193,7 +193,7 @@ static int should_scan_mapping(struct parse_opts *opts, struct maps *map) static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf, struct maps *map) { - if (opts->parse_mask & PARSE_NOADD_TREE) + if (is_parse_option(opts, PARSE_NOADD_TREE)) return 0; return 1; @@ -468,7 +468,7 @@ int scan_all_pids(struct pageframe *pf, struct process *process_list, int pid; int count = 0; - if (opts->parse_mask & PARSE_PROCESS_NAME) { + if (is_parse_option(opts, PARSE_PROCESS_NAME)) { while ((pid = get_next_pid_by_name(&dir, opts->name))) { count += read_pageframe_with_threads(pid, pf, process_list, @@ -477,20 +477,20 @@ int scan_all_pids(struct pageframe *pf, struct process *process_list, dir = NULL; } - if (opts->parse_mask & PARSE_PID) + if (is_parse_option(opts, PARSE_PID)) count = read_pageframe_with_threads(opts->pid, pf, process_list, opts); - if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) { + if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) { printf("Failed to find any matching processes " "with given arguments\n"); return -1; } - if (opts->parse_mask & PARSE_DUMP) + if (is_parse_option(opts, PARSE_DUMP)) return 0; - if (opts->parse_mask & PARSE_MAP_NAME) { + if (is_parse_option(opts, PARSE_MAP_NAME)) { while (1) { pid = get_next_pid(&dir); if (pid <= 0) -- 2.44.0