-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuddy.h
55 lines (43 loc) · 1.03 KB
/
buddy.h
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
#ifndef BUDDY_SYSTEM
#define BUDDY_SYSTEM
#include <list>
#include <vector>
using std::list;
using std::vector;
class struct_page
{
public:
int users = 0; //页用户数量
};
class page_block
{
public:
int order;
bool valid; /* 是否可使用 */
struct_page* begin; /* 第一个页 */
void show(const struct_page* const begin);
};
class buddySystem
{
public:
// ctor and dtor
buddySystem(unsigned int max_order);
~buddySystem();
// 分配页块
page_block alloc_pages(const unsigned int order);
// 销毁页块,如果block存在空闲的伙伴,则融合后递归销毁新的页块
void free(page_block& block);
// 打印空闲页块情况到stdout
void show();
const struct_page* const get_memmap()
{
return mem_map;
}
private:
struct_page* mem_map;
vector<list<page_block>> flist;
unsigned int max_order;
// 查找buddy
list<page_block>::iterator find_buddy(const page_block& block);
};
#endif