FFmpeg
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 
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 */
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 
137 
138 /* Platform specific timing code */
140 
141 int checkasm_perf_init(void);
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 */
157 void checkasm_measure_perf_scale(CheckasmMeasurement *meas); /* ns per cycle */
158 
159 /* Miscellaneous helpers */
160 static inline int imax(const int a, const int b)
161 {
162  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 static inline void *checkasm_handle_oom(void *ptr)
171 {
172  if (!ptr) {
173  fprintf(stderr, "checkasm: out of memory\n");
174  exit(1);
175  }
176  return ptr;
177 }
178 
179 /* Allocate a zero-initialized block, clean up and exit on failure */
180 static inline void *checkasm_mallocz(const size_t size)
181 {
182  return checkasm_handle_oom(calloc(1, size));
183 }
184 
185 static inline char *checkasm_strdup(const char *str)
186 {
187  return checkasm_handle_oom(strdup(str));
188 }
189 
190 char *checkasm_vasprintf(const char *fmt, va_list arg);
191 
192 #endif /* CHECKASM_INTERNAL_H */
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:66
checkasm_interrupted
volatile sig_atomic_t checkasm_interrupted
Definition: signal.c:50
color
Definition: vf_paletteuse.c:513
CheckasmJson::level
int level
Definition: internal.h:118
checkasm_perf_validate_start
int checkasm_perf_validate_start(const CheckasmPerf *perf)
Definition: perf.c:128
checkasm_context
checkasm_jmp_buf checkasm_context
Definition: signal.c:46
checkasm_gettime_nsec
uint64_t checkasm_gettime_nsec(void)
Definition: utils.c:107
checkasm_perf_init
int checkasm_perf_init(void)
Definition: perf.c:59
b
#define b
Definition: input.c:43
checkasm_measure_nop_cycles
void checkasm_measure_nop_cycles(CheckasmMeasurement *meas, uint64_t target_cycles)
Definition: perf.c:176
CheckasmJson::file
FILE * file
Definition: internal.h:117
checkasm_perf
CheckasmPerf checkasm_perf
Definition: perf.c:52
checkasm_fprintf
void checkasm_fprintf(FILE *const f, const int color, const char *const fmt,...) CHECKASM_PRINTF(3
NORETURN
#define NORETURN
Definition: internal.h:66
CheckasmPerf
Definition: test.h:527
checkasm_gettime_nsec_diff
uint64_t checkasm_gettime_nsec_diff(uint64_t t)
Definition: utils.c:112
attributes.h
Platform and compiler attribute macros.
imax
static int imax(const int a, const int b)
Definition: internal.h:160
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
target_cycles
uint64_t target_cycles
Definition: checkasm.c:117
checkasm_perf_init_linux
int checkasm_perf_init_linux(CheckasmPerf *perf)
longjmp.h
checkasm_json
void checkasm_json(CheckasmJson *json, const char *key, const char *fmt,...) CHECKASM_PRINTF(3
checkasm_mallocz
static void * checkasm_mallocz(const size_t size)
Definition: internal.h:180
checkasm_set_signal_handlers
void checkasm_set_signal_handlers(void)
Definition: signal.c:132
checkasm_noop
void checkasm_noop(void *)
Definition: utils.c:63
CheckasmJson
Definition: internal.h:116
key
const char * key
Definition: hwcontext_opencl.c:189
stats.h
arg
const char * arg
Definition: jacosubdec.c:65
checkasm_measure_perf_scale
void checkasm_measure_perf_scale(CheckasmMeasurement *meas)
Definition: perf.c:209
imin
static int imin(const int a, const int b)
Definition: internal.h:165
seed
static unsigned int seed
Definition: videogen.c:78
checkasm_perf_init_arm
int checkasm_perf_init_arm(CheckasmPerf *perf)
Definition: arm.c:148
f
f
Definition: af_crystalizer.c:122
checkasm_strdup
static char * checkasm_strdup(const char *str)
Definition: internal.h:185
test.h
Test writing API for checkasm.
size
int size
Definition: twinvq_data.h:10344
checkasm_handle_oom
static void * checkasm_handle_oom(void *ptr)
Definition: internal.h:170
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
checkasm_setup_fprintf
void checkasm_setup_fprintf(void)
Definition: utils.c:404
checkasm_perf_validate_start_stop
int checkasm_perf_validate_start_stop(const CheckasmPerf *perf)
Definition: perf.c:150
checkasm_seed
unsigned checkasm_seed(void)
Definition: utils.c:117
CheckasmJson::nonempty
int nonempty
Definition: internal.h:119
CheckasmMeasurement
Definition: stats.h:122
checkasm_fail_abort
NORETURN void checkasm_fail_abort(const char *msg,...) CHECKASM_PRINTF(1
checkasm_jmp_buf
jmp_buf checkasm_jmp_buf
Definition: longjmp.h:66
checkasm_srand
void checkasm_srand(unsigned seed)
Definition: utils.c:127
checkasm_json_pop
void checkasm_json_pop(CheckasmJson *json, char type)
Definition: utils.c:485
checkasm_json_str
void void checkasm_json_str(CheckasmJson *json, const char *key, const char *str)
Definition: utils.c:444
checkasm_json_push
void checkasm_json_push(CheckasmJson *json, const char *const key, char type)
Definition: utils.c:469
checkasm_run_on_all_cores
int checkasm_run_on_all_cores(void(*func)(void))
Definition: checkasm.c:601
checkasm_get_last_signal_desc
const char * checkasm_get_last_signal_desc(void)
Definition: signal.c:167
checkasm_vasprintf
char * checkasm_vasprintf(const char *fmt, va_list arg)
Definition: utils.c:711
checkasm_perf_init_macos
int checkasm_perf_init_macos(CheckasmPerf *perf)
CHECKASM_PRINTF
#define CHECKASM_PRINTF(fmt, attr)
Printf-style format string checking attribute.
Definition: attributes.h:59