source: doc/generic_types/evaluation/c-stack.c@ e6dceef

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since e6dceef was d919f47, checked in by Aaron Moss <a3moss@…>, 9 years ago

Update generics paper benchmarks

  • Property mode set to 100644
File size: 1.0 KB
Line 
1#include <stdlib.h>
2#include "c-stack.h"
3
4struct stack_node {
5 void* value;
6 struct stack_node* next;
7};
8
9struct stack new_stack() {
10 return (struct stack){ NULL };
11}
12
13void copy_stack(struct stack* s, struct stack* t, void* (*copy)(void*)) {
14 struct stack_node** crnt = &s->head;
15 struct stack_node* next = t->head;
16 while ( next ) {
17 *crnt = malloc(sizeof(struct stack_node));
18 **crnt = (struct stack_node){ copy(next->value) };
19 crnt = &(*crnt)->next;
20 next = next->next;
21 }
22 *crnt = 0;
23}
24
25void clear_stack(struct stack* s) {
26 struct stack_node* next = s->head;
27 while ( next ) {
28 struct stack_node* crnt = next;
29 next = crnt->next;
30 free(crnt->value);
31 free(crnt);
32 }
33 s->head = NULL;
34}
35
36_Bool stack_empty(const struct stack* s) {
37 return s->head == NULL;
38}
39
40void push_stack(struct stack* s, void* value) {
41 struct stack_node* n = malloc(sizeof(struct stack_node));
42 *n = (struct stack_node){ value, s->head };
43 s->head = n;
44}
45
46void* pop_stack(struct stack* s) {
47 struct stack_node* n = s->head;
48 s->head = n->next;
49 void* x = n->value;
50 free(n);
51 return x;
52}
Note: See TracBrowser for help on using the repository browser.