blob: 46462b4c801de1e1dd773e28eb466e7e959d5c88 [file] [log] [blame]
Kaido Kert612c0202020-01-22 10:28:42 -08001/*
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
Kaido Kert612c0202020-01-22 10:28:42 -080030#include "src/ref.h"
31
Kaido Kert612c0202020-01-22 10:28:42 -080032static 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
Kaido Kert788710a2023-06-05 07:50:22 -070037Dav1dRef *dav1d_ref_create(size_t size) {
38 size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
Kaido Kert612c0202020-01-22 10:28:42 -080039
Kaido Kert788710a2023-06-05 07:50:22 -070040 uint8_t *const data = dav1d_alloc_aligned(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
52static void pool_free_callback(const uint8_t *const data, void *const user_data) {
53 dav1d_mem_pool_push((Dav1dMemPool*)data, user_data);
54}
55
56Dav1dRef *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;
Kaido Kert612c0202020-01-22 10:28:42 -080070
71 return res;
72}
73
74Dav1dRef *dav1d_ref_wrap(const uint8_t *const ptr,
75 void (*free_callback)(const uint8_t *data, void *user_data),
Kaido Kert788710a2023-06-05 07:50:22 -070076 void *const user_data)
Kaido Kert612c0202020-01-22 10:28:42 -080077{
78 Dav1dRef *res = malloc(sizeof(Dav1dRef));
79 if (!res) return NULL;
80
81 res->data = NULL;
82 res->const_data = ptr;
83 atomic_init(&res->ref_cnt, 1);
Kaido Kert788710a2023-06-05 07:50:22 -070084 res->free_ref = 1;
Kaido Kert612c0202020-01-22 10:28:42 -080085 res->free_callback = free_callback;
86 res->user_data = user_data;
87
88 return res;
89}
90
Kaido Kert612c0202020-01-22 10:28:42 -080091void dav1d_ref_dec(Dav1dRef **const pref) {
92 assert(pref != NULL);
93
94 Dav1dRef *const ref = *pref;
95 if (!ref) return;
96
Kaido Kert612c0202020-01-22 10:28:42 -080097 *pref = NULL;
Kaido Kert788710a2023-06-05 07:50:22 -070098 if (atomic_fetch_sub(&ref->ref_cnt, 1) == 1) {
99 const int free_ref = ref->free_ref;
100 ref->free_callback(ref->const_data, ref->user_data);
101 if (free_ref) free(ref);
102 }
Kaido Kert612c0202020-01-22 10:28:42 -0800103}
104
105int dav1d_ref_is_writable(Dav1dRef *const ref) {
106 return atomic_load(&ref->ref_cnt) == 1 && ref->data;
107}