Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
sasha0552 authored Aug 18, 2024
1 parent 8a96dbc commit afcd16d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
23 changes: 6 additions & 17 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,14 @@ int main(int argc, char *argv[]) {

/***** OPTION PARSING *****/
{
// Iterate through command-line arguments
for (unsigned int i = 1; i < argc; i++) {
// Check if the option is "-ibs" or "--iterations-before-switch" and if there is a next argument
if ((IS_OPTION("-ibs") || IS_OPTION("--iterations-before-switch")) && HAS_NEXT_ARG) {
// Parse the integer option and store it in iterations_before_switch
if (!parse_uint_option(argv[++i], &iterations_before_switch)) {
goto usage;
}

// Continue to the next argument
continue;
}

// Check if the option is "-psh" or "--performance-state-high" and if there is a next argument
Expand All @@ -135,9 +133,6 @@ int main(int argc, char *argv[]) {
if (!parse_uint_option(argv[++i], &performance_state_high)) {
goto usage;
}

// Continue to the next argument
continue;
}

// Check if the option is "-psl" or "--performance-state-low" and if there is a next argument
Expand All @@ -146,9 +141,6 @@ int main(int argc, char *argv[]) {
if (!parse_uint_option(argv[++i], &performance_state_low)) {
goto usage;
}

// Continue to the next argument
continue;
}

// Check if the option is "-si" or "--sleep-interval" and if there is a next argument
Expand All @@ -157,9 +149,6 @@ int main(int argc, char *argv[]) {
if (!parse_uint_option(argv[++i], &sleep_interval)) {
goto usage;
}

// Continue to the next argument
continue;
}

// Check if the option is "-tt" or "--temperature-threshold" and if there is a next argument
Expand All @@ -168,15 +157,15 @@ int main(int argc, char *argv[]) {
if (!parse_uint_option(argv[++i], &temperature_threshold)) {
goto usage;
}

// Continue to the next argument
continue;
}
}

// If an invalid option is encountered, print the usage instructions
// Display usage instructions to the user
if (false) {
// Display usage instructions to the user
usage:

// If an invalid option is provided, print the usage instructions
// Print the usage instructions
printf("Usage: %s [options]\n", argv[0]);
printf("\n");
printf("Options:\n");
Expand Down
64 changes: 32 additions & 32 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@
#define HAS_NEXT_ARG (i + 1 < argc)

// Macro to simplify NVAPI function calls and handle errors
#define NVAPI_CALL(call, label) do { \
/* Evaluate the NVAPI function call and store the result */ \
NvAPI_Status result = (call); \
\
/* Check if the result indicates an error */ \
if (result != NVAPI_OK) { \
/* Prepare a buffer to hold the error message */ \
NvAPI_ShortString error; \
\
/* Retrieve the error message associated with the result code */ \
if (NvAPI_GetErrorMessage(result, error) != NVAPI_OK) { \
strcpy(error, "<NvAPI_GetErrorMessage() call failed>"); \
} \
\
/* Print the error message to standard error */ \
fprintf(stderr, "%s: %s\n", #call, error); \
\
/* Jump to the specified label */ \
goto label; \
} \
#define NVAPI_CALL(call, label) do { \
/* Evaluate the NVAPI function call and store the result */ \
NvAPI_Status result = (call); \
\
/* Check if the result indicates an error */ \
if (result != NVAPI_OK) { \
/* Prepare a buffer to hold the error message */ \
NvAPI_ShortString error; \
\
/* Retrieve the error message associated with the result code */ \
if (NvAPI_GetErrorMessage(result, error) != NVAPI_OK) { \
strcpy(error, "<NvAPI_GetErrorMessage() call failed>"); \
} \
\
/* Print the error message to standard error */ \
fprintf(stderr, "%s: %s\n", #call, error); \
\
/* Jump to the specified label */ \
goto label; \
} \
} while(0)

// Macro to simplify NVML function calls and handle errors
#define NVML_CALL(call, label) do { \
/* Evaluate the NVML function call and store the result */ \
nvmlReturn_t result = (call); \
\
/* Check if the result indicates an error */ \
if (result != NVML_SUCCESS) { \
/* Print the error message to standard error */ \
fprintf(stderr, "%s: %s\n", #call, nvmlErrorString(result)); \
\
/* Jump to the specified label */ \
goto label; \
} \
#define NVML_CALL(call, label) do { \
/* Evaluate the NVML function call and store the result */ \
nvmlReturn_t result = (call); \
\
/* Check if the result indicates an error */ \
if (result != NVML_SUCCESS) { \
/* Print the error message to standard error */ \
fprintf(stderr, "%s: %s\n", #call, nvmlErrorString(result)); \
\
/* Jump to the specified label */ \
goto label; \
} \
} while(0)

/***** ***** ***** ***** ***** STRUCTURES ***** ***** ***** ***** *****/
Expand Down

0 comments on commit afcd16d

Please sign in to comment.