commit a57d77709b02bb6c39d33bdae1e9a85ea247abbd
parent 92912c78b5fd86669d51b2ea4cc0187b0c5973aa
Author: Eyal Sawady <ecs@d2evs.net>
Date: Sun, 21 Feb 2021 18:06:19 -0500
Add rt::unensure
Diffstat:
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/rt/ensure.ha b/rt/ensure.ha
@@ -15,7 +15,19 @@ export fn ensure(s: *slice, membsz: size, length: size) void = {
};
};
s.capacity = cap;
- let data = realloc(s.data, s.capacity * membsz);
- assert(data != null);
+ const data = realloc(s.data, s.capacity * membsz);
+ assert(data != null || s.capacity * membsz == 0);
+ s.data = data;
+};
+
+export fn unensure(s: *slice, membsz: size, length: size) void = {
+ let cap = s.capacity;
+ for (cap > length) {
+ cap /= 2;
+ };
+ cap *= 2;
+ s.capacity = cap;
+ const data = realloc(s.data, s.capacity * membsz);
+ assert(data != null || s.capacity * membsz == 0);
s.data = data;
};