| 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 | void checkasm_fprintf(FILE *const f, const int color, const char *const fmt, ...) | ||
| 113 | CHECKASM_PRINTF(3, 4); | ||
| 114 | |||
| 115 | /* Light-weight helper for printing nested JSON objects */ | ||
| 116 | typedef struct CheckasmJson { | ||
| 117 | FILE *file; | ||
| 118 | int level; | ||
| 119 | int nonempty; | ||
| 120 | } CheckasmJson; | ||
| 121 | |||
| 122 | void checkasm_json(CheckasmJson *json, const char *key, const char *fmt, ...) | ||
| 123 | CHECKASM_PRINTF(3, 4); | ||
| 124 | void checkasm_json_str(CheckasmJson *json, const char *key, const char *str); | ||
| 125 | void checkasm_json_push(CheckasmJson *json, const char *const key, char type); | ||
| 126 | void checkasm_json_pop(CheckasmJson *json, char type); | ||
| 127 | |||
| 128 | /* Platform specific signal handling */ | ||
| 129 | void checkasm_set_signal_handlers(void); | ||
| 130 | const char *checkasm_get_last_signal_desc(void); | ||
| 131 | |||
| 132 | /* Set to 1 if the process should terminate. The current test will continue | ||
| 133 | * executing until the next report() call, then the process will exit. */ | ||
| 134 | extern volatile sig_atomic_t checkasm_interrupted; | ||
| 135 | |||
| 136 | extern checkasm_jmp_buf checkasm_context; | ||
| 137 | |||
| 138 | /* Platform specific timing code */ | ||
| 139 | extern CheckasmPerf checkasm_perf; | ||
| 140 | |||
| 141 | int checkasm_perf_init(void); | ||
| 142 | int checkasm_perf_init_linux(CheckasmPerf *perf); | ||
| 143 | int checkasm_perf_init_macos(CheckasmPerf *perf); | ||
| 144 | int checkasm_perf_init_arm(CheckasmPerf *perf); | ||
| 145 | int checkasm_perf_validate_start(const CheckasmPerf *perf); | ||
| 146 | int checkasm_perf_validate_start_stop(const CheckasmPerf *perf); | ||
| 147 | |||
| 148 | int checkasm_run_on_all_cores(void (*func)(void)); | ||
| 149 | |||
| 150 | uint64_t checkasm_gettime_nsec(void); | ||
| 151 | uint64_t checkasm_gettime_nsec_diff(uint64_t t); /* subtracts t */ | ||
| 152 | unsigned checkasm_seed(void); | ||
| 153 | void checkasm_noop(void *); | ||
| 154 | |||
| 155 | /* These functions update the measurements in `meas` directly; must be initialized */ | ||
| 156 | void checkasm_measure_nop_cycles(CheckasmMeasurement *meas, uint64_t target_cycles); | ||
| 157 | void checkasm_measure_perf_scale(CheckasmMeasurement *meas); /* ns per cycle */ | ||
| 158 | |||
| 159 | /* Miscellaneous helpers */ | ||
| 160 | 471 | static inline int imax(const int a, const int b) | |
| 161 | { | ||
| 162 | 471 | return a > b ? a : b; | |
| 163 | } | ||
| 164 | |||
| 165 | ✗ | static inline int imin(const int a, const int b) | |
| 166 | { | ||
| 167 | ✗ | return a < b ? a : b; | |
| 168 | } | ||
| 169 | |||
| 170 | 44324 | static inline void *checkasm_handle_oom(void *ptr) | |
| 171 | { | ||
| 172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44324 times.
|
44324 | if (!ptr) { |
| 173 | ✗ | fprintf(stderr, "checkasm: out of memory\n"); | |
| 174 | ✗ | exit(1); | |
| 175 | } | ||
| 176 | 44324 | return ptr; | |
| 177 | } | ||
| 178 | |||
| 179 | /* Allocate a zero-initialized block, clean up and exit on failure */ | ||
| 180 | 29942 | static inline void *checkasm_mallocz(const size_t size) | |
| 181 | { | ||
| 182 | 29942 | return checkasm_handle_oom(calloc(1, size)); | |
| 183 | } | ||
| 184 | |||
| 185 | 14382 | static inline char *checkasm_strdup(const char *str) | |
| 186 | { | ||
| 187 | 14382 | return checkasm_handle_oom(strdup(str)); | |
| 188 | } | ||
| 189 | |||
| 190 | char *checkasm_vasprintf(const char *fmt, va_list arg); | ||
| 191 | |||
| 192 | #endif /* CHECKASM_INTERNAL_H */ | ||
| 193 |