FFmpeg
Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
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
95void checkasm_srand(unsigned seed);
96
97/* Internal variant of checkasm_fail_func() that also jumps back to the signal
98 * handler */
99NORETURN 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 */
111void checkasm_setup_fprintf(void);
112int checkasm_vfprintf(FILE *const f, int color, const char *fmt, va_list arg)
113 CHECKASM_PRINTF(3, 0);
114
115CHECKASM_PRINTF(3, 4)
116static inline int checkasm_fprintf(FILE *const f, int color, const char *fmt, ...)
117{
118 va_list ap;
119 va_start(ap, fmt);
120 int ret = checkasm_vfprintf(f, color, fmt, ap);
121 va_end(ap);
122 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 */
130void checkasm_statusline(const char *status);
131
132/* Light-weight helper for printing nested JSON objects */
133typedef struct CheckasmJson {
134 FILE *file;
135 int level;
138
139void checkasm_json(CheckasmJson *json, const char *key, const char *fmt, ...)
140 CHECKASM_PRINTF(3, 4);
141void checkasm_json_str(CheckasmJson *json, const char *key, const char *str);
142void checkasm_json_push(CheckasmJson *json, const char *const key, char type);
143void checkasm_json_pop(CheckasmJson *json, char type);
144
145/* Platform specific signal handling */
147const 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. */
151extern volatile sig_atomic_t checkasm_interrupted;
152
154
155/* Platform specific timing code */
157
158int checkasm_perf_init(void);
164
165int checkasm_run_on_all_cores(void (*func)(void));
166
167uint64_t checkasm_gettime_nsec(void);
168uint64_t checkasm_gettime_nsec_diff(uint64_t t); /* subtracts t */
169unsigned checkasm_seed(void);
170void checkasm_noop(void *);
171
172/* These functions update the measurements in `meas` directly; must be initialized */
174void checkasm_measure_perf_scale(CheckasmMeasurement *meas); /* ns per cycle */
175
176/* Miscellaneous helpers */
177static inline int imax(const int a, const int b)
178{
179 return a > b ? a : b;
180}
181
182static inline int imin(const int a, const int b)
183{
184 return a < b ? a : b;
185}
186
187static inline void *checkasm_handle_oom(void *ptr)
188{
189 if (!ptr) {
190 fprintf(stderr, "checkasm: out of memory\n");
191 exit(1);
192 }
193 return ptr;
194}
195
196/* Allocate a zero-initialized block, clean up and exit on failure */
197static inline void *checkasm_mallocz(const size_t size)
198{
199 return checkasm_handle_oom(calloc(1, size));
200}
201
202static inline char *checkasm_strdup(const char *str)
203{
204 return checkasm_handle_oom(strdup(str));
205}
206
207char *checkasm_vasprintf(const char *fmt, va_list arg);
208
209#endif /* CHECKASM_INTERNAL_H */
#define f(width, name)
Definition cbs_vp8.c:236
uint64_t target_cycles
Definition checkasm.c:118
int checkasm_run_on_all_cores(void(*func)(void))
Definition checkasm.c:625
const char * key
int a
cl_device_type type
#define b
Definition input.c:43
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:66
const char * arg
Definition jacosubdec.c:65
jmp_buf checkasm_jmp_buf
Definition longjmp.h:66
FILE * file
Definition internal.h:134
Test writing API for checkasm.
Platform and compiler attribute macros.
#define CHECKASM_PRINTF(fmt, attr)
Printf-style format string checking attribute.
Definition attributes.h:59
static char * checkasm_strdup(const char *str)
Definition internal.h:202
uint64_t checkasm_gettime_nsec_diff(uint64_t t)
Definition utils.c:112
static void * checkasm_mallocz(const size_t size)
Definition internal.h:197
int checkasm_perf_init(void)
Definition perf.c:59
static int imin(const int a, const int b)
Definition internal.h:182
void checkasm_srand(unsigned seed)
Definition utils.c:229
uint64_t checkasm_gettime_nsec(void)
Definition utils.c:107
void checkasm_json(CheckasmJson *json, const char *key, const char *fmt,...) CHECKASM_PRINTF(3
int checkasm_perf_init_macos(CheckasmPerf *perf)
int checkasm_vfprintf(FILE *const f, int color, const char *fmt, va_list arg) CHECKASM_PRINTF(3
void checkasm_set_signal_handlers(void)
Definition signal.c:132
void checkasm_noop(void *)
Definition utils.c:63
checkasm_jmp_buf checkasm_context
Definition signal.c:46
int checkasm_perf_init_linux(CheckasmPerf *perf)
static int imax(const int a, const int b)
Definition internal.h:177
const char * checkasm_get_last_signal_desc(void)
Definition signal.c:167
void checkasm_json_pop(CheckasmJson *json, char type)
Definition utils.c:627
void checkasm_statusline(const char *status)
Definition utils.c:501
unsigned checkasm_seed(void)
Definition utils.c:117
int checkasm_perf_validate_start_stop(const CheckasmPerf *perf)
Definition perf.c:150
#define NORETURN
Definition internal.h:66
void checkasm_json_push(CheckasmJson *json, const char *const key, char type)
Definition utils.c:611
NORETURN void checkasm_fail_abort(const char *msg,...) CHECKASM_PRINTF(1
void checkasm_setup_fprintf(void)
Definition utils.c:544
static void * checkasm_handle_oom(void *ptr)
Definition internal.h:187
int static int checkasm_fprintf(FILE *const f, int color, const char *fmt,...)
Definition internal.h:116
char * checkasm_vasprintf(const char *fmt, va_list arg)
Definition utils.c:853
CheckasmPerf checkasm_perf
Definition perf.c:52
void void checkasm_json_str(CheckasmJson *json, const char *key, const char *str)
Definition utils.c:586
void checkasm_measure_nop_cycles(CheckasmMeasurement *meas, uint64_t target_cycles)
Definition perf.c:176
int checkasm_perf_init_arm(CheckasmPerf *perf)
Definition arm.c:148
volatile sig_atomic_t checkasm_interrupted
Definition signal.c:50
void checkasm_measure_perf_scale(CheckasmMeasurement *meas)
Definition perf.c:209
int checkasm_perf_validate_start(const CheckasmPerf *perf)
Definition perf.c:128
int size
static unsigned int seed
Definition videogen.c:78