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