xref: /aosp_15_r20/external/libdav1d/src/ref.c (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
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 
default_free_callback(const uint8_t * const data,void * const user_data)32 static void default_free_callback(const uint8_t *const data, void *const user_data) {
33     assert(data == user_data);
34     dav1d_free_aligned(user_data);
35 }
36 
dav1d_ref_create(const enum AllocationType type,size_t size)37 Dav1dRef *dav1d_ref_create(const enum AllocationType type, size_t size) {
38     size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
39 
40     uint8_t *const data = dav1d_alloc_aligned(type, size + sizeof(Dav1dRef), 64);
41     if (!data) return NULL;
42 
43     Dav1dRef *const res = (Dav1dRef*)(data + size);
44     res->const_data = res->user_data = res->data = data;
45     atomic_init(&res->ref_cnt, 1);
46     res->free_ref = 0;
47     res->free_callback = default_free_callback;
48 
49     return res;
50 }
51 
pool_free_callback(const uint8_t * const data,void * const user_data)52 static void pool_free_callback(const uint8_t *const data, void *const user_data) {
53     dav1d_mem_pool_push((Dav1dMemPool*)data, user_data);
54 }
55 
dav1d_ref_create_using_pool(Dav1dMemPool * const pool,size_t size)56 Dav1dRef *dav1d_ref_create_using_pool(Dav1dMemPool *const pool, size_t size) {
57     size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
58 
59     Dav1dMemPoolBuffer *const buf =
60         dav1d_mem_pool_pop(pool, size + sizeof(Dav1dRef));
61     if (!buf) return NULL;
62 
63     Dav1dRef *const res = &((Dav1dRef*)buf)[-1];
64     res->data = buf->data;
65     res->const_data = pool;
66     atomic_init(&res->ref_cnt, 1);
67     res->free_ref = 0;
68     res->free_callback = pool_free_callback;
69     res->user_data = buf;
70 
71     return res;
72 }
73 
dav1d_ref_dec(Dav1dRef ** const pref)74 void dav1d_ref_dec(Dav1dRef **const pref) {
75     assert(pref != NULL);
76 
77     Dav1dRef *const ref = *pref;
78     if (!ref) return;
79 
80     *pref = NULL;
81     if (atomic_fetch_sub(&ref->ref_cnt, 1) == 1) {
82         const int free_ref = ref->free_ref;
83         ref->free_callback(ref->const_data, ref->user_data);
84         if (free_ref) dav1d_free(ref);
85     }
86 }
87