aboutsummaryrefslogtreecommitdiff
path: root/kernel/vm/anon.c
blob: 978ff011c875e1e8c585e3bc5004546365fa3f9d (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
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
#include "mm/mobj.h"
#include "mm/page.h"
#include "mm/pframe.h"
#include "mm/slab.h"

#include "util/debug.h"
#include "util/string.h"

/* for debugging/verification purposes */
int anon_count = 0; 

static slab_allocator_t *anon_allocator;

static long anon_fill_pframe(mobj_t *o, pframe_t *pf);

static long anon_flush_pframe(mobj_t *o, pframe_t *pf);

static void anon_destructor(mobj_t *o);

static mobj_ops_t anon_mobj_ops = {.get_pframe = NULL,
                                   .fill_pframe = anon_fill_pframe,
                                   .flush_pframe = anon_flush_pframe,
                                   .destructor = anon_destructor};

/*
 * Initialize anon_allocator using the slab allocator.
 */
void anon_init()
{
    anon_allocator = slab_allocator_create("anon", sizeof(mobj_t));
}

/*
 * The mobj should be locked upon successful return. Use mobj_init and
 * mobj_lock.
 */
mobj_t *anon_create()
{
    // make a new mobj
    mobj_t *mobj = (mobj_t *)slab_obj_alloc(anon_allocator);
    // initialize the mobj
    if (mobj)
    {
        mobj_init(mobj, MOBJ_ANON, &anon_mobj_ops);
        mobj_lock(mobj);
        return mobj;
    }
    panic("MINE anon_create: slab_obj_alloc failed");
}

/* 
 * This function is not complicated -- think about what the pframe should look
 * like for an anonymous object 
 */
static long anon_fill_pframe(mobj_t *o, pframe_t *pf)
{
    memset(pf->pf_addr, 0, PAGE_SIZE);
    return 0;
}

static long anon_flush_pframe(mobj_t *o, pframe_t *pf) { return 0; }

/*
 * Release all resources associated with an anonymous object.
 *
 * Hints:
 *  1) Call mobj_default_destructor() to free pframes
 *  2) Free the mobj
 */
static void anon_destructor(mobj_t *o)
{
    // call the default destructor
    mobj_default_destructor(o);

    // free the mobj
    slab_obj_free(anon_allocator, o);
}