Coverage Report

Created: 2026-05-16 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/ref.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include "src/ref.h"
31
32
0
static void default_free_callback(const uint8_t *const data, void *const user_data) {
33
0
    assert(data == user_data);
34
0
    dav1d_free_aligned(user_data);
35
0
}
36
37
0
Dav1dRef *dav1d_ref_create(const enum AllocationType type, size_t size) {
38
0
    size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
39
40
0
    uint8_t *const data = dav1d_alloc_aligned(type, size + sizeof(Dav1dRef), 64);
41
0
    if (!data) return NULL;
42
43
0
    Dav1dRef *const res = (Dav1dRef*)(data + size);
44
0
    res->const_data = res->user_data = res->data = data;
45
0
    atomic_init(&res->ref_cnt, 1);
46
0
    res->free_ref = 0;
47
0
    res->free_callback = default_free_callback;
48
49
0
    return res;
50
0
}
51
52
0
static void pool_free_callback(const uint8_t *const data, void *const user_data) {
53
0
    dav1d_mem_pool_push((Dav1dMemPool*)data, user_data);
54
0
}
55
56
0
Dav1dRef *dav1d_ref_create_using_pool(Dav1dMemPool *const pool, size_t size) {
57
0
    void *const buf = dav1d_mem_pool_pop(pool, size);
58
0
    if (!buf) return NULL;
59
60
    /* Store Dav1dRef inside the Dav1dMemPoolBuffer alignment padding */
61
0
    assert(sizeof(Dav1dMemPoolBuffer) + sizeof(Dav1dRef) <= 64);
62
0
    Dav1dRef *const res = &((Dav1dRef*)buf)[-1];
63
0
    res->data = buf;
64
0
    res->const_data = pool;
65
0
    atomic_init(&res->ref_cnt, 1);
66
0
    res->free_ref = 0;
67
0
    res->free_callback = pool_free_callback;
68
0
    res->user_data = buf;
69
70
0
    return res;
71
0
}
72
73
0
void dav1d_ref_dec(Dav1dRef **const pref) {
74
0
    assert(pref != NULL);
75
76
0
    Dav1dRef *const ref = *pref;
77
0
    if (!ref) return;
78
79
0
    *pref = NULL;
80
0
    if (atomic_fetch_sub(&ref->ref_cnt, 1) == 1) {
81
0
        const int free_ref = ref->free_ref;
82
0
        ref->free_callback(ref->const_data, ref->user_data);
83
0
        if (free_ref) dav1d_free(ref);
84
0
    }
85
0
}