/
KMalloc.cpp
90 lines (55 loc) · 1.63 KB
/
KMalloc.cpp
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
#include <mm/KMalloc.h>
#include <mm/LibAlloc.h>
#include <mm/PMalloc.h>
#include <system/func/kprintf.h>
#include <mt/synchronization/Spinlock.h>
#include <mt/synchronization/Mutex.h>
#include <cpputil/Unused.h>
#include <interrupt/IState.h>
#include <stddef.h>
#include <stdint.h>
MT::Synchronization::Spinlock :: Spinlock_t liballoc_spinlock = MT::Synchronization::Spinlock :: Initializer ();
MT::Synchronization::Mutex :: Mutex_t liballoc_mutex = MT::Synchronization::Mutex :: Initializer ();
bool liballoc_lockmode = false;
bool liballoc_reint = false;
void liballoc_switchlockmode ()
{
liballoc_reint = Interrupt::IState :: ReadAndSetBlock ();
MT::Synchronization::Spinlock :: SpinAcquire ( & liballoc_spinlock );
liballoc_lockmode = true;
Interrupt::IState :: WriteBlock ( liballoc_reint );
};
int liballoc_lock ()
{
if ( liballoc_lockmode )
MT::Synchronization::Mutex :: Acquire ( & liballoc_mutex );
else
{
liballoc_reint = Interrupt::IState :: ReadAndSetBlock ();
MT::Synchronization::Spinlock :: SpinAcquire ( & liballoc_spinlock );
}
return 0;
};
int liballoc_unlock ()
{
if ( liballoc_lockmode && ( ! liballoc_reint ) )
MT::Synchronization::Mutex :: Release ( & liballoc_mutex );
else
{
MT::Synchronization::Spinlock :: Release ( & liballoc_spinlock );
Interrupt::IState :: WriteBlock ( liballoc_reint );
}
return 0;
};
void * liballoc_alloc ( size_t Pages )
{
void * Addr = mm_pmalloc ( static_cast <uint32_t> ( Pages ) );
return Addr;
};
int liballoc_free ( void * Address, size_t Length )
{
UNUSED ( Length );
mm_pfree ( Address );
return 0;
};
#include "LibAlloc.c"