Skip to content

Commit

Permalink
Remove typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsunyan committed Jul 6, 2019
1 parent 63a0db6 commit cee8ba4
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 172 deletions.
38 changes: 19 additions & 19 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
#include <unistd.h>

static void undervolt_free(void * pointer) {
undervolt_t * undervolt = pointer;
struct undervolt_t * undervolt = pointer;
free(undervolt->title);
}

static void hwp_power_term_free(void * pointer) {
hwp_power_term_t * hwp_power_term = pointer;
struct hwp_power_term_t * hwp_power_term = pointer;
free(hwp_power_term->domain);
}

static void hwp_hint_free(void * pointer) {
hwp_hint_t * hwp_hint = pointer;
struct hwp_hint_t * hwp_hint = pointer;
if (hwp_hint->hwp_power_terms) {
array_free(hwp_hint->hwp_power_terms);
}
free(hwp_hint->load_hint);
free(hwp_hint->normal_hint);
}

void free_config(config_t * config) {
void free_config(struct config_t * config) {
if (config) {
unsigned int i;
if (config->undervolts) {
Expand All @@ -52,7 +52,7 @@ void free_config(config_t * config) {
}

static bool parse_power_limit_value(const char * line,
power_limit_value_t * value) {
struct power_limit_value_t * value) {
char * tmp = NULL;
char * next = NULL;
int power = (int) strtol(line, &tmp, 10);
Expand Down Expand Up @@ -146,15 +146,15 @@ static bool parse_hwp_load(const char * line, bool * multi, float * threshold,
}
}

static bool parse_hwp_power(const char * line, array_t ** hwp_power_terms,
static bool parse_hwp_power(const char * line, struct array_t ** hwp_power_terms,
bool * nl, bool * nll) {
int args = 1;
bool error = false;
bool and = false;
char * domain = NULL;
bool greater;
float power;
array_t * result = NULL;
struct array_t * result = NULL;

while (line) {
int len;
Expand Down Expand Up @@ -198,7 +198,7 @@ static bool parse_hwp_power(const char * line, array_t ** hwp_power_terms,
break;
}
} else if (args % 4 == 3) {
hwp_power_term_t * hwp_power_term;
struct hwp_power_term_t * hwp_power_term;
power = strtof(line, &tmp);
if (tmp) {
int tmp_len = (int) (tmp - line);
Expand All @@ -210,7 +210,7 @@ static bool parse_hwp_power(const char * line, array_t ** hwp_power_terms,
}
}
if (!result) {
result = array_new(sizeof(hwp_power_term_t),
result = array_new(sizeof(struct hwp_power_term_t),
hwp_power_term_free);
}
hwp_power_term = result ? array_add(result) : NULL;
Expand Down Expand Up @@ -266,13 +266,13 @@ static bool contains_hint_or_append(const char ** hints, int count,
return false;
}

static bool validate_hwp_hint(array_t * hwp_hints, bool * nl, bool * nll) {
static bool validate_hwp_hint(struct array_t * hwp_hints, bool * nl, bool * nll) {
int i;
int force_count = 0;
const char * hints[2 * hwp_hints->count];

for (i = 0; i < hwp_hints->count; i++) {
hwp_hint_t * hwp_hint = array_get(hwp_hints, i);
struct hwp_hint_t * hwp_hint = array_get(hwp_hints, i);
if (contains_hint_or_append(hints, 2 * i, hwp_hint->load_hint) ||
contains_hint_or_append(hints, 2 * i + 1, hwp_hint->normal_hint)) {
NEW_LINE(nl, *nll);
Expand All @@ -299,10 +299,10 @@ static bool validate_hwp_hint(array_t * hwp_hints, bool * nl, bool * nll) {
return true;
}

config_t * load_config(config_t * old_config, bool * nl) {
struct config_t * load_config(struct config_t * old_config, bool * nl) {
unsigned int i;
bool nll = false;
config_t * config;
struct config_t * config;
if (old_config) {
config = old_config;
if (config->undervolts) {
Expand All @@ -312,7 +312,7 @@ config_t * load_config(config_t * old_config, bool * nl) {
array_free(config->hwp_hints);
}
} else {
config = malloc(sizeof(config_t));
config = malloc(sizeof(struct config_t));
if (!config) {
NEW_LINE(nl, nll);
perror("No enough memory");
Expand Down Expand Up @@ -390,7 +390,7 @@ config_t * load_config(config_t * old_config, bool * nl) {
int len;
char * title;
float value;
undervolt_t * undervolt;
struct undervolt_t * undervolt;
iuv_read_line_error();
tmp = NULL;
index = (int) strtol(line, &tmp, 10);
Expand All @@ -414,7 +414,7 @@ config_t * load_config(config_t * old_config, bool * nl) {
iuv_print_break("Invalid value: %s\n", line);
}
if (!config->undervolts) {
config->undervolts = array_new(sizeof(undervolt_t),
config->undervolts = array_new(sizeof(struct undervolt_t),
undervolt_free);
if (!config->undervolts) {
free(title);
Expand Down Expand Up @@ -478,10 +478,10 @@ config_t * load_config(config_t * old_config, bool * nl) {
bool load_multi;
float load_threshold;
bool power = false;
array_t * hwp_power_terms = NULL;
struct array_t * hwp_power_terms = NULL;
char * load_hint;
char * normal_hint;
hwp_hint_t * hwp_hint;
struct hwp_hint_t * hwp_hint;
iuv_read_line_error();
if (!strcmp(line, "force")) {
force = true;
Expand Down Expand Up @@ -529,7 +529,7 @@ config_t * load_config(config_t * old_config, bool * nl) {
}
memcpy(normal_hint, line, len + 1);
if (!config->hwp_hints) {
config->hwp_hints = array_new(sizeof(hwp_hint_t),
config->hwp_hints = array_new(sizeof(struct hwp_hint_t),
hwp_hint_free);
if (!config->hwp_hints) {
free(load_hint);
Expand Down
46 changes: 23 additions & 23 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,65 @@
#define MSR_ADDR_UNITS 0x606
#define MSR_ADDR_VOLTAGE 0x150

typedef struct {
struct undervolt_t {
int index;
char * title;
float value;
} undervolt_t;
};

typedef struct {
struct power_domain_t {
const char * name;
size_t mem_addr;
int msr_addr;
} power_domain_t;
};

static power_domain_t power_domains[1] = {
static struct power_domain_t power_domains[1] = {
{ "package", 0xfed159a0, 0x610 }
};

typedef struct {
struct power_limit_value_t {
int power;
float time_window;
bool enabled;
} power_limit_value_t;
};

typedef struct {
struct power_limit_t {
bool apply;
power_limit_value_t short_term;
power_limit_value_t long_term;
struct power_limit_value_t short_term;
struct power_limit_value_t long_term;
void * mem;
} power_limit_t;
};

typedef struct {
struct hwp_power_term_t {
bool and;
char * domain;
bool greater;
double power;
} hwp_power_term_t;
};

typedef struct {
struct hwp_hint_t {
bool force;
bool load;
bool load_multi;
float load_threshold;
bool power;
array_t * hwp_power_terms;
struct array_t * hwp_power_terms;
char * load_hint;
char * normal_hint;
} hwp_hint_t;
};

typedef struct {
struct config_t {
int fd_msr;
int fd_mem;
array_t * undervolts;
power_limit_t power[ARRAY_SIZE(power_domains)];
struct array_t * undervolts;
struct power_limit_t power[ARRAY_SIZE(power_domains)];
bool tjoffset_apply;
float tjoffset;
array_t * hwp_hints;
struct array_t * hwp_hints;
int interval;
} config_t;
};

void free_config(config_t * config);
config_t * load_config(config_t * old_config, bool * nl);
void free_config(struct config_t * config);
struct config_t * load_config(struct config_t * old_config, bool * nl);

#endif
20 changes: 9 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,22 @@ enum arg_mode {
ARG_MODE_FLOAT
};

typedef struct arg_t arg_t;

typedef struct arg_t {
struct arg_t {
char short_name;
const char * long_name;
enum arg_mode mode;
bool (* check)(arg_t *);
bool (* check)(struct arg_t *);
bool present;
const char * value;
float float_value;
} arg_t;
};

#define ARG_END { '\0', NULL, ARG_MODE_END, NULL, false, NULL, 0 }
#define ARG_EMPTY(s, l, c) { s, l, ARG_MODE_EMPTY, c, false, NULL, 0 }
#define ARG_STRING(s, l, c, v) { s, l, ARG_MODE_STRING, c, false, v, 0 }
#define ARG_FLOAT(s, l, c, v) { s, l, ARG_MODE_FLOAT, c, false, #v, v }

static bool handle_arg(arg_t * arg, const char * value, bool * consume_next) {
static bool handle_arg(struct arg_t * arg, const char * value, bool * consume_next) {
arg->present = true;
if (arg->mode == ARG_MODE_END || arg->mode == ARG_MODE_EMPTY) {
*consume_next = false;
Expand Down Expand Up @@ -63,7 +61,7 @@ static bool handle_arg(arg_t * arg, const char * value, bool * consume_next) {
return !arg->check || arg->check(arg);
}

static bool parse_args(int argc, char ** argv, arg_t * args) {
static bool parse_args(int argc, char ** argv, struct arg_t * args) {
int i, j, k;
bool consume_next;
for (i = 0; i < argc; i++) {
Expand Down Expand Up @@ -142,7 +140,7 @@ static bool parse_args(int argc, char ** argv, arg_t * args) {
return true;
}

static arg_t * arg(arg_t * args, const char * name) {
static struct arg_t * arg(struct arg_t * args, const char * name) {
int i;
bool s = strlen(name) == 1;
for (i = 0; args && args[i].mode != ARG_MODE_END; i++) {
Expand All @@ -154,15 +152,15 @@ static arg_t * arg(arg_t * args, const char * name) {
return NULL;
}

static bool arg_check_measure_format(arg_t * arg) {
static bool arg_check_measure_format(struct arg_t * arg) {
if (strcmp(arg->value, "terminal") && strcmp(arg->value, "csv")) {
fprintf(stderr, "Available formats: terminal, csv.\n");
return false;
}
return true;
}

static bool arg_check_measure_sleep(arg_t * arg) {
static bool arg_check_measure_sleep(struct arg_t * arg) {
if (arg->float_value <= 0) {
fprintf(stderr, "Sleep interval should be greater than 0.\n");
return false;
Expand All @@ -178,7 +176,7 @@ int main(int argc, char ** argv) {
return parse_args(argc - 2, &argv[2], NULL) &&
read_apply_mode(true) ? 0 : 1;
} else if (argc >= 2 && !strcmp(argv[1], "measure")) {
arg_t args[3] = {
struct arg_t args[3] = {
ARG_STRING('f', "format", arg_check_measure_format, "terminal"),
ARG_FLOAT('s', "sleep", arg_check_measure_sleep, 1),
ARG_END
Expand Down
Loading

0 comments on commit cee8ba4

Please sign in to comment.