FFmpeg
Loading...
Searching...
No Matches
function.c
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#include <stdio.h>
31#include <stdlib.h>
32
33#include "function.h"
34#include "internal.h"
35
36/* Deallocate a tree */
37static void func_uninit(CheckasmFunc *const f)
38{
39 if (!f)
40 return;
41
42 CheckasmFuncVersion *v = f->versions.next;
43 while (v) {
44 CheckasmFuncVersion *next = v->next;
45 free(v->suffix);
46 free(v);
47 v = next;
48 }
49
50 CheckasmFunc *const left = f->child[0];
51 CheckasmFunc *const right = f->child[1];
52 free(f->report_name);
53 free(f);
54
55 func_uninit(right);
57}
58
60{
61 func_uninit(tree->root);
62 memset(tree, 0, sizeof(*tree));
63}
64
65#define is_digit(x) ((x) >= '0' && (x) <= '9')
66
67/* ASCIIbetical sort except preserving natural order for numbers */
68static int cmp_func_names(const char *a, const char *b)
69{
70 const char *const start = a;
71
72 int ascii_diff, digit_diff;
73 for (; !(ascii_diff = *(const unsigned char *) a - *(const unsigned char *) b) && *a;
74 a++, b++)
75 ;
76 for (; is_digit(*a) && is_digit(*b); a++, b++)
77 ;
78
79 if (a > start && is_digit(a[-1]) && (digit_diff = is_digit(*a) - is_digit(*b)))
80 return digit_diff;
81
82 return ascii_diff;
83}
84
85/* Perform a tree rotation in the specified direction and return the new root */
86static CheckasmFunc *tree_rotate(CheckasmFunc *const f, const int dir)
87{
88 CheckasmFunc *const r = f->child[dir ^ 1];
89
90 f->child[dir ^ 1] = r->child[dir];
91 r->child[dir] = f;
92 r->color = f->color;
93 f->color = 0;
94 return r;
95}
96
97#define is_red(f) ((f) && !(f)->color)
98
99/* Balance a left-leaning red-black tree at the specified node */
100static void tree_balance(CheckasmFunc **const root)
101{
102 CheckasmFunc *const f = *root;
103
104 if (is_red(f->child[0]) && is_red(f->child[1])) {
105 f->color ^= 1;
106 f->child[0]->color = f->child[1]->color = 1;
107 } else if (!is_red(f->child[0]) && is_red(f->child[1]))
108 *root = tree_rotate(f, 0); /* Rotate left */
109 else if (is_red(f->child[0]) && is_red(f->child[0]->child[0]))
110 *root = tree_rotate(f, 1); /* Rotate right */
111}
112
113/* Get a node with the specified name, creating it if it doesn't exist; returns
114 * 1 if a new node was inserted, 0 otherwise. */
115static int func_get(CheckasmFunc **const root, const char *const name,
116 CheckasmFunc **const out_func)
117{
118 CheckasmFunc *f = *root;
119 if (!f) {
120 /* Allocate and insert a new node into the tree */
121 const size_t name_length = strlen(name) + 1;
122 f = checkasm_mallocz(offsetof(CheckasmFunc, name) + name_length);
123 memcpy(f->name, name, name_length);
124 *out_func = *root = f;
125 return 1;
126 }
127
128 /* Search the tree for a matching node */
129 const int cmp = cmp_func_names(name, f->name);
130 if (!cmp) {
131 *out_func = f;
132 return 0;
133 }
134
135 int inserted = func_get(&f->child[cmp > 0], name, out_func);
136 if (inserted)
137 tree_balance(root); /* Rebalance the tree on the way up */
138 return inserted;
139}
140
142{
144 int inserted = func_get(&tree->root, name, &func);
145 if (inserted)
146 tree->root->color = 1; /* Ensure root is black */
147 return func;
148}
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define f(width, name)
Definition cbs_vp8.c:236
#define NULL
Definition coverity.c:32
CheckasmFuncTree tree
Definition checkasm.c:79
static int cmp_func_names(const char *a, const char *b)
Definition function.c:68
void checkasm_func_tree_uninit(CheckasmFuncTree *tree)
Definition function.c:59
#define is_digit(x)
Definition function.c:65
CheckasmFunc * checkasm_func_get(CheckasmFuncTree *tree, const char *const name)
Definition function.c:141
static CheckasmFunc * tree_rotate(CheckasmFunc *const f, const int dir)
Definition function.c:86
static void func_uninit(CheckasmFunc *const f)
Definition function.c:37
#define is_red(f)
Definition function.c:97
static int func_get(CheckasmFunc **const root, const char *const name, CheckasmFunc **const out_func)
Definition function.c:115
static void tree_balance(CheckasmFunc **const root)
Definition function.c:100
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:66
static av_always_inline int cmp(MPVEncContext *const s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
Definition motion_est.c:263
const char * name
Definition qsvenc.c:142
struct CheckasmFuncVersion * next
Definition function.h:42
static void * checkasm_mallocz(const size_t size)
Definition internal.h:197