-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandover.c
163 lines (135 loc) · 3.76 KB
/
handover.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#define HANDOVER_INCLUDE_UTILITES
#include "handover.h"
char const *handover_tag_name(HandoverTag tag)
{
switch (tag)
{
#define TAG(NAME, VALUE) \
case HANDOVER_##NAME: \
return #NAME;
HANDOVER_TAGS(TAG)
#undef TAG
}
return "UNKNOWN";
}
bool handover_mergeable(uint32_t tag)
{
switch (tag)
{
case HANDOVER_FREE:
case HANDOVER_LOADER:
case HANDOVER_KERNEL:
case HANDOVER_RESERVED:
return true;
default:
return false;
}
}
bool handover_overlap(HandoverRecord lhs, HandoverRecord rhs)
{
return lhs.start < rhs.start + rhs.size && rhs.start < lhs.start + lhs.size;
}
bool handover_just_before(HandoverRecord lhs, HandoverRecord rhs)
{
return lhs.start + lhs.size == rhs.start;
}
bool handover_just_after(HandoverRecord lhs, HandoverRecord rhs)
{
return lhs.start == rhs.start + rhs.size;
}
HandoverRecord handover_half_under(HandoverRecord self, HandoverRecord other)
{
if (handover_overlap(self, other) &&
self.start < other.start)
{
return (HandoverRecord){
.tag = self.tag,
.flags = 0,
.start = self.start,
.size = other.start - self.start,
};
}
return (HandoverRecord){};
}
HandoverRecord handover_half_over(HandoverRecord self, HandoverRecord other)
{
if (handover_overlap(self, other) &&
self.start + self.size > other.start + other.size)
{
return (HandoverRecord){
.tag = self.tag,
.flags = 0,
.start = other.start + other.size,
.size = self.start + self.size - other.start - other.size,
};
}
return (HandoverRecord){};
}
void handover_insert(HandoverPayload *payload, size_t index, HandoverRecord record)
{
for (size_t i = payload->count; i > index; i--)
{
payload->records[i] = payload->records[i - 1];
}
payload->records[index] = record;
payload->count++;
}
void handover_remove(HandoverPayload *payload, size_t index)
{
for (size_t i = index; i < payload->count - 1; i++)
{
payload->records[i] = payload->records[i + 1];
}
payload->count--;
}
void handover_append(HandoverPayload *payload, HandoverRecord record)
{
if (record.size == 0)
return;
for (size_t i = 0; i < payload->count; i++)
{
HandoverRecord *other = &payload->records[i];
if (record.tag == other->tag &&
handover_just_after(record, *other) &&
handover_mergeable(record.tag))
{
other->size += record.size;
return;
}
if (record.tag == other->tag &&
handover_just_before(record, *other) &&
handover_mergeable(record.tag))
{
other->start -= record.size;
other->size += record.size;
return;
}
if (handover_overlap(record, *other))
{
if (!handover_mergeable(record.tag))
{
HandoverRecord tmp = record;
record = *other;
*other = tmp;
}
HandoverRecord half_under = handover_half_under(record, *other);
HandoverRecord half_over = handover_half_over(record, *other);
handover_remove(payload, i);
if (half_under.size)
handover_insert(payload, i, half_under);
if (half_over.size)
handover_insert(payload, i, half_over);
return;
}
if (record.start < other->start)
{
handover_insert(payload, i, record);
return;
}
}
payload->records[payload->count++] = record;
}
char const *handover_str(HandoverPayload const *payload, uint32_t offset)
{
return (char const *)payload + offset;
}