making tcc happier

This commit is contained in:
2024-12-27 12:34:08 +02:00
parent 6ae7d7320d
commit 6006a802e1
10 changed files with 416 additions and 274 deletions

View File

@@ -5,14 +5,28 @@
#include <stdint.h>
#include <stdlib.h>
#define SLICE_INIT(Type, initial_cap) ({ \
#define SLICE(Type) \
struct Type##Slice { \
uint32_t len; \
uint32_t cap; \
Type* arr; \
}
#define ARR_INIT(Type, initial_cap) ({ \
Type* arr = calloc(initial_cap, sizeof(Type)); \
if (!arr) \
exit(1); \
(__typeof__(Type*)) { arr }; \
arr; \
})
#define SLICE_RESIZE(slice, Type, new_cap) ({ \
#define SLICE_INIT(Type, initial_cap) \
{ \
.len = 0, \
.cap = (initial_cap), \
.arr = ARR_INIT(Type, initial_cap) \
}
#define SLICE_RESIZE(Type, slice, new_cap) ({ \
uint32_t cap = (new_cap); \
Type* new_arr = realloc((slice)->arr, cap * sizeof(Type)); \
if (!new_arr) \
@@ -21,12 +35,17 @@
(slice)->cap = cap; \
})
#define SLICE_ENSURE_CAPACITY(slice, Type, additional) ({ \
#define SLICE_ENSURE_CAPACITY(Type, slice, additional) ({ \
if ((slice)->len + (additional) > (slice)->cap) { \
SLICE_RESIZE(slice, \
Type, \
SLICE_RESIZE(Type, \
slice, \
((slice)->cap * 2 > (slice)->len + (additional)) ? (slice)->cap * 2 : (slice)->len + (additional)); \
} \
})
#define SLICE_APPEND(Type, slice, item) ({ \
SLICE_ENSURE_CAPACITY(Type, slice, 1); \
(slice)->arr[(slice)->len++] = (item); \
})
#endif