1#include <uacpi/internal/shareable.h>
2
3#define BUGGED_REFCOUNT 0xFFFFFFFF
4
5void uacpi_shareable_init(uacpi_handle handle)
6{
7 struct uacpi_shareable *shareable = handle;
8 shareable->reference_count = 1;
9}
10
11uacpi_bool uacpi_bugged_shareable(uacpi_handle handle)
12{
13 struct uacpi_shareable *shareable = handle;
14
15 if (uacpi_unlikely(shareable->reference_count == 0))
16 shareable->reference_count = BUGGED_REFCOUNT;
17
18 return shareable->reference_count == BUGGED_REFCOUNT;
19}
20
21void uacpi_make_shareable_bugged(uacpi_handle handle)
22{
23 struct uacpi_shareable *shareable = handle;
24 shareable->reference_count = BUGGED_REFCOUNT;
25}
26
27uacpi_u32 uacpi_shareable_ref(uacpi_handle handle)
28{
29 struct uacpi_shareable *shareable = handle;
30
31 if (uacpi_unlikely(uacpi_bugged_shareable(shareable)))
32 return BUGGED_REFCOUNT;
33
34 return shareable->reference_count++;
35}
36
37uacpi_u32 uacpi_shareable_unref(uacpi_handle handle)
38{
39 struct uacpi_shareable *shareable = handle;
40
41 if (uacpi_unlikely(uacpi_bugged_shareable(shareable)))
42 return BUGGED_REFCOUNT;
43
44 return shareable->reference_count--;
45}
46
47void uacpi_shareable_unref_and_delete_if_last(
48 uacpi_handle handle, void (*do_free)(uacpi_handle)
49)
50{
51 if (handle == UACPI_NULL)
52 return;
53
54 if (uacpi_unlikely(uacpi_bugged_shareable(handle)))
55 return;
56
57 if (uacpi_shareable_unref(handle) == 1)
58 do_free(handle);
59}
60
61uacpi_u32 uacpi_shareable_refcount(uacpi_handle handle)
62{
63 struct uacpi_shareable *shareable = handle;
64 return shareable->reference_count;
65}
66