commit b6df30cf544f96796e536f65e70db9dfc73e7d5b
parent b2f9cad339eb3c6d48df27267304a24aad39253d
Author: Eyal Sawady <ecs@d2evs.net>
Date: Thu, 4 Feb 2021 14:28:43 -0500
rt: add rt::ensure
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/rt/ensure.ha b/rt/ensure.ha
@@ -0,0 +1,15 @@
+export type slice = struct {
+ data: *void,
+ length: size,
+ capacity: size,
+};
+
+export fn ensure(s: *slice, membsz: size, length: size) void = {
+ // TODO: make sure length * membsz < SIZE_MAX
+ for (s.capacity < length) {
+ s.capacity *= 2z;
+ };
+ let data = realloc(s.data, s.capacity * membsz);
+ assert(data != null: nullable *void);
+ s.data = data;
+};