aboutsummaryrefslogtreecommitdiff
path: root/src/alloc.h
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-16 17:11:55 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-16 17:11:55 +0200
commitcca81e59a9afeebe1237f713e919b5002df3d749 (patch)
treeae4ab593bfcec8208db9c912cc7712372473ef5a /src/alloc.h
parent51a716cc7bc555d765cf4ab97c6ee4935683d129 (diff)
downloadffwd-cca81e59a9afeebe1237f713e919b5002df3d749.tar.gz
ffwd-cca81e59a9afeebe1237f713e919b5002df3d749.zip
cpu: extract alloc.{c,h} and vec.c from embed.c
Diffstat (limited to 'src/alloc.h')
-rw-r--r--src/alloc.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/alloc.h b/src/alloc.h
new file mode 100644
index 0000000..bc40e8b
--- /dev/null
+++ b/src/alloc.h
@@ -0,0 +1,34 @@
+#ifndef EMBED_ALLOC_H
+#define EMBED_ALLOC_H
+
+#include <stddef.h>
+
+/* Overflow-checked size arithmetic and typed (re)allocation helpers shared by
+ * the model loader, the forward pass, and the late-interaction code. Every
+ * helper returns -1 (or NULL) on overflow or allocation failure so callers can
+ * bail without a partial buffer. realloc_* keep the original pointer intact on
+ * failure; the caller still owns it. */
+
+/* out = a * b, or -1 if the product overflows size_t. */
+int mul_size(size_t a, size_t b, size_t *out);
+
+/* out = a + b, or -1 if the sum overflows size_t. */
+int add_size(size_t a, size_t b, size_t *out);
+
+/* Round a requested sequence capacity up to the workspace growth granularity,
+ * never shrinking below the current capacity. Returns -1 on bad input/overflow. */
+int grow_cap(int current, int needed, int *out);
+
+/* realloc *ptr to hold `count` floats; *ptr unchanged on failure. */
+int realloc_floats(float **ptr, size_t count);
+
+/* malloc `count` floats, NULL on overflow/failure. */
+float *malloc_floats(size_t count);
+
+/* realloc *ptr to hold rows*cols floats; -1 on negative dims/overflow/failure. */
+int realloc_floats_2d(float **ptr, int rows, int cols);
+
+/* realloc *ptr to hold `count` ints; *ptr unchanged on failure. */
+int realloc_ints(int **ptr, size_t count);
+
+#endif /* EMBED_ALLOC_H */