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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
/**
* @file
*
* Dynamic arrays.
*
* See @ref Vectors.
*/
#ifndef DRGN_VECTOR_H
#define DRGN_VECTOR_H
#include <stdbool.h>
#include <stdlib.h> // IWYU pragma: keep
#include <string.h> // IWYU pragma: keep
#include "generics.h"
#include "minmax.h"
#include "util.h"
#include "pp.h"
/**
* @ingroup Internals
*
* @defgroup Vectors Vectors
*
* Dynamic arrays (a.k.a.\ vectors).
*
* This is an implementation of generic, strongly-typed vectors.
*
* A vector is defined with @ref DEFINE_VECTOR(). Each generated vector
* interface is prefixed with a given name; the interface documented here uses
* the example name @c vector.
*
* @{
*/
#ifdef DOXYGEN
/**
* @struct vector
*
* Vector instance.
*
* There are no requirements on how this is allocated; it may be global, on the
* stack, allocated by @c malloc(), embedded in another structure, etc.
*/
struct vector;
/**
* Initialize a @ref vector.
*
* The new vector is empty.
*
* @sa VECTOR_INIT
*/
void vector_init(struct vector *vector);
/** Free memory allocated by a @ref vector. */
void vector_deinit(struct vector *vector);
/** Return the number of entries in a @ref vector. */
size_type vector_size(const struct vector *vector);
/** Return whether a @ref vector is empty. */
bool vector_empty(const struct vector *vector);
/**
* Maximum possible number of entries in a @ref vector.
*
* Attempts to increase the size or capacity beyond this will fail.
*/
const size_type vector_max_size;
/**
* Update the number of entries in a @ref vector.
*
* If @p size is greater than the current capacity, this increases the capacity
* to at least @p size and reallocates the entries.
*
* If @p size is greater than the current size, the entries between the old size
* and the new size are uninitialized.
*
* @return @c true on success, @c false on failure.
*/
bool vector_resize(struct vector *vector, size_t size);
/**
* Set the size of a @ref vector to zero.
*
* This does not change the capacity or free the entries.
*/
void vector_clear(struct vector *vector);
/** Return the number of allocated entries in a @ref vector. */
size_type vector_capacity(const struct vector *vector);
/**
* Increase the capacity of a @ref vector.
*
* If @p capacity is greater than the current capacity, this increases the
* capacity to at least @p capacity and reallocates the entries. Otherwise, it
* does nothing.
*
* @return @c true on success, @c false on failure.
*/
bool vector_reserve(struct vector *vector, size_t capacity);
/**
* Increase the capacity of a @ref vector to accomodate at least one append.
*
* If the current capacity is equal to the current size, this increases the
* capacity by at least one and reallocates the entries. Otherwise, it does
* nothing.
*
* @return @c true on success, @c false on failure.
*/
bool vector_reserve_for_append(struct vector *vector);
/**
* Increase the capacity of a @ref vector to accomodate at least @p n appends.
*
* If the current capacity minus the current size is not at least @p n, this
* increases the capacity by at least @p n and reallocates the entries.
* Otherwise, it does nothing.
*
* @return @c true on success, @c false on failure.
*/
bool vector_reserve_for_extend(struct vector *vector, size_t n);
/**
* Free unused memory in a @ref vector.
*
* This may reduce the capacity and reallocate the entries. It may also do
* nothing.
*/
void vector_shrink_to_fit(struct vector *vector);
/**
* Steal the array of entries from a @ref vector.
*
* This returns the internal array of entries. The vector can no longer be used
* except to be passed to @ref vector_deinit(), which will do nothing.
*
* This is undefined if the vector type was defined with a non-zero @c
* inline_size.
*
* This can be used to build an array when the size isn't known ahead of time
* but won't change after the array is built. For example:
*
* ```
* DEFINE_VECTOR(int_vector, int);
*
* bool primes_less_than(int n, int **array_ret, size_t *size_ret)
* {
*
* _cleanup_(int_vector_deinit) struct int_vector vector = VECTOR_INIT;
* for (int i = 2; i < n; i++) {
* if (is_prime(i) && !int_vector_push(&vector, &i))
* return false;
* }
* int_vector_shrink_to_fit(&vector);
* int_vector_steal(&vector, array_ret, size_ret);
* return true;
* }
* ```
*
* As demonstrated here, it may be desirable to call @ref vector_shrink_to_fit()
* first.
*
* @param[out] entries_ret Returned array. This must be freed with @c free().
* @param[out] size_ret Returned number of entries in array. May be @c NULL.
*/
void vector_steal(struct vector *vector, entry_type **entries_ret,
size_type *size_ret);
/**
* Return the array of entries in a @ref vector.
*
* The vector may be empty, in which case this is equal to `vector_end(vector)`.
*/
entry_type *vector_begin(struct vector *vector);
/**
* Return one past the last entry in a @ref vector.
*
* The vector may be empty, in which case this is equal to
* `vector_begin(vector)`.
*/
entry_type *vector_end(struct vector *vector);
/**
* Return the first entry in a @ref vector.
*
* This is equivalent to `vector_at(vector, 0)`. The vector must not be empty
* (in contrast to @ref vector_begin()).
*/
entry_type *vector_first(struct vector *vector);
/**
* Return the last entry in a @ref vector.
*
* This is equivalent to `vector_at(vector, vector_size(vector) - 1)`. The
* vector must not be empty.
*/
entry_type *vector_last(struct vector *vector);
/**
* Return the entry at the given index in a @ref vector.
*
* @param[in] i Entry index. Must be less than the size of the vector.
*/
entry_type *vector_at(struct vector *vector, size_t i);
/**
* Append to a @ref vector.
*
* This increases vector's size by one. If the current capacity is equal to the
* current size, this increases the capacity by at least one and reallocates the
* entries.
*
* @return @c true on success, @c false on failure.
*/
bool vector_append(struct vector *vector, const entry_type *entry);
/**
* Append an uninitialized entry to a @ref vector.
*
* Like @ref vector_append(), but return a pointer to the new (uninitialized)
* entry.
*
* @return The new entry on success, @c NULL on failure.
*/
entry_type *vector_append_entry(struct vector *vector);
/**
* Append all of the entries from one vector to another.
*
* @param[in] dst Vector to append to.
* @param[in] src Source vector. This is not modified.
* @return @c true on success, @c false on failure.
*/
bool vector_extend(struct vector *dst, const struct vector *src);
/**
* Remove and return the last entry in a @ref vector.
*
* The vector must not be empty. This decreases the size by one. It does not
* change the capacity or reallocate the entries.
*
* @return A pointer to the removed entry, which remains valid until another
* entry is inserted in its place or the entries are reallocated.
*/
entry_type *vector_pop(struct vector *vector);
#endif
/**
* Inline as many entries as possible without making the vector type larger than
* if @c inline_size was 0.
*
* This can be passed as the @c inline_size argument to @ref DEFINE_VECTOR().
*/
#define vector_inline_minimal -1
/**
* Define a vector type without defining its functions.
*
* This is useful when the vector type must be defined in one place (e.g., a
* header) but the interface is defined elsewhere (e.g., a source file) with
* @ref DEFINE_VECTOR_FUNCTIONS(). Otherwise, just use @ref DEFINE_VECTOR().
*
* This takes the same arguments as @ref DEFINE_VECTOR().
*/
#define DEFINE_VECTOR_TYPE(...) \
PP_OVERLOAD(DEFINE_VECTOR_TYPE_I, __VA_ARGS__)(__VA_ARGS__)
#define DEFINE_VECTOR_TYPE_I2(vector, entry_type) \
DEFINE_VECTOR_TYPE_I3(vector, entry_type, 0)
#define DEFINE_VECTOR_TYPE_I3(vector, entry_type, inline_size) \
DEFINE_VECTOR_TYPE_I4(vector, entry_type, inline_size, size_t)
#define DEFINE_VECTOR_TYPE_I4(vector, entry_type, inline_size, size_type) \
typedef typeof(entry_type) vector##_entry_type; \
\
typedef typeof(size_type) vector##_size_type; \
_Static_assert((vector##_size_type)-1 > 0, "size_type must be unsigned"); \
_Static_assert((vector##_size_type)-1 <= SIZE_MAX, \
"size_type must not be larger than size_t"); \
\
enum { vector##_inline_size_arg = (inline_size) }; \
/* \
* If the vector was defined with a zero inline size, then we don't want to \
* require the complete definition of the entry type, so we do this to stub it \
* out. \
*/ \
typedef_if(vector##_inline_entry_type, vector##_inline_size_arg == 0, void *, \
vector##_entry_type); \
enum { \
vector##_inline_size = \
vector##_inline_size_arg == vector_inline_minimal \
? sizeof(void *) / sizeof(vector##_inline_entry_type) \
: vector##_inline_size_arg, \
/* Used to avoid a zero-length array. */ \
vector##_inline_size_non_zero = \
vector##_inline_size == 0 ? 1 : vector##_inline_size, \
}; \
\
struct vector { \
union { \
vector##_entry_type *_data; \
/* \
* If the vector has no inline entries, then we want this to \
* degrade to (entry_type *) instead of (entry_type [0]) so that\
* the vector is not over-aligned to alignof(entry_type) and to \
* avoid zero-length arrays. \
*/ \
type_if(vector##_inline_size == 0, vector##_entry_type *, \
vector##_inline_entry_type [vector##_inline_size_non_zero])\
_idata; \
}; \
vector##_size_type _size; \
vector##_size_type _capacity; \
}; \
struct DEFINE_VECTOR_needs_semicolon
/**
* Define the functions for a vector.
*
* The vector type must have already been defined with @ref
* DEFINE_VECTOR_TYPE().
*
* Unless the type and function definitions must be in separate places, use @ref
* DEFINE_VECTOR() instead.
*
* @sa DEFINE_VECTOR()
*/
#define DEFINE_VECTOR_FUNCTIONS(vector) \
__attribute__((__unused__)) \
static void vector##_init(struct vector *vector) \
{ \
if (vector##_inline_size == 0) \
vector->_data = NULL; \
vector->_size = vector->_capacity = 0; \
} \
\
static bool vector##_is_inline(const struct vector *vector) \
{ \
return vector##_inline_size > 0 && vector->_capacity == 0; \
} \
\
__attribute__((__unused__)) \
static void vector##_deinit(struct vector *vector) \
{ \
if (!vector##_is_inline(vector)) \
free(vector->_data); \
} \
\
__attribute__((__unused__)) \
static vector##_size_type vector##_size(const struct vector *vector) \
{ \
return vector->_size; \
} \
\
__attribute__((__unused__)) \
static bool vector##_empty(const struct vector *vector) \
{ \
return vector->_size == 0; \
} \
\
static const vector##_size_type vector##_max_size = \
/* The redundant cast works around llvm/llvm-project#38137. */ \
(vector##_size_type)min_iconst(PTRDIFF_MAX / sizeof(vector##_entry_type),\
(vector##_size_type)-1); \
\
static vector##_size_type vector##_capacity(const struct vector *vector) \
{ \
if (vector##_is_inline(vector)) \
return vector##_inline_size; \
return vector->_capacity; \
} \
\
static bool vector##_reallocate(struct vector *vector, size_t capacity) \
{ \
void *new_data; \
if (vector##_is_inline(vector)) { \
new_data = malloc(capacity * sizeof(vector##_entry_type)); \
if (!new_data) \
return false; \
memcpy(new_data, vector->_idata, \
vector##_size(vector) * sizeof(vector##_entry_type)); \
} else { \
new_data = realloc(vector->_data, \
capacity * sizeof(vector##_entry_type)); \
if (!new_data) \
return false; \
} \
vector->_data = new_data; \
vector->_capacity = capacity; \
return true; \
} \
\
static bool vector##_reserve_for_extend(struct vector *vector, size_t n) \
{ \
vector##_size_type size = vector##_size(vector); \
/* \
* Cast to size_t to avoid -Wsign-error if size_type is promoted to int.\
*/ \
if (n <= (size_t)(vector##_capacity(vector) - size)) \
return true; \
if (n > (size_t)(vector##_max_size - size)) \
return false; \
vector##_size_type new_capacity = size + (n > size ? n : size); \
if (new_capacity < size || new_capacity > vector##_max_size) \
new_capacity = vector##_max_size; \
return vector##_reallocate(vector, new_capacity); \
} \
\
__attribute__((__unused__)) \
static bool vector##_resize(struct vector *vector, size_t size) \
{ \
if (vector->_size < size \
&& !vector##_reserve_for_extend(vector, size - vector->_size)) \
return false; \
vector->_size = size; \
return true; \
} \
\
__attribute__((__unused__)) \
static void vector##_clear(struct vector *vector) \
{ \
vector->_size = 0; \
} \
\
__attribute__((__unused__)) \
static bool vector##_reserve(struct vector *vector, size_t capacity) \
{ \
if (capacity <= vector##_capacity(vector)) \
return true; \
if (capacity > vector##_max_size) \
return false; \
return vector##_reallocate(vector, capacity); \
} \
\
static bool vector##_reserve_for_append(struct vector *vector) \
{ \
return vector##_reserve_for_extend(vector, 1); \
} \
\
__attribute__((__unused__)) \
static void vector##_shrink_to_fit(struct vector *vector) \
{ \
vector##_size_type size = vector##_size(vector); \
if (vector->_capacity <= size) \
return; \
if (size > vector##_inline_size) { \
vector##_reallocate(vector, size); \
} else if (vector##_inline_size > 0) { \
void *old_data = vector->_data; \
memcpy(vector->_idata, old_data, \
size * sizeof(vector##_entry_type)); \
free(old_data); \
vector->_capacity = 0; \
} else { \
free(vector->_data); \
vector->_data = NULL; \
vector->_capacity = 0; \
} \
} \
\
/* \
* If the vector was defined with a non-zero inline size, make vector_steal() \
* fail at compile time by having it take a dummy type incompatible with struct \
* vector (but close enough to the real thing so the function body compiles). \
*/ \
struct vector##_steal_is_undefined_for_non_zero_inline_size { \
void *_data; \
vector##_size_type _size; \
}; \
__attribute__((__unused__)) \
static void vector##_steal(type_if(vector##_inline_size_arg == 0, \
struct vector, \
struct vector##_steal_is_undefined_for_non_zero_inline_size)\
*vector, \
vector##_entry_type **entries_ret, \
vector##_size_type *size_ret) \
{ \
*entries_ret = vector->_data; \
if (size_ret) \
*size_ret = vector->_size; \
vector->_data = NULL; \
} \
\
static vector##_entry_type *vector##_begin(struct vector *vector) \
{ \
if (vector##_is_inline(vector)) \
return vector->_idata; \
return vector->_data; \
} \
\
__attribute__((__unused__)) \
static vector##_entry_type *vector##_end(struct vector *vector) \
{ \
return add_to_possibly_null_pointer(vector##_begin(vector), \
vector##_size(vector)); \
} \
\
__attribute__((__unused__)) \
static vector##_entry_type *vector##_first(struct vector *vector) \
{ \
return vector##_begin(vector); \
} \
\
__attribute__((__unused__)) \
static vector##_entry_type *vector##_last(struct vector *vector) \
{ \
return vector##_begin(vector) + vector##_size(vector) - 1; \
} \
\
__attribute__((__unused__)) \
static vector##_entry_type *vector##_at(struct vector *vector, size_t i) \
{ \
return vector##_begin(vector) + i; \
} \
\
static vector##_entry_type *vector##_append_entry(struct vector *vector) \
{ \
if (!vector##_reserve_for_append(vector)) \
return NULL; \
return vector##_begin(vector) + vector->_size++; \
} \
\
__attribute__((__unused__)) \
static bool vector##_append(struct vector *vector, \
const vector##_entry_type *entry) \
{ \
vector##_entry_type *new_entry = vector##_append_entry(vector); \
if (!new_entry) \
return false; \
memcpy(new_entry, entry, sizeof(*entry)); \
return true; \
} \
\
__attribute__((__unused__)) \
static bool vector##_extend(struct vector *dst, const struct vector *src) \
{ \
if (src->_size == 0) \
return true; \
if (!vector##_reserve_for_extend(dst, src->_size)) \
return false; \
memcpy(vector##_end(dst), vector##_begin((struct vector *)src), \
src->_size * sizeof(vector##_entry_type)); \
dst->_size += src->_size; \
return true; \
} \
\
__attribute__((__unused__)) \
static vector##_entry_type *vector##_pop(struct vector *vector) \
{ \
return vector##_begin(vector) + --vector->_size; \
} \
struct DEFINE_VECTOR_needs_semicolon
/**
* Define a vector interface.
*
* This macro defines a vector type along with its functions. It accepts a
* variable number of arguments:
*
* ```
* DEFINE_VECTOR(vector, entry_type);
* DEFINE_VECTOR(vector, entry_type, inline_size);
* DEFINE_VECTOR(vector, entry_type, inline_size, size_type);
* ```
*
* @param[in] vector Name of the type to define. This is prefixed to all of the
* types and functions defined for that type.
* @param[in] entry_type Type of entries in the vector.
* @param[in] inline_size Number of entries to store directly in the vector type
* instead of as a separate allocation, or @ref vector_inline_minimal. The
* default is 0. If this is not 0, then the complete definition of @p entry_type
* must be available.
* @param[in] size_type Unsigned integer type to use to store size and capacity.
* The default is `size_t`.
*/
#define DEFINE_VECTOR(vector, ...) \
DEFINE_VECTOR_TYPE(vector, __VA_ARGS__); \
DEFINE_VECTOR_FUNCTIONS(vector)
/**
* Empty vector initializer.
*
* This can be used to initialize a vector when declaring it.
*
* @sa vector_init()
*/
#define VECTOR_INIT { { 0 } }
/**
* Define and initialize an empty @ref vector of type @p vector_type named @p
* vector that is automatically deinitialized when it goes out of scope.
*/
#define VECTOR(vector_type, vector) \
__attribute__((__cleanup__(vector_type##_deinit))) \
struct vector_type vector = VECTOR_INIT
/**
* Iterate over every entry in a @ref vector.
*
* This is roughly equivalent to
*
* ```
* for (entry_type *it = vector_begin(vector), *end = vector_end(vector);
* it != end; it++)
* ```
*
* Except that @p vector is only evaluated once.
*
* @param[in] vector_type Name of vector type.
* @param[out] it Name of iteration variable.
* @param[in] vector Vector to iterate over.
*/
#define vector_for_each(vector_type, it, vector) \
for (vector_type##_entry_type *it, \
*it##__end = ({ \
struct vector_type *it##__vector = (vector); \
it = vector_type##_begin(it##__vector); \
vector_type##_end(it##__vector); \
}); \
it != it##__end; it++)
/** @} */
#endif /* DRGN_VECTOR_H */
|