-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace.98.c
More file actions
226 lines (187 loc) · 6.02 KB
/
space.98.c
File metadata and controls
226 lines (187 loc) · 6.02 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// File created: 2011-08-06 15:44:53
#include "space.all.h"
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "stdlib.any.h"
#include "space/map-no-place.98.h"
#include "space/place-box.98.h"
#include "space/place-box-put.98.h"
mushspace* mushspace_init(void* vp, mushstats* stats) {
mushspace *space = vp ? vp : malloc(sizeof *space);
if (!space)
return NULL;
if (!mushboxen_init(&space->boxen)) {
free(space);
return NULL;
}
if (stats) {
space->private_stats = false;
space->stats = stats;
} else {
space->private_stats = true;
if (!(space->stats = malloc(sizeof *space->stats))) {
free(space);
return NULL;
}
// See http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-February/019920.html for
// discussion on this. As of Clang 3.1, this is our only option apart from
// spelling out an initializer for each field.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
*space->stats = (mushstats){0};
#pragma clang diagnostic pop
}
space->invalidatees = NULL;
// Placate valgrind and such: it's not necessary to define these before the
// first use.
space->last_beg = space->last_end = MUSHCOORDS(0,0,0);
mushmemorybuf_init(&space->recent_buf);
mushcell_space(
space->static_box.array, MUSH_ARRAY_LEN(space->static_box.array));
space->signal = musherr_default_handler;
return space;
}
void mushspace_free(mushspace* space) {
mushboxen_free(&space->boxen);
if (space->invalidatees) {
free(space->invalidatees);
free(space->invalidatees_data);
}
if (space->private_stats)
free(space->stats);
}
mushspace* mushspace_copy(void* vp, const mushspace* space, mushstats* stats) {
mushspace *copy = vp ? vp : malloc(sizeof *copy);
if (!copy)
return NULL;
memcpy(copy, space, sizeof *copy);
if (stats) {
copy->private_stats = false;
copy->stats = stats;
} else {
copy->private_stats = true;
copy->stats = malloc(sizeof *copy->stats);
if (!copy->stats) {
free(copy);
return NULL;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
*space->stats = (mushstats){0};
#pragma clang diagnostic pop
}
mushboxen_copy(©->boxen, &space->boxen);
// Invalidatees refer to the original space, not the copy.
copy->invalidatees = NULL;
return copy;
}
mushcell mushspace_get(const mushspace* space, mushcoords c) {
if (mushstaticaabb_contains(c))
return mushstaticaabb_get(&space->static_box, c);
const mushaabb* box;
if ((box = mushboxen_get(&space->boxen, c)))
return mushaabb_get(box, c);
return ' ';
}
void mushspace_put(mushspace* space, mushcoords p, mushcell c) {
if (mushstaticaabb_contains(p)) {
mushstaticaabb_put(&space->static_box, p, c);
return;
}
mushaabb* box = mushboxen_get(&space->boxen, p);
if (box) {
mushaabb_put(box, p, c);
return;
}
mushspace_place_box_put(space, p, c);
}
void mushspace_get_loose_bounds(const mushspace* space, mushbounds* bounds) {
bounds->beg = MUSHSTATICAABB_BEG;
bounds->end = MUSHSTATICAABB_END;
mushboxen_loosen_bounds(&space->boxen, bounds);
}
void mushspace_map(mushspace* space, mushbounds bounds,
void(*f)(musharr_mushcell, mushcoords, mushcoords, void*),
void* data)
{
mushaabb aabb;
mushaabb_make(&aabb, &bounds);
mushspace_place_box(space, &aabb, NULL, NULL);
mushspace_map_no_place(space, &bounds, data, f, NULL);
}
void mushspace_map_existing(
mushspace* space, mushbounds bounds,
void(*f)(musharr_mushcell, mushcoords, mushcoords, void*),
void(*g)( mushcoords, mushcoords, void*),
void* data)
{
mushaabb aabb;
mushaabb_make(&aabb, &bounds);
mushspace_map_no_place(space, &bounds, data, f, g);
}
bool mushspace_invalidate_all(mushspace* space) {
bool (**i)(void*) = space->invalidatees;
void **d = space->invalidatees_data;
bool success = true;
if (i)
while (*i)
if (!(*i++)(*d++))
success = false;
return success;
}
bool mushspace_add_invalidatee(mushspace* space, bool(*i)(void*), void* d) {
size_t n = 1;
bool (**is)(void*) = space->invalidatees;
void **id;
if (is) {
while (*is++)
++n;
id = space->invalidatees_data;
} else
id = NULL;
is = realloc(space->invalidatees, (n+1) * sizeof *is);
if (!is)
return false;
// The realloc succeeded, so space->invalidatees is invalid, so we have to
// overwrite it immediately. If the realloc below fails, this is still fine;
// we're just using one function pointer's worth of memory for nothing.
space->invalidatees = is;
// We need to place the terminator immediately, though, since if this was
// the first invalidatee and the realloc below fails, we'd leave
// invalidatees as a nonnull but unterminated array.
is[n] = NULL;
id = realloc(id, n * sizeof *id);
if (!id)
return false;
space->invalidatees_data = id;
is[n-1] = i;
id[n-1] = d;
return true;
}
void mushspace_del_invalidatee(mushspace* spac, void* d) {
size_t i = 0;
void **id = spac->invalidatees_data;
bool (**is)(void*) = spac->invalidatees;
for (; id[i] != d; ++i)
assert (is[i]);
if (is[i+1]) {
size_t rest_len = i+1;
while (is[rest_len])
++rest_len;
memmove(id + i, id + i + 1, rest_len * sizeof *id);
memmove(is + i, is + i + 1, (rest_len + 1) * sizeof *is);
} else
is[i] = NULL;
if (i) {
// Potential realloc failures don't matter; we'll just be using some
// extra memory. The element has already been deleted (and the list
// properly null-terminated) above.
if ((id = realloc(id, i * sizeof *id))) spac->invalidatees_data = id;
if ((is = realloc(is, (i+1) * sizeof *is))) spac->invalidatees = is;
} else {
free(id);
free(is);
spac->invalidatees = NULL;
}
}