cusel/compiler/vector.h
2026-11-21 00:46:50 +02:00

179 lines
3.8 KiB
C

struct type_erased_vector {
// Put the size_t fields first, so that dummy_pointer will alias
// them and more likely trigger a segfault if we accidentally use
// it.
size_t capacity;
size_t size;
void *ptr;
};
#define VEC(type) vec_##type
#define DECL_VEC(type) typedef struct { \
union { \
struct type_erased_vector data; \
type *dummy_pointer; \
}; \
} VEC(type)
void vec_resize(
struct type_erased_vector *vec,
size_t count,
size_t element_size,
char const function[static 1],
char const filename[static 1],
int linenum
);
#define VEC_RESIZE(vec, count) vec_resize( \
&(vec).data, \
(count), \
sizeof *(vec).dummy_pointer, \
__func__, \
__FILE__, \
__LINE__ \
);
void vec_ensure_free_space(
struct type_erased_vector *vec,
size_t element_size,
char const function[static 1],
char const filename[static 1],
int linenum
);
#define VEC_ENSURE_FREE_SPACE(vec) vec_ensure_free_space( \
&(vec).data, \
sizeof *(vec).dummy_pointer, \
__func__, \
__FILE__, \
__LINE__ \
);
void vec_push(
struct type_erased_vector *vec,
void *element,
size_t element_size,
char const function[static 1],
char const filename[static 1],
int linenum
);
#define VEC_PUSH(vec, element) vec_push( \
&(vec).data, \
&(element), \
sizeof (1 ? *(vec).dummy_pointer : (element)), \
__func__, \
__FILE__, \
__LINE__ \
);
struct type_erased_span {
size_t size;
void *ptr;
};
#define SPAN(type) span_##type
#define DECL_SPAN(type) typedef struct {\
union { \
struct type_erased_span data; \
type *dummy_pointer; \
}; \
} SPAN(type)
static inline struct type_erased_span vec_as_span(
struct type_erased_vector vec
) {
return (struct type_erased_span) {
.ptr = vec.ptr,
.size = vec.size,
};
}
#define VEC_AS_SPAN(type, vec) _Generic((vec).dummy_pointer, \
type*: (SPAN(type)) { \
.data = vec_as_span((vec).data) \
} \
)
static inline struct type_erased_span vec_free_space(
struct type_erased_vector vec
) {
if (!vec.ptr) {
// Doing any arithmetic on a NULL pointer is UB, even adding 0
// to it, so we need to handle this separately.
return (struct type_erased_span) { .ptr = NULL, .size = 0 };
}
// We can't do pointer arithmetic portably on void*, but char* has
// the correct semantics aiui universally.
char *ptr = vec.ptr;
return (struct type_erased_span) {
.ptr = ptr + vec.size,
.size = vec.capacity - vec.size,
};
}
#define VEC_FREE_SPACE(type, vec) _Generic((vec).dummy_pointer, \
type*: (SPAN(type)) { \
.data = vec_free_space((vec).data) \
} \
)
#define LENGTH(span_or_vec) ((span_or_vec).data.size / sizeof *(span_or_vec).dummy_pointer)
static inline void *generic_at(
void *ptr,
size_t size,
size_t index,
size_t element_size,
char const function[static 1],
char const filename[static 1],
int linenum
) {
size_t length = size / element_size;
if (index < length) {
size_t offset = index * element_size;
return ((char *)ptr) + offset;
} else {
panic_at(function, filename, linenum, "out-of-bounds index %zu (length %zu)", index, length);
}
}
static inline void *vec_at(
struct type_erased_vector vec,
size_t index,
size_t element_size,
char const function[static 1],
char const filename[static 1],
int linenum
) {
return generic_at(vec.ptr, vec.size, index, element_size, function, filename, linenum);
}
static inline void *span_at(
struct type_erased_span span,
size_t index,
size_t element_size,
char const function[static 1],
char const filename[static 1],
int linenum
) {
return generic_at(span.ptr, span.size, index, element_size, function, filename, linenum);
}
#define AT(span_or_vec, index) (*( \
(typeof((span_or_vec).dummy_pointer)) \
_Generic((span_or_vec).data, \
struct type_erased_vector: vec_at, \
struct type_erased_span: span_at \
) ( \
(span_or_vec).data, \
(index), \
sizeof *(span_or_vec).dummy_pointer, \
__func__, \
__FILE__, \
__LINE__ \
) \
))