harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 3fa71772e8769a24939a8f7db9379776fece4098
parent 89f77d6f4fa5ad5af97ceee51491ae1448ae5e8c
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Wed,  3 Feb 2021 18:40:39 -0500

Add rt::ensure

Diffstat:
Mrt/Makefile | 1+
Art/ensure.ha | 15+++++++++++++++
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/rt/Makefile b/rt/Makefile @@ -1,6 +1,7 @@ libhart_srcs+=\ rt/abort.ha \ rt/compile.ha \ + rt/ensure.ha \ rt/malloc.ha \ rt/memcpy.ha \ rt/memset.ha \ 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; +};