-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwref.h
65 lines (51 loc) · 1.22 KB
/
wref.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// (c) 2020 shdown
// This code is licensed under MIT license (see LICENSE.MIT for details)
#pragma once
#include "common.h"
#include "vm.h"
struct WeakRefable;
typedef struct WeakRef {
GcHeader gc_hdr;
struct WeakRefable *target;
struct WeakRef *next;
struct WeakRef *prev;
} WeakRef;
typedef struct WeakRefable {
GcHeader gc_hdr;
WeakRef *wref_first;
} WeakRefable;
UU_INHEADER void wrefable_invalidate(WeakRefable *x)
{
for (WeakRef *w = x->wref_first; w; w = w->next)
w->target = NULL;
}
UU_INHEADER WeakRef *wref_new(WeakRefable *target)
{
WeakRef *old_first = target->wref_first;
WeakRef *w = uu_xmalloc(sizeof(WeakRef), 1);
*w = (WeakRef) {
.gc_hdr = {.nrefs = 1, .kind = VK_WREF},
.target = target,
.next = old_first,
.prev = NULL,
};
if (old_first)
old_first->prev = w;
target->wref_first = w;
return w;
}
UU_INHEADER void wref_destroy(WeakRef *w)
{
WeakRef *prev = w->prev;
WeakRef *next = w->next;
if (prev) {
prev->next = next;
} else {
WeakRefable *target = w->target;
if (target)
target->wref_first = next;
}
if (next)
next->prev = prev;
free(w);
}