| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright © 2025, Niklas Haas | ||
| 3 | * Copyright © 2018, VideoLAN and dav1d authors | ||
| 4 | * Copyright © 2018, Two Orioles, LLC | ||
| 5 | * All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions are met: | ||
| 9 | * | ||
| 10 | * 1. Redistributions of source code must retain the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer. | ||
| 12 | * | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| 14 | * this list of conditions and the following disclaimer in the documentation | ||
| 15 | * and/or other materials provided with the distribution. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | */ | ||
| 29 | |||
| 30 | #ifndef CHECKASM_INTERNAL_H | ||
| 31 | #define CHECKASM_INTERNAL_H | ||
| 32 | |||
| 33 | #include <signal.h> | ||
| 34 | #include <stdint.h> | ||
| 35 | #include <stdio.h> | ||
| 36 | #include <stdlib.h> | ||
| 37 | #include <stdarg.h> | ||
| 38 | |||
| 39 | #include "checkasm/attributes.h" | ||
| 40 | #include "checkasm/test.h" | ||
| 41 | #include "longjmp.h" | ||
| 42 | #include "stats.h" | ||
| 43 | |||
| 44 | #ifdef __GNUC__ | ||
| 45 | #define COLD __attribute__((cold)) | ||
| 46 | #else | ||
| 47 | #define COLD | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #ifndef __has_attribute | ||
| 51 | #define __has_attribute(x) 0 | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #ifdef _MSC_VER | ||
| 55 | #define NOINLINE __declspec(noinline) | ||
| 56 | #elif __has_attribute(noclone) | ||
| 57 | #define NOINLINE __attribute__((noinline, noclone)) | ||
| 58 | #else | ||
| 59 | #define NOINLINE __attribute__((noinline)) | ||
| 60 | #endif | ||
| 61 | |||
| 62 | #ifdef _MSC_VER | ||
| 63 | #define NORETURN __declspec(noreturn) | ||
| 64 | #else | ||
| 65 | #include <stdnoreturn.h> | ||
| 66 | #define NORETURN noreturn | ||
| 67 | #endif | ||
| 68 | |||
| 69 | #ifdef _MSC_VER | ||
| 70 | #define ALWAYS_INLINE __forceinline | ||
| 71 | #else | ||
| 72 | #define ALWAYS_INLINE inline __attribute__((always_inline)) | ||
| 73 | #endif | ||
| 74 | |||
| 75 | #ifdef __GNUC__ | ||
| 76 | #define THREAD_LOCAL __thread | ||
| 77 | #else | ||
| 78 | #define THREAD_LOCAL _Thread_local | ||
| 79 | #endif | ||
| 80 | |||
| 81 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) | ||
| 82 | |||
| 83 | #ifdef _MSC_VER | ||
| 84 | #define PACKED(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop)) | ||
| 85 | #else | ||
| 86 | #define PACKED(...) __VA_ARGS__ __attribute__((__packed__)) | ||
| 87 | #endif | ||
| 88 | |||
| 89 | #if __has_attribute(fallthrough) | ||
| 90 | #define FALLTHROUGH __attribute__((fallthrough)) | ||
| 91 | #else | ||
| 92 | #define FALLTHROUGH do {} while (0) | ||
| 93 | #endif | ||
| 94 | |||
| 95 | void checkasm_srand(unsigned seed); | ||
| 96 | |||
| 97 | /* Internal variant of checkasm_fail_func() that also jumps back to the signal | ||
| 98 | * handler */ | ||
| 99 | NORETURN void checkasm_fail_abort(const char *msg, ...) CHECKASM_PRINTF(1, 2); | ||
| 100 | |||
| 101 | #define COLOR_DEFAULT -1 | ||
| 102 | #define COLOR_RED 31 | ||
| 103 | #define COLOR_GREEN 32 | ||
| 104 | #define COLOR_YELLOW 33 | ||
| 105 | #define COLOR_BLUE 34 | ||
| 106 | #define COLOR_MAGENTA 35 | ||
| 107 | #define COLOR_CYAN 36 | ||
| 108 | #define COLOR_WHITE 37 | ||
| 109 | |||
| 110 | /* Colored variant of fprintf for terminals that support it */ | ||
| 111 | void checkasm_setup_fprintf(void); | ||
| 112 | int checkasm_vfprintf(FILE *const f, int color, const char *fmt, va_list arg) | ||
| 113 | CHECKASM_PRINTF(3, 0); | ||
| 114 | |||
| 115 | CHECKASM_PRINTF(3, 4) | ||
| 116 | 2895 | static inline int checkasm_fprintf(FILE *const f, int color, const char *fmt, ...) | |
| 117 | { | ||
| 118 | va_list ap; | ||
| 119 | 2895 | va_start(ap, fmt); | |
| 120 | 2895 | int ret = checkasm_vfprintf(f, color, fmt, ap); | |
| 121 | 2895 | va_end(ap); | |
| 122 | 2895 | return ret; | |
| 123 | } | ||
| 124 | |||
| 125 | #define LOG_COLOR(...) checkasm_fprintf(stderr, __VA_ARGS__) | ||
| 126 | #define LOG(...) LOG_COLOR(COLOR_DEFAULT, __VA_ARGS__) | ||
| 127 | |||
| 128 | /* Update the statusline, reprinted automatically as needed. Pass NULL or an | ||
| 129 | * empty string ("") to clear it entirely. Maximum 256 characters */ | ||
| 130 | void checkasm_statusline(const char *status); | ||
| 131 | |||
| 132 | /* Light-weight helper for printing nested JSON objects */ | ||
| 133 | typedef struct CheckasmJson { | ||
| 134 | FILE *file; | ||
| 135 | int level; | ||
| 136 | int nonempty; | ||
| 137 | } CheckasmJson; | ||
| 138 | |||
| 139 | void checkasm_json(CheckasmJson *json, const char *key, const char *fmt, ...) | ||
| 140 | CHECKASM_PRINTF(3, 4); | ||
| 141 | void checkasm_json_str(CheckasmJson *json, const char *key, const char *str); | ||
| 142 | void checkasm_json_push(CheckasmJson *json, const char *const key, char type); | ||
| 143 | void checkasm_json_pop(CheckasmJson *json, char type); | ||
| 144 | |||
| 145 | /* Platform specific signal handling */ | ||
| 146 | void checkasm_set_signal_handlers(void); | ||
| 147 | const char *checkasm_get_last_signal_desc(void); | ||
| 148 | |||
| 149 | /* Set to 1 if the process should terminate. The current test will continue | ||
| 150 | * executing until the next report() call, then the process will exit. */ | ||
| 151 | extern volatile sig_atomic_t checkasm_interrupted; | ||
| 152 | |||
| 153 | extern checkasm_jmp_buf checkasm_context; | ||
| 154 | |||
| 155 | /* Platform specific timing code */ | ||
| 156 | extern CheckasmPerf checkasm_perf; | ||
| 157 | |||
| 158 | int checkasm_perf_init(void); | ||
| 159 | int checkasm_perf_init_linux(CheckasmPerf *perf); | ||
| 160 | int checkasm_perf_init_macos(CheckasmPerf *perf); | ||
| 161 | int checkasm_perf_init_arm(CheckasmPerf *perf); | ||
| 162 | int checkasm_perf_validate_start(const CheckasmPerf *perf); | ||
| 163 | int checkasm_perf_validate_start_stop(const CheckasmPerf *perf); | ||
| 164 | |||
| 165 | int checkasm_run_on_all_cores(void (*func)(void)); | ||
| 166 | |||
| 167 | uint64_t checkasm_gettime_nsec(void); | ||
| 168 | uint64_t checkasm_gettime_nsec_diff(uint64_t t); /* subtracts t */ | ||
| 169 | unsigned checkasm_seed(void); | ||
| 170 | void checkasm_noop(void *); | ||
| 171 | |||
| 172 | /* These functions update the measurements in `meas` directly; must be initialized */ | ||
| 173 | void checkasm_measure_nop_cycles(CheckasmMeasurement *meas, uint64_t target_cycles); | ||
| 174 | void checkasm_measure_perf_scale(CheckasmMeasurement *meas); /* ns per cycle */ | ||
| 175 | |||
| 176 | /* Miscellaneous helpers */ | ||
| 177 | 1828 | static inline int imax(const int a, const int b) | |
| 178 | { | ||
| 179 | 1828 | return a > b ? a : b; | |
| 180 | } | ||
| 181 | |||
| 182 | ✗ | static inline int imin(const int a, const int b) | |
| 183 | { | ||
| 184 | ✗ | return a < b ? a : b; | |
| 185 | } | ||
| 186 | |||
| 187 | 43308 | static inline void *checkasm_handle_oom(void *ptr) | |
| 188 | { | ||
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43308 times.
|
43308 | if (!ptr) { |
| 190 | ✗ | fprintf(stderr, "checkasm: out of memory\n"); | |
| 191 | ✗ | exit(1); | |
| 192 | } | ||
| 193 | 43308 | return ptr; | |
| 194 | } | ||
| 195 | |||
| 196 | /* Allocate a zero-initialized block, clean up and exit on failure */ | ||
| 197 | 29942 | static inline void *checkasm_mallocz(const size_t size) | |
| 198 | { | ||
| 199 | 29942 | return checkasm_handle_oom(calloc(1, size)); | |
| 200 | } | ||
| 201 | |||
| 202 | 13366 | static inline char *checkasm_strdup(const char *str) | |
| 203 | { | ||
| 204 | 13366 | return checkasm_handle_oom(strdup(str)); | |
| 205 | } | ||
| 206 | |||
| 207 | char *checkasm_vasprintf(const char *fmt, va_list arg); | ||
| 208 | |||
| 209 | #endif /* CHECKASM_INTERNAL_H */ | ||
| 210 |