shithub: femtolisp

Download patch

ref: 0cbf98476b3fd0a1353c6383124578fc1fdac26b
parent: c4d72d5376716b78fbe292be1d21697823b92566
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Oct 30 21:03:33 EDT 2024

add (array-alloc 'TYPE SIZE FILL)

--- a/cvalues.c
+++ b/cvalues.c
@@ -550,6 +550,29 @@
 	return cv;
 }
 
+BUILTIN("array-alloc", array_alloc)
+{
+	size_t elsize, sz;
+	long i, cnt;
+
+	argcount(nargs, 3);
+	cnt = toulong(args[1]);
+	if(cnt < 0)
+		lerrorf(ArgError, "invalid size: %d", cnt);
+
+	fltype_t *type = get_array_type(args[0]);
+	elsize = type->elsz;
+	sz = elsize * cnt;
+
+	value_t cv = cvalue(type, sz);
+	char *dest = cv_data(ptr(cv));
+	for(i = 0; i < cnt; i++){
+		cvalue_init(type->eltype, args[2], dest);
+		dest += elsize;
+	}
+	return cv;
+}
+
 // NOTE: v must be an array
 size_t
 cvalue_arraylen(value_t v)