source: doc/generic_types/evaluation/cfa-stack.c @ 4b0f997

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4b0f997 was 65cb413, checked in by Aaron Moss <a3moss@…>, 7 years ago

Patch performance regression calling CFA stdlib delete in benchmark

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <assert>
2#include <stdlib>
3#include "cfa-stack.h"
4
5forall(otype T) struct stack_node {
6        T value;
7        stack_node(T)* next;
8};
9
10forall(otype T) void ?{}(stack(T)* s) {
11        (&s->head){ 0 };
12}
13
14forall(otype T) void copy(stack(T)* s, stack(T)* t) {
15        stack_node(T)** crnt = &s->head;
16        stack_node(T)* next = t->head;
17        while ( next ) {
18                *crnt = ((stack_node(T)*)malloc()){ next->value };
19                stack_node(T)* acrnt = *crnt;
20                crnt = &acrnt->next;
21                next = next->next;
22        }
23        *crnt = 0;
24}
25
26forall(otype T) void ?{}(stack(T)* s, stack(T) t) {
27        stack_node(T)** crnt = &s->head;
28        stack_node(T)* next = t.head;
29        while ( next ) {
30                *crnt = ((stack_node(T)*)malloc()){ next->value };
31                stack_node(T)* acrnt = *crnt;
32                crnt = &acrnt->next;
33                next = next->next;
34        }
35        *crnt = 0;
36}
37
38forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t) {
39        if ( s->head == t.head ) return *s;
40        clear(s);
41        s{ t };
42        return *s;
43}
44
45forall(otype T) void ^?{}(stack(T)* s) {
46        clear(s);
47}
48
49forall(otype T) _Bool empty(const stack(T)* s) {
50        return s->head == 0;
51}
52
53forall(otype T) void push(stack(T)* s, T value) {
54        s->head = ((stack_node(T)*)malloc()){ value, s->head };
55}
56
57forall(otype T) T pop(stack(T)* s) {
58        stack_node(T)* n = s->head;
59        s->head = n->next;
60        T x = n->value;
61        ^n{};
62        free(n);
63        return x;
64}
65
66forall(otype T) void clear(stack(T)* s) {
67        stack_node(T)* next = s->head;
68        while ( next ) {
69                stack_node(T)* crnt = next;
70                next = crnt->next;
71                delete(crnt);
72        }
73        s->head = 0;
74}
Note: See TracBrowser for help on using the repository browser.