summaryrefslogtreecommitdiffstats
path: root/sys/objdir.c
blob: df4b3ebc46b098c97a71493a534df6cee71a8da2 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "api/error.h"
#include "arch/x86_64/hal/obj.h"
#include "hal/memory.h"
#include "hal/obj.h"
#include "lib/string.h"
#include "jove.h"

int
objdir_new(jove_owned_obj_t *untyped_obj)
{
    jove_untyped_t *untyped = obj_deref(untyped_obj);
    if((uintptr_t)untyped & (OBJDIR_SIZE - 1)) return -EALIGN;
    if(untyped->limit != OBJDIR_SIZE) return -EBADSIZE;

    memset(untyped, 0, OBJDIR_SIZE);
    obj_settype(untyped_obj, JOVE_OBJ_OBJDIR);
    return 0;
}

jove_obj_t
objdir_peeklocal(
        jove_obj_t *objdir_obj, 
        uint16_t entry)
{
    jove_owned_obj_t *objdir = obj_take(objdir_obj);
    if(obj_gettype(objdir) != JOVE_OBJ_OBJDIR) {
        errno = EBADOBJ;
        obj_release(objdir);
        return (jove_obj_t) { 0 };
    }
    if(entry >= OBJDIR_ENTRIES) {
        errno = EBADPATH;
        obj_release(objdir);
        return (jove_obj_t) { 0 };
    }

    jove_objdir_t *objtab = obj_deref(objdir);
    jove_obj_t obj = objtab->entries[entry];
    obj_release(objdir);
    return obj;
}

jove_owned_obj_t*
objdir_getlocal(
        jove_obj_t *objdir_obj, 
        uint16_t entry)
{
    jove_owned_obj_t *objdir = obj_take(objdir_obj);
    jove_owned_obj_t *obj = objdir_getlocal_locked(objdir, entry);
    obj_release(objdir);
    return obj;
}

jove_owned_obj_t *objdir_getlocal_locked(jove_owned_obj_t *objdir, objpath_part_t entry)
{
    if(obj_gettype(objdir) != JOVE_OBJ_OBJDIR) {
        obj_release(objdir);
        reterr(EBADOBJ);
    }
    if(entry >= OBJDIR_ENTRIES) {
        obj_release(objdir);
        reterr(EBADPATH);
    }

    jove_objdir_t *objtab = obj_deref(objdir);
    jove_owned_obj_t *obj = obj_take(&objtab->entries[entry]);
    return obj;
}

jove_owned_obj_t*
objdir_getobj(
        jove_obj_t *objdir_obj, 
        const uint16_t *path, 
        size_t path_len)
{
    jove_owned_obj_t *objdir = obj_take(objdir_obj);
    jove_owned_obj_t *obj = objdir_getobj_locked(objdir, path, path_len);
    obj_release(objdir);
    return obj;

}

jove_owned_obj_t *objdir_getobj_locked(jove_owned_obj_t *objdir,
                                       const objpath_part_t *path,
                                       size_t path_len)
{
    errno = 0;
    if(path_len == 0) return objdir;

    jove_owned_obj_t *child_obj = objdir_getlocal_locked(objdir, path[0]);
    obj_release(objdir);

    if(child_obj == NULL) return NULL;
    return objdir_getobj_locked(child_obj, &path[1], path_len - 1);
}

bool
objdir_compare(jove_obj_t *objdir_obj, objpath_part_t entry, jove_obj_t *obj)
{
    jove_obj_t peekobj = objdir_peeklocal(objdir_obj, entry);
    return obj_compare(&peekobj, obj);
}

void
objdir_swapobj(jove_owned_obj_t *a, jove_owned_obj_t *b)
{
    jove_owned_obj_t buffer_obj = *a;
    *a = *b;
    *b = buffer_obj;
}