aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/allocfast.zc
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
commitecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch)
tree70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /tests/programs/allocfast.zc
a
Diffstat (limited to 'tests/programs/allocfast.zc')
-rw-r--r--tests/programs/allocfast.zc45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/programs/allocfast.zc b/tests/programs/allocfast.zc
new file mode 100644
index 0000000..5ef2df2
--- /dev/null
+++ b/tests/programs/allocfast.zc
@@ -0,0 +1,45 @@
+// repeated allocations of a size known at compile time: new(T) of a struct of
+// numbers, and new(n) with a constant n. each pass of the loop makes an object
+// and frees it at the end of the block, so the allocator should hand back the
+// block it just took. the sums prove the objects were zeroed every time.
+struct point { int x, y; };
+struct line { point* a; int len; };
+
+fn loop_leaf(int n) {
+ let total = 0;
+ for (int i = 0; i < n; i++) {
+ point* p = new(point);
+ total += p.x + p.y; // zero from the allocator, every pass
+ p.x = i; p.y = 2;
+ }
+ return total;
+}
+fn loop_words(int n) {
+ let total = 0, i = 0;
+ while (i < n) {
+ let w = new(6);
+ total += w[0] + w[5];
+ w[0] = 7; w[5] = 9;
+ i++;
+ }
+ return total;
+}
+fn loop_owner(int n) {
+ let i = 0, total = 0;
+ while (i < n) {
+ line* l = new(line);
+ l.a = new(point);
+ l.a.x = i;
+ total += l.a.x + l.len;
+ i++;
+ }
+ return total;
+}
+fn main() {
+ let before = live_objects();
+ put_n(loop_leaf(50000)); put_c(' ');
+ put_n(loop_words(50000)); put_c(' ');
+ put_n(loop_owner(1000)); put_c(' ');
+ put_n(live_objects() - before); put_c(10);
+ return 0;
+}