summaryrefslogtreecommitdiff
path: root/Source/Library/Common/Heap.class.h
blob: f5895c78dcc289386aa686edfb7d008e4b85eab1 (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
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
91
92
#ifndef DEF_HEAP_CLASS_H
#define DEF_HEAP_CLASS_H

#include <common.h>
#include <Mutex.class.h>

//Heap minimum size : 2M
#define HEAP_MIN_SIZE 0x00200000
//Heap magic number, for verifications
#define HEAP_MAGIC 0xBEEF1337

struct heap_header_t {
	u32int magic;
	bool is_hole;
	u32int size;
};

struct heap_footer_t {
	u32int magic;
	heap_header_t *header;
};

struct heap_index_t {
	heap_header_t **data;
	u32int size;
};

#ifdef THIS_IS_MELON_KERNEL
class PageDirectory;
#else
#include <Binding/Process.class.h>
#endif

class Heap {
	private:
	u32int m_free, m_start, m_end;
	bool m_usable;
	heap_index_t m_index;
#ifdef THIS_IS_MELON_KERNEL
	bool m_user, m_rw;
	PageDirectory* m_pagedir;
#else
	Process m_process;
#endif

	Mutex m_mutex;

	void insertIntoIndex(heap_header_t *e);
	u32int findIndexEntry(heap_header_t *e);
	void removeFromIndex(u32int idx);
	void removeFromIndex(heap_header_t *e);

	void expand(u32int quantity);
	void contract();	//Quantity is automatically calculated
	
	public:
	Heap();
	~Heap();

#ifdef THIS_IS_MELON_KERNEL
	void create(u32int start, u32int size, u32int idxsize, PageDirectory* pagedir, bool user, bool rw);
#else
	void create(u32int start, u32int size, u32int idxsize);
#endif

	void* alloc(u32int sz, bool no_expand = false);
	void free(void* ptr);

	bool usable() { 
		m_mutex.waitLock();
		bool ret = m_usable;
		m_mutex.unlock();
		return ret;
	}

	u32int size() {
		m_mutex.waitLock();
		u32int ret = m_end - m_start;
		m_mutex.unlock();
		return ret;
	}

	u32int free() {
		m_mutex.waitLock();
		u32int ret = m_free;
		m_mutex.unlock();
	   	return ret;
	}
};


#endif