diff --git a/.clang-format b/.clang-format index f94fb59d..f2fb2ddd 100644 --- a/.clang-format +++ b/.clang-format @@ -19,3 +19,7 @@ SpaceAfterCStyleCast: false SpacesInParentheses: false SpacesInContainerLiterals: false BreakBeforeBinaryOperators: NonAssignment +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 877ec71e..e020549f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,22 +92,22 @@ else() endif() if ( MSVC ) - MESSAGE ( STATUS "Setting MSVC MT switches") - string (REPLACE + MESSAGE ( STATUS "Setting MSVC MT switches") + string (REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG} - ) - string (REPLACE + ) + string (REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE} - ) + ) elseif ( WIN32 ) - set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mconsole") - set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mwindows") + set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mconsole") + set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mwindows") elseif ( GCC AND NOT MINGW ) set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer") set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") diff --git a/lib/profiler/.clang-format b/lib/profiler/.clang-format new file mode 100644 index 00000000..f2fb2ddd --- /dev/null +++ b/lib/profiler/.clang-format @@ -0,0 +1,25 @@ +--- +BasedOnStyle: LLVM +IndentWidth: 8 +TabWidth: 8 +UseTab: ForIndentation +BreakBeforeBraces: Linux +ColumnLimit: 120 +PointerAlignment: Right +IndentCaseLabels: true +SpaceBeforeParens: ControlStatements +AlignAfterOpenBracket: Align +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: AllDefinitions +SortIncludes: Never +IndentPPDirectives: None +SpaceAfterCStyleCast: false +SpacesInParentheses: false +SpacesInContainerLiterals: false +BreakBeforeBinaryOperators: NonAssignment +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false diff --git a/lib/profiler/include/internal/common.h b/lib/profiler/include/internal/common.h index 97f59399..72cee34a 100644 --- a/lib/profiler/include/internal/common.h +++ b/lib/profiler/include/internal/common.h @@ -1,14 +1,13 @@ #ifndef COMMON_H_ #define COMMON_H_ -#define C_RED "\x1b[31m" -#define C_GREEN "\x1b[32m" -#define C_YELLOW "\x1b[33m" -#define C_BLUE "\x1b[34m" -#define C_MAGENTA "\x1b[35m" -#define C_CYAN "\x1b[36m" -#define C_WHITE "\x1b[37m" -#define C_RESET "\x1b[0m" - -#endif // COMMON_H_ +#define C_RED "\x1b[31m" +#define C_GREEN "\x1b[32m" +#define C_YELLOW "\x1b[33m" +#define C_BLUE "\x1b[34m" +#define C_MAGENTA "\x1b[35m" +#define C_CYAN "\x1b[36m" +#define C_WHITE "\x1b[37m" +#define C_RESET "\x1b[0m" +#endif // COMMON_H_ diff --git a/lib/profiler/include/internal/profiler_c.h b/lib/profiler/include/internal/profiler_c.h index 725019d2..33ba2fcf 100644 --- a/lib/profiler/include/internal/profiler_c.h +++ b/lib/profiler/include/internal/profiler_c.h @@ -8,25 +8,25 @@ #define ANCHOR_CAPACITY 4096 typedef struct ProfileAnchor { - const char *label; - size_t hits; - uint64_t elapsed_exclusive; - uint64_t elapsed_inclusive; - size_t processed_byte_count; + const char *label; + size_t hits; + uint64_t elapsed_exclusive; + uint64_t elapsed_inclusive; + size_t processed_byte_count; } ProfileAnchor; typedef struct Profiler { - uint64_t start, stop; - size_t len; - ProfileAnchor anchors[ANCHOR_CAPACITY]; + uint64_t start, stop; + size_t len; + ProfileAnchor anchors[ANCHOR_CAPACITY]; } Profiler; typedef struct AnchorBlock { - const char *label; - uint64_t start; - uint64_t old_elapsed_inclusive; - size_t processed_byte_count; - char *parent_anchor; + const char *label; + uint64_t start; + uint64_t old_elapsed_inclusive; + size_t processed_byte_count; + char *parent_anchor; } AnchorBlock; void prof_init(void); @@ -43,4 +43,3 @@ AnchorBlock make_anchor_block(const char *label, size_t used_bytes); void read_anchor_block(const AnchorBlock *anchor); #endif // _STOP_CLOCK_H_ - diff --git a/lib/profiler/include/macros.h b/lib/profiler/include/macros.h index 9767ab18..a6a8f0fe 100644 --- a/lib/profiler/include/macros.h +++ b/lib/profiler/include/macros.h @@ -13,22 +13,22 @@ prof_print(fp); #define _NameConcat(A, B) A##B -#define NameConcat(A, B) _NameConcat(A, B) +#define NameConcat(A, B) _NameConcat(A, B) #define TIME_BANDWIDTH_BEGIN(label, bytes) AnchorBlock NameConcat(Block, label) = make_anchor_block(#label, bytes) -#define TIME_BANDWIDTH_END(label) read_anchor_block(&Block##label) +#define TIME_BANDWIDTH_END(label) read_anchor_block(&Block##label) #define TIME_BANDWIDTH(label, bytes) \ AnchorBlock NameConcat(Block, label) __attribute__((cleanup(read_anchor_block))); \ NameConcat(Block, label) = make_anchor_block(#label, bytes) #define TIME_BLOCK_BEGIN(label) TIME_BANDWIDTH_BEGIN(label, 0) -#define TIME_BLOCK_END(label) TIME_BANDWIDTH_END(label) +#define TIME_BLOCK_END(label) TIME_BANDWIDTH_END(label) #define TIME_BLOCK(label) \ AnchorBlock NameConcat(Block, label) __attribute__((cleanup(read_anchor_block))); \ NameConcat(Block, label) = make_anchor_block(#label, 0) #define TIME_FUNC_BEGIN() AnchorBlock NameConcat(Block, __func__) = make_anchor_block(__func__, 0) -#define TIME_FUNC_END() read_anchor_block(&Block##__func__) +#define TIME_FUNC_END() read_anchor_block(&Block##__func__) #define TIME_FUNC() \ AnchorBlock NameConcat(Block, __func__) __attribute__((cleanup(read_anchor_block))); \ NameConcat(Block, __func__) = make_anchor_block(__func__, 0) diff --git a/lib/profiler/include/perf.h b/lib/profiler/include/perf.h index bd60c2c3..d13e76f2 100644 --- a/lib/profiler/include/perf.h +++ b/lib/profiler/include/perf.h @@ -11,5 +11,4 @@ uint64_t perf_read_page_fault_count(void); void perf_close(void); -#endif // _PERF_H_ - +#endif // _PERF_H_ diff --git a/lib/profiler/include/profiler.h b/lib/profiler/include/profiler.h index 096abbe7..d01d4f9e 100644 --- a/lib/profiler/include/profiler.h +++ b/lib/profiler/include/profiler.h @@ -19,4 +19,3 @@ extern "C" { #endif #endif // _PROFILER_H_ - diff --git a/lib/profiler/include/rdtsc.h b/lib/profiler/include/rdtsc.h index 11669196..51ca5aaa 100644 --- a/lib/profiler/include/rdtsc.h +++ b/lib/profiler/include/rdtsc.h @@ -17,4 +17,3 @@ uint64_t estimate_cpu_freq(uint64_t test_time); #endif #endif // _RDTSC_H_ - diff --git a/lib/profiler/include/repetition_tester.h b/lib/profiler/include/repetition_tester.h index 4211a118..80cc1ef1 100644 --- a/lib/profiler/include/repetition_tester.h +++ b/lib/profiler/include/repetition_tester.h @@ -5,54 +5,48 @@ #include #include -#define rept_error(tester, msg) \ - do { \ - fprintf(stderr, "%s()[%d] %s\n", __func__, __LINE__, msg); \ - tester->mode = TestMode_Error; \ - } while (0) - -typedef enum TestMode { - TestMode_Uninitialized, - TestMode_Testing, - TestMode_Completed, - TestMode_Error -} TestMode; +#define rept_error(tester, msg) \ + do { \ + fprintf(stderr, "%s()[%d] %s\n", __func__, __LINE__, msg); \ + tester->mode = TestMode_Error; \ + } while (0) + +typedef enum TestMode { TestMode_Uninitialized, TestMode_Testing, TestMode_Completed, TestMode_Error } TestMode; typedef enum RepetitionValueType { - RepVal_test_count, - RepVal_page_faults, - RepVal_byte_count, - RepVal_cpu_time, - RepVal_count + RepVal_test_count, + RepVal_page_faults, + RepVal_byte_count, + RepVal_cpu_time, + RepVal_count } RepetitionValueType; typedef struct RepetitionValue { - uint64_t E[RepVal_count]; + uint64_t E[RepVal_count]; } RepetitionValue; typedef struct RepetitionTestResult { - RepetitionValue total; - RepetitionValue min; - RepetitionValue max; + RepetitionValue total; + RepetitionValue min; + RepetitionValue max; } RepetitionTestResult; typedef struct RepetitionTester { - uint64_t target_processed_byte_count; - uint64_t cpu_timer_freq; - uint64_t test_time; - uint64_t start_time; - - TestMode mode; - bool print_new_mins; - uint32_t open_block_count; - uint32_t close_block_count; - - RepetitionValue current; - RepetitionTestResult results; + uint64_t target_processed_byte_count; + uint64_t cpu_timer_freq; + uint64_t test_time; + uint64_t start_time; + + TestMode mode; + bool print_new_mins; + uint32_t open_block_count; + uint32_t close_block_count; + + RepetitionValue current; + RepetitionTestResult results; } RepetitionTester; -void rept_setup(RepetitionTester *tester, uint64_t target_bytes, - uint64_t cpu_timer_freq, uint32_t test_time); +void rept_setup(RepetitionTester *tester, uint64_t target_bytes, uint64_t cpu_timer_freq, uint32_t test_time); void rept_begin(RepetitionTester *tester); @@ -64,5 +58,4 @@ bool rept_is_testing(RepetitionTester *tester); void rept_print_results(RepetitionTester *tester, FILE *fp); -#endif // REPETITION_TESTER_H_ - +#endif // REPETITION_TESTER_H_ diff --git a/lib/profiler/src/calc_cpu_freq.c b/lib/profiler/src/calc_cpu_freq.c index 74d2a259..f7263e36 100644 --- a/lib/profiler/src/calc_cpu_freq.c +++ b/lib/profiler/src/calc_cpu_freq.c @@ -5,27 +5,28 @@ typedef double f64; typedef uint64_t u64; -int main(void) +int +main(void) { - u64 os_freq = get_os_time_freq(); - u64 cpu_start = read_cpu_timer(); - u64 os_start = read_os_timer(); - u64 os_elapsed = 0; - u64 os_wait_time = os_freq; - while (os_elapsed < os_wait_time) { - u64 os_end = read_os_timer(); - os_elapsed = os_end - os_start; - } + u64 os_freq = get_os_time_freq(); + u64 cpu_start = read_cpu_timer(); + u64 os_start = read_os_timer(); + u64 os_elapsed = 0; + u64 os_wait_time = os_freq; + while (os_elapsed < os_wait_time) { + u64 os_end = read_os_timer(); + os_elapsed = os_end - os_start; + } - u64 cpu_end = read_cpu_timer(); - u64 cpu_elapsed = cpu_end - cpu_start; - u64 cpu_freq = 0; - if (os_elapsed) { - cpu_freq = (u64)((double) os_freq * cpu_elapsed / os_elapsed); - } + u64 cpu_end = read_cpu_timer(); + u64 cpu_elapsed = cpu_end - cpu_start; + u64 cpu_freq = 0; + if (os_elapsed) { + cpu_freq = (u64)((double)os_freq * cpu_elapsed / os_elapsed); + } - printf(" OS Freq: %lu (reported)\n", os_freq); - printf(" OS Seconds: %.4f\n", (f64) os_elapsed/ (f64) os_freq); - printf(" CPU Timer: %lu -> %lu = %lu\n", cpu_start, cpu_end, cpu_elapsed); - printf(" CPU Freq: %lu (guessed)\n", cpu_freq); + printf(" OS Freq: %lu (reported)\n", os_freq); + printf(" OS Seconds: %.4f\n", (f64)os_elapsed / (f64)os_freq); + printf(" CPU Timer: %lu -> %lu = %lu\n", cpu_start, cpu_end, cpu_elapsed); + printf(" CPU Freq: %lu (guessed)\n", cpu_freq); } diff --git a/lib/profiler/src/perf.c b/lib/profiler/src/perf.c index 88bd3e8d..fc070da3 100644 --- a/lib/profiler/src/perf.c +++ b/lib/profiler/src/perf.c @@ -10,67 +10,70 @@ static int _FD = 0; -static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, - int cpu, int group_fd, unsigned long flags) +static long +perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) { - int ret; + int ret; - ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, - group_fd, flags); - return ret; + ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); + return ret; } -void perf_setup(void) +void +perf_setup(void) { - static struct perf_event_attr pe; + static struct perf_event_attr pe; - if (_FD) { - close(_FD); - _FD = 0; - } + if (_FD) { + close(_FD); + _FD = 0; + } - pe.type = PERF_TYPE_SOFTWARE; - pe.size = sizeof(pe); - pe.config = PERF_COUNT_SW_PAGE_FAULTS; - pe.disabled = 1; - pe.exclude_kernel = 1; - pe.exclude_hv = 1; + pe.type = PERF_TYPE_SOFTWARE; + pe.size = sizeof(pe); + pe.config = PERF_COUNT_SW_PAGE_FAULTS; + pe.disabled = 1; + pe.exclude_kernel = 1; + pe.exclude_hv = 1; - _FD = perf_event_open(&pe, 0, -1, -1, 0); - if (_FD == -1) { - fprintf(stderr, "Error opening leader %llx : %s\n", pe.config, strerror(errno)); - fprintf(stderr, "Page errors will not be counted\n"); - _FD = 0; - return; - } + _FD = perf_event_open(&pe, 0, -1, -1, 0); + if (_FD == -1) { + fprintf(stderr, "Error opening leader %llx : %s\n", pe.config, strerror(errno)); + fprintf(stderr, "Page errors will not be counted\n"); + _FD = 0; + return; + } - ioctl(_FD, PERF_EVENT_IOC_RESET, 0); - ioctl(_FD, PERF_EVENT_IOC_ENABLE); + ioctl(_FD, PERF_EVENT_IOC_RESET, 0); + ioctl(_FD, PERF_EVENT_IOC_ENABLE); } -void perf_reset_page_fault_count(void) +void +perf_reset_page_fault_count(void) { - if (_FD) { - ioctl(_FD, PERF_EVENT_IOC_RESET, 0); - ioctl(_FD, PERF_EVENT_IOC_ENABLE); - } + if (_FD) { + ioctl(_FD, PERF_EVENT_IOC_RESET, 0); + ioctl(_FD, PERF_EVENT_IOC_ENABLE); + } } -uint64_t perf_read_page_fault_count(void) +uint64_t +perf_read_page_fault_count(void) { - size_t count = 0; - if (_FD) { - ssize_t bytes = read(_FD, &count, sizeof(count)); - if (bytes != sizeof(count)) { - count = 0; + size_t count = 0; + if (_FD) { + ssize_t bytes = read(_FD, &count, sizeof(count)); + if (bytes != sizeof(count)) { + count = 0; + } } - } - return count; + return count; } -void perf_close(void) +void +perf_close(void) { - if (_FD) { - close(_FD); - } + if (_FD) { + close(_FD); + } } diff --git a/lib/profiler/src/profiler.c b/lib/profiler/src/profiler.c index b00ec650..58ec3f52 100644 --- a/lib/profiler/src/profiler.c +++ b/lib/profiler/src/profiler.c @@ -9,7 +9,7 @@ #include #define FNV_OFFSET 14695981039346656037UL -#define FNV_PRIME 1099511628211UL +#define FNV_PRIME 1099511628211UL static Profiler _Prof; static char *CurrentAnchorLabel = NULL; diff --git a/lib/profiler/src/rdtsc.c b/lib/profiler/src/rdtsc.c index 22c68d27..deedba9e 100644 --- a/lib/profiler/src/rdtsc.c +++ b/lib/profiler/src/rdtsc.c @@ -4,52 +4,57 @@ #include "rdtsc.h" #ifdef __arm__ -static uint64_t rdtsc(void) +static uint64_t +rdtsc(void) { - uint64_t val; - asm volatile("mrs %0, cntvct_el0" : "=r" (val)); - return val; + uint64_t val; + asm volatile("mrs %0, cntvct_el0" : "=r"(val)); + return val; } #else #include #define rdtsc() __rdtsc() #endif -uint64_t get_os_time_freq(void) +uint64_t +get_os_time_freq(void) { - return 1000000; + return 1000000; } -uint64_t read_os_timer(void) +uint64_t +read_os_timer(void) { - struct timeval tv; - gettimeofday(&tv, NULL); - return get_os_time_freq()*tv.tv_sec + tv.tv_usec; + struct timeval tv; + gettimeofday(&tv, NULL); + return get_os_time_freq() * tv.tv_sec + tv.tv_usec; } -inline uint64_t read_cpu_timer(void) +inline uint64_t +read_cpu_timer(void) { - return rdtsc(); + return rdtsc(); } -uint64_t estimate_cpu_freq(uint64_t test_time) +uint64_t +estimate_cpu_freq(uint64_t test_time) { - uint64_t os_freq = get_os_time_freq(); - uint64_t cpu_start = read_cpu_timer(); - uint64_t os_start = read_os_timer(); - uint64_t os_elapsed = 0; - uint64_t os_wait_time = os_freq * test_time / 1000; - while (os_elapsed < os_wait_time) { - uint64_t os_end = read_os_timer(); - os_elapsed = os_end - os_start; - } + uint64_t os_freq = get_os_time_freq(); + uint64_t cpu_start = read_cpu_timer(); + uint64_t os_start = read_os_timer(); + uint64_t os_elapsed = 0; + uint64_t os_wait_time = os_freq * test_time / 1000; + while (os_elapsed < os_wait_time) { + uint64_t os_end = read_os_timer(); + os_elapsed = os_end - os_start; + } - uint64_t cpu_end = read_cpu_timer(); - uint64_t cpu_elapsed = cpu_end - cpu_start; - uint64_t cpu_freq = 0; - if (os_elapsed) { - cpu_freq = os_freq * cpu_elapsed / os_elapsed; - } + uint64_t cpu_end = read_cpu_timer(); + uint64_t cpu_elapsed = cpu_end - cpu_start; + uint64_t cpu_freq = 0; + if (os_elapsed) { + cpu_freq = os_freq * cpu_elapsed / os_elapsed; + } - return cpu_freq; + return cpu_freq; } diff --git a/lib/profiler/src/rep_read_test.c b/lib/profiler/src/rep_read_test.c index 18acb6c9..ec12c1fa 100644 --- a/lib/profiler/src/rep_read_test.c +++ b/lib/profiler/src/rep_read_test.c @@ -10,172 +10,174 @@ #include typedef enum AllocationType { - AllocType_none, - AllocType_malloc, - AllocType_count, + AllocType_none, + AllocType_malloc, + AllocType_count, } AllocationType; typedef struct ReadParams { - const char *fname; - AllocationType alloc_type; - uint8_t *buffer; - size_t buflen; + const char *fname; + AllocationType alloc_type; + uint8_t *buffer; + size_t buflen; } ReadParams; typedef struct TestFunction { - const char *label; - void (*func)(RepetitionTester *tester, ReadParams *params); + const char *label; + void (*func)(RepetitionTester *tester, ReadParams *params); } TestFunction; -static const char *print_allocation_type(AllocationType type) +static const char * +print_allocation_type(AllocationType type) { - const char *result; - switch (type) { - case AllocType_none: - result = ""; - break; - case AllocType_malloc: - result = "malloc"; - break; - default: - result = "unknown"; - break; - } - return result; + const char *result; + switch (type) { + case AllocType_none: + result = ""; + break; + case AllocType_malloc: + result = "malloc"; + break; + default: + result = "unknown"; + break; + } + return result; } -static void handle_allocation(ReadParams *params, uint8_t **buffer) +static void +handle_allocation(ReadParams *params, uint8_t **buffer) { - switch (params->alloc_type) { - case AllocType_malloc: - *buffer = malloc(params->buflen); - break; - case AllocType_none: - break; - default: - fprintf(stderr, "ERROR: Unrecognized allocation type"); - break; - } + switch (params->alloc_type) { + case AllocType_malloc: + *buffer = malloc(params->buflen); + break; + case AllocType_none: + break; + default: + fprintf(stderr, "ERROR: Unrecognized allocation type"); + break; + } } -static void handle_deallocation(ReadParams *params, uint8_t *buffer) +static void +handle_deallocation(ReadParams *params, uint8_t *buffer) { - switch (params->alloc_type) { - case AllocType_malloc: - free(buffer); - break; - case AllocType_none: - break; - default: - fprintf(stderr, "ERROR: Unrecognized allocation type"); - break; - } + switch (params->alloc_type) { + case AllocType_malloc: + free(buffer); + break; + case AllocType_none: + break; + default: + fprintf(stderr, "ERROR: Unrecognized allocation type"); + break; + } } -static void test_fread(RepetitionTester *tester, ReadParams *params) +static void +test_fread(RepetitionTester *tester, ReadParams *params) { - while (rept_is_testing(tester)) { - FILE *fp = fopen(params->fname, "r"); - if (fp == NULL) { - rept_error(tester, "fopen failed"); - } else { - uint8_t *dest = params->buffer; - handle_allocation(params, &dest); - rept_begin(tester); - size_t result = fread(dest, sizeof(uint8_t), params->buflen, fp); - rept_end(tester); - - if (result == params->buflen) { - rept_count_bytes(tester, result); - } else { - rept_error(tester, "fread failed"); - } - handle_deallocation(params, dest); - fclose(fp); - } - } + while (rept_is_testing(tester)) { + FILE *fp = fopen(params->fname, "r"); + if (fp == NULL) { + rept_error(tester, "fopen failed"); + } else { + uint8_t *dest = params->buffer; + handle_allocation(params, &dest); + rept_begin(tester); + size_t result = fread(dest, sizeof(uint8_t), params->buflen, fp); + rept_end(tester); + + if (result == params->buflen) { + rept_count_bytes(tester, result); + } else { + rept_error(tester, "fread failed"); + } + handle_deallocation(params, dest); + fclose(fp); + } + } } -static void test_read(RepetitionTester *tester, ReadParams *params) +static void +test_read(RepetitionTester *tester, ReadParams *params) { - while (rept_is_testing(tester)) { - int fd = open(params->fname, O_RDONLY); - if (fd < 0) { - rept_error(tester, "open failed"); - } else { - ssize_t read_bytes = 0; - uint8_t *dest = params->buffer; - handle_allocation(params, &dest); - rept_begin(tester); - while (read_bytes < (ssize_t) params->buflen) { - ssize_t bytes = read(fd, dest, params->buflen - read_bytes); - rept_count_bytes(tester, bytes); - if (bytes < 0) { - rept_error(tester, "read failed"); - } - read_bytes += bytes; - } - rept_end(tester); - handle_deallocation(params, dest); - close(fd); - } - } + while (rept_is_testing(tester)) { + int fd = open(params->fname, O_RDONLY); + if (fd < 0) { + rept_error(tester, "open failed"); + } else { + ssize_t read_bytes = 0; + uint8_t *dest = params->buffer; + handle_allocation(params, &dest); + rept_begin(tester); + while (read_bytes < (ssize_t)params->buflen) { + ssize_t bytes = read(fd, dest, params->buflen - read_bytes); + rept_count_bytes(tester, bytes); + if (bytes < 0) { + rept_error(tester, "read failed"); + } + read_bytes += bytes; + } + rept_end(tester); + handle_deallocation(params, dest); + close(fd); + } + } } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - TestFunction tests[] = { - { "fread", test_fread }, - { "read", test_read } - }; - const char *fname; - size_t fsize; - - if (argc != 2) { - fprintf(stderr, "USAGE: %s \n", argv[0]); - return EXIT_FAILURE; - } - - fname = argv[1]; - - struct stat statbuf; - if (stat(fname, &statbuf) != 0) { - perror("stat"); - return EXIT_FAILURE; - } - fsize = statbuf.st_size; - - const size_t test_count = sizeof(tests)/sizeof(TestFunction); - RepetitionTester testers[sizeof(tests)/sizeof(TestFunction)][AllocType_count] = {0}; - ReadParams params = { - .buffer = malloc(fsize * sizeof(uint8_t)), - .buflen = fsize, - .fname = fname, - }; - - uint64_t cpu_freq = estimate_cpu_freq(1000); - - if (params.buflen > 0) { - //while (1) { - for (size_t i = 0; i < test_count; ++i) { - for (uint32_t alloc_type = AllocType_none; alloc_type < AllocType_count; ++alloc_type) { - RepetitionTester *tester = &testers[i][alloc_type]; - TestFunction *test_func = tests + i; - params.alloc_type = alloc_type; - - printf("\n--- %s%s%s ---\n", - print_allocation_type(alloc_type), params.alloc_type - ? " + " : "", test_func->label); - - rept_setup(tester, fsize, cpu_freq, 10); - test_func->func(tester, ¶ms); - } - } - //} - } - - free(params.buffer); - - return EXIT_SUCCESS; + TestFunction tests[] = {{"fread", test_fread}, {"read", test_read}}; + const char *fname; + size_t fsize; + + if (argc != 2) { + fprintf(stderr, "USAGE: %s \n", argv[0]); + return EXIT_FAILURE; + } + + fname = argv[1]; + + struct stat statbuf; + if (stat(fname, &statbuf) != 0) { + perror("stat"); + return EXIT_FAILURE; + } + fsize = statbuf.st_size; + + const size_t test_count = sizeof(tests) / sizeof(TestFunction); + RepetitionTester testers[sizeof(tests) / sizeof(TestFunction)][AllocType_count] = {0}; + ReadParams params = { + .buffer = malloc(fsize * sizeof(uint8_t)), + .buflen = fsize, + .fname = fname, + }; + + uint64_t cpu_freq = estimate_cpu_freq(1000); + + if (params.buflen > 0) { + // while (1) { + for (size_t i = 0; i < test_count; ++i) { + for (uint32_t alloc_type = AllocType_none; alloc_type < AllocType_count; ++alloc_type) { + RepetitionTester *tester = &testers[i][alloc_type]; + TestFunction *test_func = tests + i; + params.alloc_type = alloc_type; + + printf("\n--- %s%s%s ---\n", print_allocation_type(alloc_type), + params.alloc_type ? " + " : "", test_func->label); + + rept_setup(tester, fsize, cpu_freq, 10); + test_func->func(tester, ¶ms); + } + } + //} + } + + free(params.buffer); + + return EXIT_SUCCESS; } diff --git a/lib/profiler/src/rep_return_data.c b/lib/profiler/src/rep_return_data.c index b98f061f..c6b8f6d2 100644 --- a/lib/profiler/src/rep_return_data.c +++ b/lib/profiler/src/rep_return_data.c @@ -5,111 +5,116 @@ #include #include - typedef struct TestFunction { - const char *label; - void (*func)(RepetitionTester *tester); + const char *label; + void (*func)(RepetitionTester *tester); } TestFunction; struct ReturnData { - float fnum; - size_t count; - double dnum; - float vec3[3]; + float fnum; + size_t count; + double dnum; + float vec3[3]; }; typedef struct ReturnData ReturnData; -static ReturnData inline_create_data(void) +static ReturnData +inline_create_data(void) { - return (ReturnData) { - .fnum = 1.2, - .count = 100, - .dnum = 3.4, - .vec3 = { 1, 2, 3 }, - }; + return (ReturnData){ + .fnum = 1.2, + .count = 100, + .dnum = 3.4, + .vec3 = {1, 2, 3}, + }; } -static ReturnData variable_create_data(void) +static ReturnData +variable_create_data(void) { - ReturnData data; - data.fnum = 1.2; - data.count = 100; - data.dnum = 3.4; - data.vec3[0] = 1; - data.vec3[1] = 2; - data.vec3[2] = 3; - return data; + ReturnData data; + data.fnum = 1.2; + data.count = 100; + data.dnum = 3.4; + data.vec3[0] = 1; + data.vec3[1] = 2; + data.vec3[2] = 3; + return data; } - -static ReturnData* malloc_create_data(void) +static ReturnData * +malloc_create_data(void) { - ReturnData *data = malloc(sizeof(ReturnData)); - if (!data) { - return NULL; - } - data->fnum = 1.2; - data->count = 100; - data->dnum = 3.4; - data->vec3[0] = 1; - data->vec3[1] = 2; - data->vec3[2] = 3; - return data; + ReturnData *data = malloc(sizeof(ReturnData)); + if (!data) { + return NULL; + } + data->fnum = 1.2; + data->count = 100; + data->dnum = 3.4; + data->vec3[0] = 1; + data->vec3[1] = 2; + data->vec3[2] = 3; + return data; } -static void test_inline_static_data(RepetitionTester *tester) +static void +test_inline_static_data(RepetitionTester *tester) { - while (rept_is_testing(tester)) { - rept_begin(tester); - ReturnData data = inline_create_data(); - rept_end(tester); - rept_count_bytes(tester, sizeof(data)); - } + while (rept_is_testing(tester)) { + rept_begin(tester); + ReturnData data = inline_create_data(); + rept_end(tester); + rept_count_bytes(tester, sizeof(data)); + } } -static void test_variable_static_data(RepetitionTester *tester) +static void +test_variable_static_data(RepetitionTester *tester) { - while (rept_is_testing(tester)) { - rept_begin(tester); - ReturnData data = variable_create_data(); - rept_end(tester); - rept_count_bytes(tester, sizeof(data)); - } + while (rept_is_testing(tester)) { + rept_begin(tester); + ReturnData data = variable_create_data(); + rept_end(tester); + rept_count_bytes(tester, sizeof(data)); + } } -static void test_allocated_data(RepetitionTester *tester) +static void +test_allocated_data(RepetitionTester *tester) { - while (rept_is_testing(tester)) { - rept_begin(tester); - ReturnData *data = malloc_create_data(); - rept_end(tester); - free(data); - rept_count_bytes(tester, sizeof(ReturnData)); - } + while (rept_is_testing(tester)) { + rept_begin(tester); + ReturnData *data = malloc_create_data(); + rept_end(tester); + free(data); + rept_count_bytes(tester, sizeof(ReturnData)); + } } -int main(void) +int +main(void) { - TestFunction tests[] = { - { "inline_static_data", test_inline_static_data }, - { "variable_static_data", test_variable_static_data }, - { "malloc_static_data", test_allocated_data }, - }; - const size_t test_count = sizeof(tests)/sizeof(TestFunction); - RepetitionTester testers[sizeof(tests)/sizeof(TestFunction)] = {0}; + TestFunction tests[] = { + {"inline_static_data", test_inline_static_data}, + {"variable_static_data", test_variable_static_data}, + {"malloc_static_data", test_allocated_data}, + }; + const size_t test_count = sizeof(tests) / sizeof(TestFunction); + RepetitionTester testers[sizeof(tests) / sizeof(TestFunction)] = {0}; - uint64_t cpu_freq = estimate_cpu_freq(2000); + uint64_t cpu_freq = estimate_cpu_freq(2000); - for (size_t i = 0; i < test_count; ++i) { - RepetitionTester *tester = &testers[i]; - TestFunction *test_func = tests + i; + for (size_t i = 0; i < test_count; ++i) { + RepetitionTester *tester = &testers[i]; + TestFunction *test_func = tests + i; - printf("\n--- %s ---\n", test_func->label); + printf("\n--- %s ---\n", test_func->label); - rept_setup(tester, sizeof(ReturnData), cpu_freq, 10); - test_func->func(tester); - } + rept_setup(tester, sizeof(ReturnData), cpu_freq, 10); + test_func->func(tester); + } - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/lib/profiler/src/repetition_tester.c b/lib/profiler/src/repetition_tester.c index 83d8cf4f..a307a0d5 100644 --- a/lib/profiler/src/repetition_tester.c +++ b/lib/profiler/src/repetition_tester.c @@ -7,152 +7,154 @@ #include "rdtsc.h" #include "perf.h" -void rept_setup(RepetitionTester *tester, uint64_t target_bytes, uint64_t - cpu_timer_freq, uint32_t test_time_seconds) +void +rept_setup(RepetitionTester *tester, uint64_t target_bytes, uint64_t cpu_timer_freq, uint32_t test_time_seconds) { - perf_setup(); - - if (tester->mode == TestMode_Uninitialized) { - tester->mode = TestMode_Testing; - tester->target_processed_byte_count = target_bytes; - tester->cpu_timer_freq = cpu_timer_freq; - tester->print_new_mins = true; - tester->results.min.E[RepVal_cpu_time] = (uint64_t) - 1; - tester->results.max.E[RepVal_cpu_time] = 0; - } else if (tester->mode == TestMode_Completed) { - tester->mode = TestMode_Testing; - - if (tester->target_processed_byte_count != target_bytes) { - rept_error(tester, "target_processed_byte_count changed"); - } - - if (tester->cpu_timer_freq != cpu_timer_freq) { - rept_error(tester, "CPU frequencey changed"); - } - } - - tester->test_time = test_time_seconds * cpu_timer_freq; - tester->start_time = read_cpu_timer(); + perf_setup(); + + if (tester->mode == TestMode_Uninitialized) { + tester->mode = TestMode_Testing; + tester->target_processed_byte_count = target_bytes; + tester->cpu_timer_freq = cpu_timer_freq; + tester->print_new_mins = true; + tester->results.min.E[RepVal_cpu_time] = (uint64_t)-1; + tester->results.max.E[RepVal_cpu_time] = 0; + } else if (tester->mode == TestMode_Completed) { + tester->mode = TestMode_Testing; + + if (tester->target_processed_byte_count != target_bytes) { + rept_error(tester, "target_processed_byte_count changed"); + } + + if (tester->cpu_timer_freq != cpu_timer_freq) { + rept_error(tester, "CPU frequencey changed"); + } + } + + tester->test_time = test_time_seconds * cpu_timer_freq; + tester->start_time = read_cpu_timer(); } -void rept_begin(RepetitionTester *tester) +void +rept_begin(RepetitionTester *tester) { - ++tester->open_block_count; - perf_reset_page_fault_count(); + ++tester->open_block_count; + perf_reset_page_fault_count(); - RepetitionValue *curr = &tester->current; - curr->E[RepVal_cpu_time] -= read_cpu_timer(); + RepetitionValue *curr = &tester->current; + curr->E[RepVal_cpu_time] -= read_cpu_timer(); } -void rept_end(RepetitionTester *tester) +void +rept_end(RepetitionTester *tester) { - ++tester->close_block_count; + ++tester->close_block_count; - RepetitionValue *curr = &tester->current; - curr->E[RepVal_cpu_time] += read_cpu_timer(); - curr->E[RepVal_page_faults] += perf_read_page_fault_count(); + RepetitionValue *curr = &tester->current; + curr->E[RepVal_cpu_time] += read_cpu_timer(); + curr->E[RepVal_page_faults] += perf_read_page_fault_count(); } -void rept_count_bytes(RepetitionTester *tester, uint64_t byte_count) +void +rept_count_bytes(RepetitionTester *tester, uint64_t byte_count) { - RepetitionValue *curr = &tester->current; - curr->E[RepVal_byte_count] += byte_count; + RepetitionValue *curr = &tester->current; + curr->E[RepVal_byte_count] += byte_count; } -static void print_row(FILE *fp, const char *label, RepetitionValue val, uint64_t rate) +static void +print_row(FILE *fp, const char *label, RepetitionValue val, uint64_t rate) { - static const double gb = 1024.0 * 1024.0 * 1024.0; - uint64_t test_count = val.E[RepVal_test_count]; - double divisor = test_count ? (double) test_count : 1; - - double E[RepVal_count]; - for (size_t i = 0; i < RepVal_count; ++i) { - E[i] = (double) val.E[i] / divisor; - } - - uint64_t time = E[RepVal_cpu_time]; - double seconds = (double) time/rate; - double millis = seconds*1000.0; - double gb_per_second = E[RepVal_byte_count] / (gb * seconds); - - if (isatty(fileno(fp))) { - fprintf(fp, C_WHITE "%s:" C_GREEN " %lu " C_YELLOW "(%fms) " C_CYAN "%fgb/s" C_RESET, - label, time, millis, gb_per_second); - } else { - fprintf(fp, "%s: %lu (%fms) %fgb/s", label, time, millis, gb_per_second); - } - - if (E[RepVal_page_faults] > 0) { - if (isatty(fileno(fp))) { - fprintf(fp, C_RED " PF: %0.4f (%0.4fk/fault)" C_RESET, - E[RepVal_page_faults], E[RepVal_byte_count]/(E[RepVal_page_faults] * 1024.0)); - } else { - fprintf(fp, " PF: %.4f (%.4fk/fault)", - E[RepVal_page_faults], - E[RepVal_byte_count]/(E[RepVal_page_faults] * 1024.0)); - } - } - fprintf(fp, " \r"); + static const double gb = 1024.0 * 1024.0 * 1024.0; + uint64_t test_count = val.E[RepVal_test_count]; + double divisor = test_count ? (double)test_count : 1; + + double E[RepVal_count]; + for (size_t i = 0; i < RepVal_count; ++i) { + E[i] = (double)val.E[i] / divisor; + } + + uint64_t time = E[RepVal_cpu_time]; + double seconds = (double)time / rate; + double millis = seconds * 1000.0; + double gb_per_second = E[RepVal_byte_count] / (gb * seconds); + + if (isatty(fileno(fp))) { + fprintf(fp, C_WHITE "%s:" C_GREEN " %lu " C_YELLOW "(%fms) " C_CYAN "%fgb/s" C_RESET, label, time, + millis, gb_per_second); + } else { + fprintf(fp, "%s: %lu (%fms) %fgb/s", label, time, millis, gb_per_second); + } + + if (E[RepVal_page_faults] > 0) { + if (isatty(fileno(fp))) { + fprintf(fp, C_RED " PF: %0.4f (%0.4fk/fault)" C_RESET, E[RepVal_page_faults], + E[RepVal_byte_count] / (E[RepVal_page_faults] * 1024.0)); + } else { + fprintf(fp, " PF: %.4f (%.4fk/fault)", E[RepVal_page_faults], + E[RepVal_byte_count] / (E[RepVal_page_faults] * 1024.0)); + } + } + fprintf(fp, " \r"); } -bool rept_is_testing(RepetitionTester *tester) +bool +rept_is_testing(RepetitionTester *tester) { - if (tester->mode == TestMode_Testing) { - RepetitionValue curr = tester->current; - uint64_t curr_time = read_cpu_timer(); - - if (tester->open_block_count) { - - if (tester->open_block_count != tester->close_block_count) { - rept_error(tester, "Unbalanced begin/end time"); - } else if (curr.E[RepVal_byte_count] != tester->target_processed_byte_count) { - rept_error(tester, "Processed byte count mismatch"); - } else { - RepetitionTestResult *result = &tester->results; - - curr.E[RepVal_test_count] = 1; - - for (size_t i = 0; i < RepVal_count; ++i) { - result->total.E[i] += curr.E[i]; - } - if (result->max.E[RepVal_cpu_time] < curr.E[RepVal_cpu_time]) { - result->max = curr; - } - if (result->min.E[RepVal_cpu_time] > curr.E[RepVal_cpu_time]) { - result->min = curr; - tester->start_time = curr_time; - if (tester->print_new_mins) { - print_row(stdout, - "Min", - result->min, - tester->cpu_timer_freq); - fflush(stdout); - } - } - - tester->open_block_count = 0; - tester->close_block_count = 0; - tester->current = (RepetitionValue) {0}; - } - } - - if (curr_time - tester->start_time > tester->test_time) { - tester->mode = TestMode_Completed; - perf_close(); - rept_print_results(tester, stdout); - } - } - return tester->mode == TestMode_Testing; + if (tester->mode == TestMode_Testing) { + RepetitionValue curr = tester->current; + uint64_t curr_time = read_cpu_timer(); + + if (tester->open_block_count) { + + if (tester->open_block_count != tester->close_block_count) { + rept_error(tester, "Unbalanced begin/end time"); + } else if (curr.E[RepVal_byte_count] != tester->target_processed_byte_count) { + rept_error(tester, "Processed byte count mismatch"); + } else { + RepetitionTestResult *result = &tester->results; + + curr.E[RepVal_test_count] = 1; + + for (size_t i = 0; i < RepVal_count; ++i) { + result->total.E[i] += curr.E[i]; + } + if (result->max.E[RepVal_cpu_time] < curr.E[RepVal_cpu_time]) { + result->max = curr; + } + if (result->min.E[RepVal_cpu_time] > curr.E[RepVal_cpu_time]) { + result->min = curr; + tester->start_time = curr_time; + if (tester->print_new_mins) { + print_row(stdout, "Min", result->min, tester->cpu_timer_freq); + fflush(stdout); + } + } + + tester->open_block_count = 0; + tester->close_block_count = 0; + tester->current = (RepetitionValue){0}; + } + } + + if (curr_time - tester->start_time > tester->test_time) { + tester->mode = TestMode_Completed; + perf_close(); + rept_print_results(tester, stdout); + } + } + return tester->mode == TestMode_Testing; } -void rept_print_results(RepetitionTester *tester, FILE *fp) +void +rept_print_results(RepetitionTester *tester, FILE *fp) { - RepetitionTestResult *result = &tester->results; - uint64_t cpu_freq = tester->cpu_timer_freq; - print_row(fp, "Min", result->min, cpu_freq); - fprintf(fp, "\n"); - print_row(fp, "Max", result->max, cpu_freq); - fprintf(fp, "\n"); - print_row(fp, "Avg", result->total, cpu_freq); - fprintf(fp, "\n"); + RepetitionTestResult *result = &tester->results; + uint64_t cpu_freq = tester->cpu_timer_freq; + print_row(fp, "Min", result->min, cpu_freq); + fprintf(fp, "\n"); + print_row(fp, "Max", result->max, cpu_freq); + fprintf(fp, "\n"); + print_row(fp, "Avg", result->total, cpu_freq); + fprintf(fp, "\n"); } diff --git a/lib/profiler/test/calc_cpu_freq.c b/lib/profiler/test/calc_cpu_freq.c index b220175a..ec020305 100644 --- a/lib/profiler/test/calc_cpu_freq.c +++ b/lib/profiler/test/calc_cpu_freq.c @@ -5,27 +5,28 @@ typedef double f64; typedef uint64_t u64; -int main(void) +int +main(void) { - u64 os_freq = get_os_time_freq(); - u64 cpu_start = read_cpu_timer(); - u64 os_start = read_os_timer(); - u64 os_elapsed = 0; - u64 os_wait_time = os_freq; - while (os_elapsed < os_wait_time) { - u64 os_end = read_os_timer(); - os_elapsed = os_end - os_start; - } + u64 os_freq = get_os_time_freq(); + u64 cpu_start = read_cpu_timer(); + u64 os_start = read_os_timer(); + u64 os_elapsed = 0; + u64 os_wait_time = os_freq; + while (os_elapsed < os_wait_time) { + u64 os_end = read_os_timer(); + os_elapsed = os_end - os_start; + } - u64 cpu_end = read_cpu_timer(); - u64 cpu_elapsed = cpu_end - cpu_start; - u64 cpu_freq = 0; - if (os_elapsed) { - cpu_freq = os_freq * cpu_elapsed / os_elapsed; - } + u64 cpu_end = read_cpu_timer(); + u64 cpu_elapsed = cpu_end - cpu_start; + u64 cpu_freq = 0; + if (os_elapsed) { + cpu_freq = os_freq * cpu_elapsed / os_elapsed; + } - printf(" OS Freq: %lu (reported)\n", os_freq); - printf(" OS Seconds: %.4f\n", (f64) os_elapsed/ (f64) os_freq); - printf(" CPU Timer: %lu -> %lu = %lu\n", cpu_start, cpu_end, cpu_elapsed); - printf(" CPU Freq: %lu (guessed)\n", cpu_freq); + printf(" OS Freq: %lu (reported)\n", os_freq); + printf(" OS Seconds: %.4f\n", (f64)os_elapsed / (f64)os_freq); + printf(" CPU Timer: %lu -> %lu = %lu\n", cpu_start, cpu_end, cpu_elapsed); + printf(" CPU Freq: %lu (guessed)\n", cpu_freq); } diff --git a/src/defines.h b/src/defines.h index ec1882a6..98ea6295 100644 --- a/src/defines.h +++ b/src/defines.h @@ -24,30 +24,30 @@ #include "config.h" /* Room/Map dimensions */ -#define MAP_ROOM_WIDTH 16 +#define MAP_ROOM_WIDTH 16 #define MAP_ROOM_HEIGHT 12 #define MAP_V_ROOM_COUNT 10 #define MAP_H_ROOM_COUNT 10 -#define TILE_DIMENSION 32 +#define TILE_DIMENSION 32 #define SPRITE_DIMENSION 16 /* Display stuff */ -#define GAME_VIEW_WIDTH (MAP_ROOM_WIDTH * TILE_DIMENSION) // 16 * 32 +#define GAME_VIEW_WIDTH (MAP_ROOM_WIDTH * TILE_DIMENSION) // 16 * 32 #define GAME_VIEW_HEIGHT (MAP_ROOM_HEIGHT * TILE_DIMENSION) // 12 * 32 -#define SKILL_BAR_WIDTH GAME_VIEW_WIDTH +#define SKILL_BAR_WIDTH GAME_VIEW_WIDTH #define SKILL_BAR_HEIGHT 32 -#define RIGHT_GUI_WIDTH (10 * SPRITE_DIMENSION) // 10 * 16 +#define RIGHT_GUI_WIDTH (10 * SPRITE_DIMENSION) // 10 * 16 #define MINIMAP_GUI_HEIGHT 128 -#define STATS_GUI_HEIGHT (GAME_VIEW_HEIGHT + SKILL_BAR_HEIGHT - MINIMAP_GUI_HEIGHT) +#define STATS_GUI_HEIGHT (GAME_VIEW_HEIGHT + SKILL_BAR_HEIGHT - MINIMAP_GUI_HEIGHT) #define BOTTOM_GUI_HEIGHT (10 * SPRITE_DIMENSION) -#define BOTTOM_GUI_WIDTH (GAME_VIEW_WIDTH + RIGHT_GUI_WIDTH) +#define BOTTOM_GUI_WIDTH (GAME_VIEW_WIDTH + RIGHT_GUI_WIDTH) -#define SCREEN_WIDTH (GAME_VIEW_WIDTH + RIGHT_GUI_WIDTH) +#define SCREEN_WIDTH (GAME_VIEW_WIDTH + RIGHT_GUI_WIDTH) #define SCREEN_HEIGHT (GAME_VIEW_HEIGHT + SKILL_BAR_HEIGHT + BOTTOM_GUI_HEIGHT) /* Quality of life stuff */ @@ -66,15 +66,15 @@ #endif // _WIN32 #define UNPACK_COLOR(color) color.r, color.g, color.b, color.a -#define C_WHITE (SDL_Color){255, 255, 255, 255} -#define C_RED (SDL_Color){255, 0, 0, 255} -#define C_GREEN (SDL_Color){0, 255, 0, 255} -#define C_BLUE (SDL_Color){60, 134, 252, 255} -#define C_LIGHTBLUE (SDL_Color){143, 178, 234, 255} -#define C_YELLOW (SDL_Color){255, 255, 0, 255} -#define C_BLACK (SDL_Color){0, 0, 0, 255} -#define C_PURPLE (SDL_Color){137, 16, 229, 255} -#define C_GREY (SDL_Color){89, 89, 89, 255} +#define C_WHITE (SDL_Color){255, 255, 255, 255} +#define C_RED (SDL_Color){255, 0, 0, 255} +#define C_GREEN (SDL_Color){0, 255, 0, 255} +#define C_BLUE (SDL_Color){60, 134, 252, 255} +#define C_LIGHTBLUE (SDL_Color){143, 178, 234, 255} +#define C_YELLOW (SDL_Color){255, 255, 0, 255} +#define C_BLACK (SDL_Color){0, 0, 0, 255} +#define C_PURPLE (SDL_Color){137, 16, 229, 255} +#define C_GREY (SDL_Color){89, 89, 89, 255} // MSVC seems to have min/max defined. // Haven't looked into it further. @@ -88,9 +88,9 @@ typedef enum Direction_t { UP, DOWN, LEFT, RIGHT, INVALID } Direction; typedef enum GameMode { REGULAR, QUICK, ARCADE } GameMode; -#define CONTROLLER_BTN(xindex, mode) CLIP16(xindex, mode == GAMEPAD_TYPE_XB ? 0 : 16) +#define CONTROLLER_BTN(xindex, mode) CLIP16(xindex, mode == GAMEPAD_TYPE_XB ? 0 : 16) #define CONTROLLER_TRIGGER(xindex, mode) CLIP16(xindex + (mode == GAMEPAD_TYPE_XB ? 16 : 0), 32) -#define CONTROLLER_BUMPER(xindex, mode) CLIP16(xindex + (mode == GAMEPAD_TYPE_XB ? 16 : 0), 48) -#define CONTROLLER_OPT(xindex, mode) CLIP16(xindex + (mode == GAMEPAD_TYPE_PS ? 16 : 0), 64) +#define CONTROLLER_BUMPER(xindex, mode) CLIP16(xindex + (mode == GAMEPAD_TYPE_XB ? 16 : 0), 48) +#define CONTROLLER_OPT(xindex, mode) CLIP16(xindex + (mode == GAMEPAD_TYPE_PS ? 16 : 0), 64) #endif // DEFINES_H_ diff --git a/src/gui.c b/src/gui.c index 92c3b6cb..45e800e9 100644 --- a/src/gui.c +++ b/src/gui.c @@ -39,7 +39,7 @@ #define DEFAULT_EVENT_MESSAGES {NULL, 5, 0, LOG_LINES_MAX_LEN} #define POS_Y_COLLECTABLES 64 -#define POS_Y_XPBAR 128 +#define POS_Y_XPBAR 128 static struct LogData_t { char log[LOG_LINES_COUNT][LOG_LINES_MAX_LEN]; diff --git a/src/gui.h b/src/gui.h index d4202a39..55680cb3 100644 --- a/src/gui.h +++ b/src/gui.h @@ -20,13 +20,13 @@ #define GUI_H_ #include "roommatrix.h" -#define LOG_LINES_COUNT 10 +#define LOG_LINES_COUNT 10 #define LOG_LINES_MAX_LEN 200 -#define LOG_FONT_SIZE 8 -#define LABEL_FONT_SIZE 8 +#define LOG_FONT_SIZE 8 +#define LABEL_FONT_SIZE 8 #define EVENT_MESSAGE_DISPLAY_TIME 2500 -#define EVENT_MESSAGE_FONT_SIZE 20 +#define EVENT_MESSAGE_FONT_SIZE 20 #include "linkedlist.h" #include "sprite.h" diff --git a/src/input.h b/src/input.h index b0087803..e4b942b8 100644 --- a/src/input.h +++ b/src/input.h @@ -22,40 +22,40 @@ #include #include -#define KEY_LEFT 0x1 -#define KEY_RIGHT 0x2 -#define KEY_UP 0x4 -#define KEY_DOWN 0x8 -#define KEY_NUM0 0x10 -#define KEY_NUM1 0x20 -#define KEY_NUM2 0x40 -#define KEY_NUM3 0x80 -#define KEY_NUM4 0x100 -#define KEY_NUM5 0x200 -#define KEY_NUM6 0x400 -#define KEY_NUM7 0x800 -#define KEY_NUM8 0x1000 -#define KEY_NUM9 0x2000 -#define KEY_ESC 0x4000 -#define KEY_ENTER 0x8000 -#define KEY_SPACE 0x10000 -#define KEY_TAB 0x20000 +#define KEY_LEFT 0x1 +#define KEY_RIGHT 0x2 +#define KEY_UP 0x4 +#define KEY_DOWN 0x8 +#define KEY_NUM0 0x10 +#define KEY_NUM1 0x20 +#define KEY_NUM2 0x40 +#define KEY_NUM3 0x80 +#define KEY_NUM4 0x100 +#define KEY_NUM5 0x200 +#define KEY_NUM6 0x400 +#define KEY_NUM7 0x800 +#define KEY_NUM8 0x1000 +#define KEY_NUM9 0x2000 +#define KEY_ESC 0x4000 +#define KEY_ENTER 0x8000 +#define KEY_SPACE 0x10000 +#define KEY_TAB 0x20000 #define KEY_BACKSPACE 0x40000 -#define KEY_CTRL_M 0x1 -#define KEY_CTRL_S 0x2 -#define KEY_CTRL_D 0x4 +#define KEY_CTRL_M 0x1 +#define KEY_CTRL_S 0x2 +#define KEY_CTRL_D 0x4 #define KEY_SHIFT_NUM1 0x8 #define KEY_SHIFT_NUM2 0x10 #define KEY_SHIFT_NUM3 0x20 #define KEY_SHIFT_NUM4 0x40 #define KEY_SHIFT_NUM5 0x80 -#define KEY_CTRL_F 0x100 -#define KEY_CTRL_V 0x200 +#define KEY_CTRL_F 0x100 +#define KEY_CTRL_V 0x200 -#define MBUTTON_LEFT 0x1 +#define MBUTTON_LEFT 0x1 #define MBUTTON_MIDDLE 0x2 -#define MBUTTON_RIGHT 0x4 +#define MBUTTON_RIGHT 0x4 #define TEXT_INPUT_MAX_LEN 16 diff --git a/src/loot.c b/src/loot.c index d9a61e76..d707f9de 100644 --- a/src/loot.c +++ b/src/loot.c @@ -23,8 +23,8 @@ #include "gui.h" #include "mixer.h" -#define MAX_ITEM_DROP 16 -#define ITEM_DROP_TIME_MS 175 +#define MAX_ITEM_DROP 16 +#define ITEM_DROP_TIME_MS 175 #define DROP_POSITION_COUNT 9 static bool item_dropped = false; diff --git a/src/player.c b/src/player.c index 3e91cec8..476aa201 100644 --- a/src/player.c +++ b/src/player.c @@ -44,10 +44,10 @@ #endif // STEAM_BUILD #define ENGINEER_STATS {12, 12, 5, 7, 2, 1, 1, false, false} -#define MAGE_STATS {12, 12, 5, 7, 1, 1, 1, false, false} -#define PALADIN_STATS {12, 12, 8, 9, 3, 1, 1, false, false} -#define ROGUE_STATS {9, 9, 6, 9, 4, 2, 1, false, false} -#define WARRIOR_STATS {12, 12, 8, 9, 3, 1, 1, false, false} +#define MAGE_STATS {12, 12, 5, 7, 1, 1, 1, false, false} +#define PALADIN_STATS {12, 12, 8, 9, 3, 1, 1, false, false} +#define ROGUE_STATS {9, 9, 6, 9, 4, 2, 1, false, false} +#define WARRIOR_STATS {12, 12, 8, 9, 3, 1, 1, false, false} static void levelup(Player *player) diff --git a/src/position.c b/src/position.c index 41d82e65..dce1c100 100644 --- a/src/position.c +++ b/src/position.c @@ -20,7 +20,7 @@ #include "position.h" #include "defines.h" -#define ROOM_PX_WIDTH (TILE_DIMENSION * MAP_ROOM_WIDTH) +#define ROOM_PX_WIDTH (TILE_DIMENSION * MAP_ROOM_WIDTH) #define ROOM_PX_HEIGHT (TILE_DIMENSION * MAP_ROOM_HEIGHT) Position diff --git a/src/roommatrix.h b/src/roommatrix.h index d9c8f8b1..b732dea4 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -60,15 +60,15 @@ typedef struct RoomSpace { LinkedList *objects; } RoomSpace; -#define SPACE_IS_OCCUPIED(space) ((space) && (space->flags & TILE_OCCUPIED)) -#define SPACE_IS_LETHAL(space) ((space) && (space->flags & TILE_LETHAL)) -#define SPACE_IS_LIGHTSOURCE(space) ((space) && (space->flags & TILE_LIGHTSOURCE)) -#define SPACE_IS_DAMAGING(space) ((space) && (space->flags & TILE_DAMAGE)) -#define SPACE_SET_FLAG(space, flag) ((space) && ((space)->flags |= flag)) -#define SPACE_CLEAR_FLAG(space, flag) ((space)->flags &= ~flag) +#define SPACE_IS_OCCUPIED(space) ((space) && (space->flags & TILE_OCCUPIED)) +#define SPACE_IS_LETHAL(space) ((space) && (space->flags & TILE_LETHAL)) +#define SPACE_IS_LIGHTSOURCE(space) ((space) && (space->flags & TILE_LIGHTSOURCE)) +#define SPACE_IS_DAMAGING(space) ((space) && (space->flags & TILE_DAMAGE)) +#define SPACE_SET_FLAG(space, flag) ((space) && ((space)->flags |= flag)) +#define SPACE_CLEAR_FLAG(space, flag) ((space)->flags &= ~flag) #define SPACE_TOGGLE_FLAG(space, flag) ((space) && ((space)->flags ^= flag)) -#define SPACE_IS_BLOCKED(space) (SPACE_IS_OCCUPIED(space) || (space)->monster) +#define SPACE_IS_BLOCKED(space) (SPACE_IS_OCCUPIED(space) || (space)->monster) #define SPACE_IS_WALKABLE(space) (!(space->flags & (TILE_OCCUPIED | TILE_LETHAL | TILE_DAMAGE))) typedef struct RoomMatrix_t { diff --git a/src/time.c b/src/time.c index 623e3abb..8b4e845f 100644 --- a/src/time.c +++ b/src/time.c @@ -20,8 +20,8 @@ #include "time.h" #include "util.h" -#define SECONDS_PER_DAY 86400 -#define SECONDS_PER_HOUR 3600 +#define SECONDS_PER_DAY 86400 +#define SECONDS_PER_HOUR 3600 #define SECONDS_PER_MINUTE 60 #if defined(_MSC_VER) || defined(__MINGW32__) diff --git a/src/util.h b/src/util.h index 84c3a63f..c550b6d6 100644 --- a/src/util.h +++ b/src/util.h @@ -37,7 +37,7 @@ #ifdef DEBUG #define debug(...) log_print(stdout, "DEBUG", __FNAME__, __LINE__, __func__, __VA_ARGS__) -#define info(...) log_print(stdout, "INFO", __FNAME__, __LINE__, __func__, __VA_ARGS__) +#define info(...) log_print(stdout, "INFO", __FNAME__, __LINE__, __func__, __VA_ARGS__) #else // DEBUG #define debug(...) \ do { \ @@ -47,7 +47,7 @@ } while (0) #endif // DEBUG -#define warn(...) log_print(stderr, "WARN", __FNAME__, __LINE__, __func__, __VA_ARGS__) +#define warn(...) log_print(stderr, "WARN", __FNAME__, __LINE__, __func__, __VA_ARGS__) #define error(...) log_print(stderr, "ERROR", __FNAME__, __LINE__, __func__, __VA_ARGS__) #ifdef DEBUG #define fatal(...) \ diff --git a/src/vector2d.h b/src/vector2d.h index b1450fa2..beb9201b 100644 --- a/src/vector2d.h +++ b/src/vector2d.h @@ -23,9 +23,9 @@ #define VECTOR2D_NODIR (Vector2d){0, 0} #define VECTOR2D_RIGHT (Vector2d){1, 0} -#define VECTOR2D_LEFT (Vector2d){-1, 0} -#define VECTOR2D_UP (Vector2d){0, -1} -#define VECTOR2D_DOWN (Vector2d){0, 1} +#define VECTOR2D_LEFT (Vector2d){-1, 0} +#define VECTOR2D_UP (Vector2d){0, -1} +#define VECTOR2D_DOWN (Vector2d){0, 1} #define VEC2D(x, y) \ (Vector2d) \