aboutsummaryrefslogtreecommitdiff
path: root/include/kpl/allocate.h
blob: ee24d72755a089eaf440421309fae8f12a0df058 (plain)
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
/*
 * KPL object allocators helpers
 */

#pragma once

/*
 * All allocations must extend the KPL_OBJECT_HEADER
 * All objects are allocated through this
 */
inline void *kpl_object_allocate(kpl_thread *thread, int32_t size) {
    kpl_object *obj = kpl_memory_allocator_allocate(&thread->memory_allocator, size, thread->thread_id);
    if (!obj)
        return NULL;
    obj->object_flags = KPL_OBJECT_FLAGS_EMPTY;
    __atomic_clear(&obj->gc_mark, __ATOMIC_RELAXED);
    obj->gc_prev = obj->gc_next = NULL;
    obj->gc_next = thread->gc_head;
    if (thread->gc_head)
        thread->gc_head->gc_prev = obj;
    thread->gc_head = obj;
    return obj;
}

/*
 * All objects are returned by this
 */
inline void kpl_object_return(kpl_env *env, void *object) {
    kpl_object *obj = object;
    if (obj->gc_prev)
        obj->gc_prev->gc_next = obj->gc_next;
    if (obj->gc_next)
        obj->gc_next->gc_prev = obj->gc_prev;
    kpl_memory_allocator_return(&env->threads[obj->thread_id].memory_allocator, object);
}