-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
142 lines (126 loc) · 3.09 KB
/
memory.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "bootpack.h"
unsigned int memtest(unsigned int start, unsigned int end)
{
char flg486 = 0;
unsigned int eflg, cr0, i;
eflg = io_load_eflags();
eflg |= EFLAGS_AC_BIT;
io_store_eflags(eflg);
eflg = io_load_eflags();
if ((eflg & EFLAGS_AC_BIT) != 0)
flg486 = 1;
eflg &= ~EFLAGS_AC_BIT;
io_store_eflags(eflg);
if (flg486 != 0)
{
cr0 = load_cr0();
cr0 |= CR0_CACHE_DISABLE;
store_cr0(cr0);
}
i = memtest_sub(start, end);
if (flg486 != 0)
{
cr0 = load_cr0();
cr0 &= ~CR0_CACHE_DISABLE;
store_cr0(cr0);
}
return i;
}
void memman_init(struct MEMMAN *man)
{
man->frees = 0;
man->maxfrees = 0;
man->lostsize = 0;
man->losts = 0;
return;
}
unsigned int memman_total(struct MEMMAN *man)
{
unsigned int i, t = 0;
for (i = 0; i < man->frees; i++)
t += man->free[i].size;
return t;
}
unsigned int memman_alloc(struct MEMMAN *man, unsigned int size)
{
unsigned int i, a;
for (i = 0; i < man->frees; i++)
{
if (man->free[i].size >= size)
{
a = man->free[i].addr;
man->free[i].addr += size;
man->free[i].size -= size;
if (man->free[i].size == 0)
{
man->frees--;
for (; i < man->frees; i++)
man->free[i] = man->free[i + 1];
}
return a;
}
}
return 0;
}
int memman_free(struct MEMMAN *man, unsigned int addr, unsigned int size)
{
int i, j;
for (i = 0; i < man->frees; i++)
if (man->free[i].addr > addr)
break;
if (i > 0)
{
if (man->free[i - 1].addr + man->free[i - 1].size == addr)
{
man->free[i - 1].size += size;
if (i < man->frees)
{
if (addr + size == man->free[i].addr)
{
man->free[i - 1].size += man->free[i].size;
man->frees--;
for (; i < man->frees; i++)
man->free[i] = man->free[i + 1];
}
}
return 0;
}
}
if (i < man->frees)
{
if (addr + size == man->free[i].addr)
{
man->free[i].addr = addr;
man->free[i].size += size;
return 0;
}
}
if (man->frees < MEMMAN_FREES)
{
for (j = man->frees; j > i; j--)
man->free[j] = man->free[j - 1];
man->frees++;
if (man->maxfrees < man->frees)
man->maxfrees = man->frees;
man->free[i].addr = addr;
man->free[i].size = size;
return 0;
}
man->losts++;
man->lostsize += size;
return -1;
}
unsigned int memman_alloc_4k(struct MEMMAN *man, unsigned int size)
{
unsigned int a;
size = (size + 0xfff) & 0xfffff000;
a = memman_alloc(man, size);
return a;
}
int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size)
{
int i;
size = (size + 0xfff) & 0xfffff000;
i = memman_free(man, addr, size);
return i;
}