source: doc/papers/general/evaluation/c-stack.c@ 1a3eab8

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 with_gc
Last change on this file since 1a3eab8 was ac4dad2, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

shorten experimental code

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[604e76d]1#include <stdlib.h>
2#include "c-stack.h"
3
[ac4dad2]4typedef struct node {
[81e8ab0]5 void * value;
[ac4dad2]6 struct node * next;
7} node;
8
9void copy_stack( stack * s, const stack * t, void * (*copy)( const void * ) ) {
10 node ** cr = &s->head;
11 for ( node * nx = t->head; nx; nx = nx->next ) {
12 *cr = malloc( sizeof(node) ); /***/
13 (*cr)->value = copy( nx->value );
14 cr = &(*cr)->next;
[604e76d]15 }
[ac4dad2]16 *cr = NULL;
[604e76d]17}
18
[ac4dad2]19void clear_stack( stack * s, void (* free_el)( void * ) ) {
20 for ( node * nx = s->head; nx; ) {
21 node * cr = nx;
22 nx = cr->next;
23 free_el( cr->value );
24 free( cr );
[604e76d]25 }
[ac4dad2]26 s->head = NULL;
[604e76d]27}
[ac4dad2]28
29stack new_stack() { return (stack){ NULL }; /***/ }
30
31stack * assign_stack( stack * s, const stack * t,
[8b001bd]32 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) {
[e59f0bf]33 if ( s->head == t->head ) return s;
34 clear_stack( s, free_el ); /***/
35 copy_stack( s, t, copy_el ); /***/
36 return s;
37}
[604e76d]38
[ac4dad2]39_Bool stack_empty( const stack * s ) { return s->head == NULL; }
[604e76d]40
[ac4dad2]41void push_stack( stack * s, void * v ) {
42 node * n = malloc( sizeof(node) ); /***/
43 *n = (node){ v, s->head }; /***/
[604e76d]44 s->head = n;
45}
46
[ac4dad2]47void * pop_stack( stack * s ) {
48 node * n = s->head;
[604e76d]49 s->head = n->next;
[81e8ab0]50 void * v = n->value;
51 free( n );
52 return v;
[604e76d]53}
Note: See TracBrowser for help on using the repository browser.