summaryrefslogtreecommitdiff
path: root/mem.h
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-08-06 16:44:49 +0100
committerubq323 <ubq323@ubq323.website>2024-08-06 16:48:32 +0100
commita266b97829ebe698a4256890fdf1214e479fe1de (patch)
tree61cc3d7aaae0933ffa3081f88f7514c086b14ae6 /mem.h
parent000e8ca43f4968412ed5c8fc514bb6caa3e5c450 (diff)
rearrange and refactor compiler
part 1/? of making compiler use badthing objects instead of separate ast
Diffstat (limited to 'mem.h')
-rw-r--r--mem.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/mem.h b/mem.h
index d13e02f..e7edb82 100644
--- a/mem.h
+++ b/mem.h
@@ -13,6 +13,21 @@ void *M(State *S, void *ptr, size_t old, size_t new);
#define RENEW_ARR(S,p,ty,old,new) (ty*)M(S, (p), (old)*sizeof(ty), (new)*sizeof(ty))
#define NEW_OBJ(S,ty, oty) (ty*)alloc_obj(S, sizeof(ty), oty)
+// needs len,cap,d fields
+#define ENSURE_CAP(S, darr, type, needed) \
+ if (darr.cap < needed) { \
+ size_t __newsz = next_pwrof2(needed); \
+ if (__newsz < 8) __newsz = 8;
+ darr.d = RENEW_ARR(S, darr.d, type, darr.cap, __newsz); \
+ darr.cap = __newsz; \
+ }
+
+inline size_t next_pwrof2(size_t x) {
+ size_t p = 1;
+ while (p < x) p <<= 1;
+ return p;
+}
+
#define FREE(S,p,ty) M(S, p, sizeof(ty), 0)
#define FREE_ARR(S,p,ty,old) M(S, p, (old)*sizeof(ty), 0)