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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#include <stdlib.h>
#include <gran/mem/ideal_alloc.h>
struct region {
uint64_t start, end;
};
static inline int region_cmp(struct region a, struct region b)
{
if (a.start == b.start && a.end == b.end)
return 0;
if (a.start > b.start)
return 1;
if (a.end < b.end)
return -1;
return 0;
}
#define SPTREE_NAME regions
#define SPTREE_TYPE struct region
#define SPTREE_CMP(a, b) region_cmp((a), (b))
#include <conts/sptree.h>
struct space {
struct regions regions;
};
static struct space create_space()
{
return (struct space){.regions = regions_create()};
}
static void destroy_space(struct space *space)
{
regions_destroy(&space->regions);
}
#define VEC_NAME spaces
#define VEC_TYPE struct space
#include <conts/vec.h>
struct alloc {
struct component component;
struct spaces spaces;
uint64_t max_spaces;
struct reg in;
struct reg out;
struct component *send;
};
static void alloc_destroy(struct alloc *a)
{
foreach(spaces, s, &a->spaces) {
destroy_space(s);
}
spaces_destroy(&a->spaces);
free(a);
}
static stat alloc_receive(struct alloc *a, struct component *from,
struct packet pkt)
{
(void)from; /* unused */
if (a->in.busy)
return EBUSY;
a->in.pkt = pkt;
a->in.busy = true;
return OK;
}
static stat alloc_resp(struct alloc *a, enum alloc_if_ret resp, uint64_t start,
uint64_t end)
{
a->in.busy = false;
struct packet pkt = response(a->in.pkt);
struct alloc_if_r *r = (struct alloc_if_r *)&pkt.data;
r->r = resp;
r->start = start;
r->end = end;
stat ret = SEND(a, a->send, pkt);
if (ret == EBUSY) {
a->out.pkt = pkt;
a->out.busy = true;
return OK;
}
return ret;
}
static stat alloc_new(struct alloc *a)
{
struct alloc_if_new *n = (struct alloc_if_new *)&a->in.pkt.data;
if (n->space >= a->max_spaces)
return alloc_resp(a, ALLOC_IF_ERR_SPACE, 0, 0);
struct space *space = spaces_at(&a->spaces, n->space);
assert(space);
struct region new = {.start = n->start, .end = n->end};
struct region *region = regions_insert(&space->regions, new);
if (!region) {
alloc_resp(a, ALLOC_IF_ERR_RANGE, 0, 0);
return EMEM;
}
return alloc_resp(a, ALLOC_IF_OK, n->start, n->end);
}
static stat alloc_rm(struct alloc *a)
{
struct alloc_if_rm *rm = (struct alloc_if_rm *)&a->in.pkt.data;
if (rm->space >= a->max_spaces)
return alloc_resp(a, ALLOC_IF_ERR_SPACE, 0, 0);
struct space *space = spaces_at(&a->spaces, rm->space);
assert(space);
struct region find = {.start = rm->start, .end = rm->end};
struct region *region = regions_find(&space->regions, find);
if (!region)
return alloc_resp(a, ALLOC_IF_ERR_RANGE, 0, 0);
regions_remove_found(&space->regions, region);
regions_free_found(&space->regions, region);
return alloc_resp(a, ALLOC_IF_OK, 0, 0);
}
static stat alloc_q(struct alloc *a)
{
struct alloc_if_q *q = (struct alloc_if_q *)&a->in.pkt.data;
if (q->space >= a->max_spaces)
return alloc_resp(a, ALLOC_IF_ERR_SPACE, 0, 0);
struct space *space = spaces_at(&a->spaces, q->space);
assert(space);
struct region find = {.start = q->addr, .end = q->addr};
struct region *region = regions_find(&space->regions, find);
if (!region)
return alloc_resp(a, ALLOC_IF_ERR_RANGE, 0, 0);
return alloc_resp(a, ALLOC_IF_OK, region->start, region->end);
}
static stat alloc_clock(struct alloc *a)
{
if (!a->in.busy)
return OK;
if (a->out.busy) {
stat ret = SEND(a, a->send, a->out.pkt);
if (ret == EBUSY)
return OK;
assert(ret == OK);
a->out.busy = false;
}
uint32_t off = (uint32_t)a->in.pkt.to;
switch (off) {
case ALLOC_IF_NEW: return alloc_new(a);
case ALLOC_IF_RM: return alloc_rm(a);
case ALLOC_IF_Q: return alloc_q(a);
default: break;
}
return OK;
}
void ideal_alloc_connect(struct component *alloc, struct component *send)
{
struct alloc *a = (struct alloc *)alloc;
assert(a->send == NULL);
a->send = send;
}
struct component *create_ideal_alloc(uint64_t max_spaces)
{
struct alloc *a = calloc(1, sizeof(struct alloc));
a->spaces = spaces_create(max_spaces);
if (spaces_uninit(&a->spaces)) {
free(a);
return NULL;
}
for (size_t i = 0; i < max_spaces; ++i) {
struct space space = create_space();
spaces_append(&a->spaces, space);
}
a->max_spaces = max_spaces;
a->component.clock = (clock_callback)alloc_clock;
a->component.receive = (receive_callback)alloc_receive;
a->component.destroy = (destroy_callback)alloc_destroy;
return (struct component *)a;
}
|