summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/sys/link.c
blob: c8b08ec537ca7b7e44c9ff1c6a4ddd12f1e3dc96 (plain) (blame)
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
#include "sys/link.h"
#include "api/obj.h"
#include "arch/x86_64/hal/paging.h"
#include "hal/memory.h"
#include "hal/obj.h"
#include "jove.h"
#include "sys/lock.h"

int link_new(jove_owned_obj_t *target_obj, jove_owned_obj_t *untyped_obj, uint8_t permissions)
{
    jove_untyped_t *untyped = obj_deref(untyped_obj);
    if(untyped->limit != sizeof(jove_link_obj_t)) return -EBADSIZE;
    
    jove_link_obj_t *link = (jove_link_obj_t*)untyped;
    release_mutex(&link->lock);
    link->permissions = permissions;
    link->refcount = 2;
    link->obj = target_obj->locked;

    obj_setvalue(target_obj, (uintptr_t)link);
    obj_setvalue(untyped_obj, (uintptr_t)link);

    obj_settype(target_obj, JOVE_OBJ_LINK);
    obj_settype(untyped_obj, JOVE_OBJ_LINK);

    return 0;
}

int link_dup(jove_owned_obj_t *link_obj, jove_owned_obj_t *dest_obj)
{
    jove_link_obj_t *link = obj_deref(link_obj);
    obj_settype(dest_obj, JOVE_OBJ_LINK);
    obj_setvalue(dest_obj, obj_getvalue(link_obj));
    link->refcount++;
    return 0;
}

inline jove_owned_obj_t*
link_deref(jove_link_obj_t *link)
{
    return obj_take(&link->obj);
}

inline jove_owned_obj_t*
link_try_deref(jove_link_obj_t *link)
{
    return obj_trytake(&link->obj);
}

inline void link_lock(jove_link_obj_t *link)
{ take_mutex(&link->lock); }
inline void link_unlock(jove_link_obj_t *link)
{ release_mutex(&link->lock); }

inline uint8_t link_getperm(jove_link_obj_t *link)
{ return link->permissions; }
inline void link_setperm(jove_link_obj_t *link, uint8_t perm)
{ link->permissions = perm; }