hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
weak.cpp
Go to the documentation of this file.
1//
2// Created by XIaokang00010 on 2026/7/20.
3//
4
5#include <mimalloc/include/mimalloc.h>
6#include "weak.h"
7
8WeakSlot *runtime_weak_slot_alloc(void *target, WeakSlot **weak_slots_head_ptr) {
9 auto *slot = static_cast<WeakSlot *>(mi_calloc(1, sizeof(WeakSlot)));
10 slot->target = target;
11 // Prepend to target's linked list
12 slot->next_in_target = *weak_slots_head_ptr;
13 *weak_slots_head_ptr = slot;
14 return slot;
15}
16
17void runtime_weak_slot_free(WeakSlot *slot, WeakSlot **weak_slots_head_ptr) {
18 if (!slot) return;
19 // Unlink from target's linked list
20 if (*weak_slots_head_ptr == slot) {
21 *weak_slots_head_ptr = slot->next_in_target;
22 } else {
23 auto *cur = *weak_slots_head_ptr;
24 while (cur && cur->next_in_target != slot) cur = cur->next_in_target;
25 if (cur) cur->next_in_target = slot->next_in_target;
26 }
27 mi_free(slot);
28}
29
30void runtime_weak_slot_nullify_all(WeakSlot **weak_slots_head_ptr) {
31 auto *slot = *weak_slots_head_ptr;
32 while (slot) {
33 slot->target = nullptr;
34 auto *next = slot->next_in_target;
35 slot->next_in_target = nullptr;
36 slot = next;
37 }
38 *weak_slots_head_ptr = nullptr;
39}
void * target
Definition weak.h:11
WeakSlot * next_in_target
Definition weak.h:12
WeakSlot * runtime_weak_slot_alloc(void *target, WeakSlot **weak_slots_head_ptr)
Definition weak.cpp:8
void runtime_weak_slot_nullify_all(WeakSlot **weak_slots_head_ptr)
Definition weak.cpp:30
void runtime_weak_slot_free(WeakSlot *slot, WeakSlot **weak_slots_head_ptr)
Definition weak.cpp:17