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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util/alloc.h>
#include <util/asan.h>
#include <util/exit.h>
#include <util/gv_math.h>
#include <util/list-private.h>
#include <util/prisize_t.h>
static const void *slot_from_const_list(const list_t_ *list, size_t index,
size_t stride) {
assert(list != NULL);
assert(list->base != NULL || index == 0 || stride == 0);
const char *const base = list->base;
return base + index * stride;
}
static void *slot_from_list(list_t_ *list, size_t index, size_t stride) {
assert(list != NULL);
assert(list->base != NULL || index == 0 || stride == 0);
char *const base = list->base;
return base + index * stride;
}
static const void *slot_from_const_base(const void *base, size_t index,
size_t stride) {
assert(base != NULL || index == 0 || stride == 0);
const char *const b = base;
return b + index * stride;
}
static void *slot_from_base(void *base, size_t index, size_t stride) {
assert(base != NULL || index == 0 || stride == 0);
char *const b = base;
return b + index * stride;
}
#define INDEX_TO(origin, index, stride) \
(_Generic((origin), \
const list_t_ *: slot_from_const_list, \
list_t_ *: slot_from_list, \
const void *: slot_from_const_base, \
void *: slot_from_base)((origin), (index), (stride)))
size_t gv_list_append_slot_(list_t_ *list, size_t item_size) {
assert(list != NULL);
// do we need to expand the backing storage?
if (list->size == list->capacity) {
const size_t c = list->capacity == 0 ? 1 : (list->capacity * 2);
gv_list_reserve_(list, c, item_size);
}
assert(list->capacity > 0);
assert(list->size < list->capacity);
// append the new slot
const size_t new_slot = (list->head + list->size) % list->capacity;
void *const slot = INDEX_TO(list, new_slot, item_size);
ASAN_UNPOISON(slot, item_size);
++list->size;
return new_slot;
}
size_t gv_list_prepend_slot_(list_t_ *list, size_t item_size) {
assert(list != NULL);
// do we need to expand the backing storage?
if (list->size == list->capacity) {
const size_t c = list->capacity == 0 ? 1 : (list->capacity * 2);
gv_list_reserve_(list, c, item_size);
}
assert(list->capacity > 0);
assert(list->size < list->capacity);
// prepend the new slot
list->head = (list->head + (list->capacity - 1)) % list->capacity;
void *const slot = INDEX_TO(list, list->head, item_size);
ASAN_UNPOISON(slot, item_size);
++list->size;
return list->head;
}
static int try_reserve(list_t_ *list, size_t capacity, size_t item_size) {
assert(list != NULL);
// if we can already fit enough items, nothing to do
if (list->capacity >= capacity) {
return 0;
}
// will the arithmetic below overflow?
assert(capacity > 0);
if (SIZE_MAX / capacity < item_size) {
return EOVERFLOW;
}
void *const base = realloc(list->base, capacity * item_size);
if (base == NULL) {
return ENOMEM;
}
// zero the new memory
{
void *const new = INDEX_TO(base, list->capacity, item_size);
const size_t new_bytes = (capacity - list->capacity) * item_size;
memset(new, 0, new_bytes);
// poison the new (conceptually unallocated) memory
ASAN_POISON(new, new_bytes);
}
// Do we need to shuffle the prefix upwards? E.g.
//
// ┌───┬───┬───┬───┐
// old: │ 3 │ 4 │ 1 │ 2 │
// └───┴───┴─┼─┴─┼─┘
// │ └───────────────┐
// └───────────────┐ │
// ▼ ▼
// ┌───┬───┬───┬───┬───┬───┬───┬───┐
// new: │ 3 │ 4 │ │ │ │ │ 1 │ 2 │
// └───┴───┴───┴───┴───┴───┴───┴───┘
// a b c d e f g h
if (list->head + list->size > list->capacity) {
const size_t prefix = list->capacity - list->head;
const size_t new_head = capacity - prefix;
// unpoison target range, slots [g, h] in example
void *const target = INDEX_TO(base, new_head, item_size);
ASAN_UNPOISON(target, prefix * item_size);
const void *const src = INDEX_TO(base, list->head, item_size);
memmove(target, src, prefix * item_size);
// (re-)poison new gap, slots [c, f] in example
void *const gap_begin = INDEX_TO(base, list->size - prefix, item_size);
ASAN_POISON(gap_begin, (list->capacity - list->size) * item_size);
list->head = new_head;
}
list->base = base;
list->capacity = capacity;
return 0;
}
bool gv_list_try_append_(list_t_ *list, const void *item, size_t item_size) {
assert(list != NULL);
assert(item != NULL);
// do we need to expand the backing storage?
if (list->size == list->capacity) {
do {
// can we attempt doubling without integer overflow?
if (SIZE_MAX / 2 >= list->capacity) {
const size_t c = list->capacity == 0 ? 1 : (list->capacity * 2);
if (try_reserve(list, c, item_size) == 0) {
// success
break;
}
}
// try a more conservative expansion
if (SIZE_MAX - 1 >= list->capacity) {
if (try_reserve(list, list->capacity + 1, item_size) == 0) {
// success
break;
}
}
// failed to expand the list
return false;
} while (0);
}
assert(list->size < list->capacity);
// we can now append, knowing it will not require backing storage expansion
const size_t new_slot = (list->head + list->size) % list->capacity;
void *const slot = INDEX_TO(list, new_slot, item_size);
ASAN_UNPOISON(slot, item_size);
memcpy(slot, item, item_size);
++list->size;
return true;
}
size_t gv_list_get_(const list_t_ list, size_t index) {
assert(index < list.size && "index out of bounds");
return (list.head + index) % list.capacity;
}
size_t gv_list_find_(const list_t_ list, const void *needle, size_t item_size) {
for (size_t i = 0; i < list.size; ++i) {
const size_t slot = gv_list_get_(list, i);
const void *candidate = INDEX_TO(&list, slot, item_size);
if (memcmp(needle, candidate, item_size) == 0) {
return i;
}
}
return SIZE_MAX;
}
void gv_list_remove_(list_t_ *list, size_t index, size_t item_size) {
assert(list != NULL);
assert(index < list->size);
// shrink the list
for (size_t i = index + 1; i < list->size; ++i) {
const size_t dst_slot = gv_list_get_(*list, i - 1);
void *const dst = INDEX_TO(list, dst_slot, item_size);
const size_t src_slot = gv_list_get_(*list, i);
const void *const src = INDEX_TO(list, src_slot, item_size);
memcpy(dst, src, item_size);
}
const size_t truncated_slot = gv_list_get_(*list, list->size - 1);
void *truncated = INDEX_TO(list, truncated_slot, item_size);
ASAN_POISON(truncated, item_size);
--list->size;
}
void gv_list_clear_(list_t_ *list, size_t item_size) {
assert(list != NULL);
for (size_t i = 0; i < list->size; ++i) {
const size_t slot = gv_list_get_(*list, i);
void *const to_poison = INDEX_TO(list, slot, item_size);
ASAN_POISON(to_poison, item_size);
}
list->size = 0;
// opportunistically re-sync the list
list->head = 0;
}
void gv_list_reserve_(list_t_ *list, size_t capacity, size_t item_size) {
const int err = try_reserve(list, capacity, item_size);
if (err != 0) {
fprintf(stderr,
"failed to reserve %" PRISIZE_T " elements of size %" PRISIZE_T
" bytes: %s\n",
capacity, item_size, strerror(err));
graphviz_exit(EXIT_FAILURE);
}
}
bool gv_list_contains_(const list_t_ list, const void *needle,
size_t item_size) {
return gv_list_find_(list, needle, item_size) != SIZE_MAX;
}
list_t_ gv_list_copy_(const list_t_ list, size_t item_size) {
list_t_ ret = {.base = gv_calloc(list.capacity, item_size),
.capacity = list.capacity};
// opportunistically create the new list synced
for (size_t i = 0; i < list.size; ++i) {
const size_t slot = gv_list_get_(list, i);
const void *const src = INDEX_TO(&list, slot, item_size);
void *const dst = INDEX_TO(&ret, ret.size, item_size);
assert(ret.size < ret.capacity);
memcpy(dst, src, item_size);
++ret.size;
}
// mark the remainder of the allocated space as inaccessible
void *const to_poison = INDEX_TO(&ret, ret.size, item_size);
const size_t to_poison_len = (ret.capacity - ret.size) * item_size;
ASAN_POISON(to_poison, to_poison_len);
return ret;
}
bool gv_list_is_contiguous_(const list_t_ list) {
return list.head + list.size <= list.capacity;
}
void gv_list_sync_(list_t_ *list, size_t item_size) {
assert(list != NULL);
// Allow unrestricted access. The shuffle below accesses both allocated
// and unallocated elements, so just let it read and write everything.
ASAN_UNPOISON(list->base, list->capacity * item_size);
// Shuffle the list 1-1 until it is aligned. This is not efficient, but
// we assume this is a relatively rare operation.
while (list->head != 0) {
// rotate the list leftwards by 1
assert(list->capacity > 0);
// shuffle byte-by-byte to avoid dynamic allocation
for (size_t i = 0; i < item_size; ++i) {
uint8_t lowest;
memcpy(&lowest, list->base, sizeof(lowest));
const size_t remainder = list->capacity * item_size - sizeof(lowest);
memmove(list->base, (char *)list->base + sizeof(lowest), remainder);
memcpy((char *)list->base + remainder, &lowest, sizeof(lowest));
}
--list->head;
}
/* synchronization should have ensured the list no longer wraps */
assert(gv_list_is_contiguous_(*list));
/* re-establish access restrictions */
void *end = INDEX_TO(list, list->size, item_size);
ASAN_POISON(end, (list->capacity - list->size) * item_size);
}
void gv_list_sort_(list_t_ *list, int (*cmp)(const void *, const void *),
size_t item_size) {
assert(list != NULL);
assert(cmp != NULL);
gv_list_sync_(list, item_size);
if (list->size) {
qsort(list->base, list->size, item_size, cmp);
}
}
static void exchange(void *a, void *b, size_t size) {
assert(a != NULL);
assert(b != NULL);
// do a byte-by-byte swap of the two objects
char *x = a;
char *y = b;
for (size_t i = 0; i < size; ++i) {
SWAP(&x[i], &y[i]);
}
}
void gv_list_reverse_(list_t_ *list, size_t item_size) {
assert(list != NULL);
// move from the outside inwards, swapping elements
for (size_t i = 0; i < list->size / 2; ++i) {
const size_t a = gv_list_get_(*list, i);
const size_t b = gv_list_get_(*list, list->size - i - 1);
void *const x = INDEX_TO(list, a, item_size);
void *const y = INDEX_TO(list, b, item_size);
exchange(x, y, item_size);
}
}
void gv_list_shrink_to_fit_(list_t_ *list, size_t item_size) {
assert(list != NULL);
gv_list_sync_(list, item_size);
if (list->capacity > list->size) {
list->base = gv_recalloc(list->base, list->capacity, list->size, item_size);
list->capacity = list->size;
}
}
void gv_list_free_(list_t_ *list) {
assert(list != NULL);
free(list->base);
*list = (list_t_){0};
}
void gv_list_pop_front_(list_t_ *list, void *into, size_t item_size) {
assert(list != NULL);
assert(list->size > 0);
assert(into != NULL);
// find and pop the first slot
const size_t slot = gv_list_get_(*list, 0);
void *const to_pop = INDEX_TO(list, slot, item_size);
memcpy(into, to_pop, item_size);
ASAN_POISON(to_pop, item_size);
list->head = (list->head + 1) % list->capacity;
--list->size;
}
void gv_list_pop_back_(list_t_ *list, void *into, size_t item_size) {
assert(list != NULL);
assert(list->size > 0);
assert(into != NULL);
// find and pop last slot
const size_t slot = gv_list_get_(*list, list->size - 1);
void *const to_pop = INDEX_TO(list, slot, item_size);
memcpy(into, to_pop, item_size);
ASAN_POISON(to_pop, item_size);
--list->size;
}
void gv_list_detach_(list_t_ *list, void *datap, size_t *sizep,
size_t item_size) {
assert(list != NULL);
assert(datap != NULL);
gv_list_sync_(list, item_size);
memcpy(datap, &list->base, sizeof(void *));
if (sizep != NULL) {
*sizep = list->size;
}
*list = (list_t_){0};
}
|