aboutsummaryrefslogtreecommitdiff
path: root/kernel/mm/pframe.c
blob: 6eff123f97cef659e8e7d4c2ee47630a941f1d2a (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
#include "globals.h"

#include "mm/pframe.h"
#include "mm/slab.h"

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

static slab_allocator_t *pframe_allocator;

void pframe_init()
{
    pframe_allocator = slab_allocator_create("pframe", sizeof(pframe_t));
    KASSERT(pframe_allocator);
}

/*
 * Create a pframe and initialize its members appropriately.
 */
pframe_t *pframe_create()
{
    pframe_t *pf = slab_obj_alloc(pframe_allocator);
    if (!pf)
    {
        return NULL;
    }
    memset(pf, 0, sizeof(pframe_t));
    kmutex_init(&pf->pf_mutex);
    list_link_init(&pf->pf_link);
    return pf;
}

/*
 * Free the pframe (don't forget to unlock the mutex) and set *pfp = NULL
 *
 * The pframe must be locked, its contents not in memory (pf->pf_addr == NULL),
 * have a pincount of 0, and not be linked into a memory object's list.
 */
void pframe_free(pframe_t **pfp)
{
    KASSERT(kmutex_owns_mutex(&(*pfp)->pf_mutex));
    KASSERT(!(*pfp)->pf_addr);
    KASSERT(!(*pfp)->pf_dirty);
    KASSERT(!list_link_is_linked(&(*pfp)->pf_link));
    kmutex_unlock(&(*pfp)->pf_mutex);
    slab_obj_free(pframe_allocator, *pfp);
    *pfp = NULL;
}

/*
 * Unlock the pframe and set *pfp = NULL
 */
void pframe_release(pframe_t **pfp)
{
    pframe_t *pf = *pfp;
    KASSERT(kmutex_owns_mutex(&pf->pf_mutex));
    *pfp = NULL;
    kmutex_unlock(&pf->pf_mutex);
}