summaryrefslogtreecommitdiffstats
path: root/hal/shmem.c
blob: 2928a1b00b693559c055467672f3a400476ce4ca (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
#include "api/error.h"
#include "hal/obj.h"
#include "lib/string.h"
#include "memory.h"

int 
shmem_new(jove_owned_obj_t *untyped_obj)
{
    jove_untyped_t *untyped = obj_deref(untyped_obj);
    if((uintptr_t)untyped & 0xFFF) return -EALIGN;
    if(untyped->limit & 0xFFF) return -EBADSIZE;

    size_t limit_pages = untyped->limit >> 12;
    if(limit_pages > JOVE_SMHEM_MAXPAGES) return -ETOOBIG;

    obj_setvalue(untyped_obj, (uintptr_t)untyped | limit_pages);
    obj_settype(untyped_obj, JOVE_OBJ_SHMEMORY);
    memset(untyped, 0, untyped->limit);
    return 0;
}

inline size_t 
shmem_numpages(jove_owned_obj_t *shmem_obj)
{
    return obj_getvalue(shmem_obj) & JOVE_SMHEM_MAXPAGES;
}

inline void*
shmem_deref(jove_owned_obj_t *shmem_obj)
{
    return phys_tovirt(obj_getvalue(shmem_obj) & ~JOVE_SMHEM_MAXPAGES);
}