commit 9f48b024b6ebb12cd95e15d4dbe9432234d54d2a
parent e5fc2405c2b4db3247cdf8b278aee30d8c50548e
Author: Eyal Sawady <ecs@d2evs.net>
Date: Sun, 21 Feb 2021 18:06:53 -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;
};