source: src/tests/init_once.c@ 3f869f0

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory 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 3f869f0 was 00b7cd3, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

update include files for test programs

  • Property mode set to 100644
File size: 3.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// init_once.c --
8//
9// Author : Rob Schluntz
10// Created On : Tue Jun 14 15:43:35 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 5 16:40:07 2016
13// Update Count : 2
14//
15
16// want to ensure ctor/dtor called at most once per object.
17// whole point of ctor/dtor is that you don't know what's in
18// memory when it's first called, so can't rely on member to
19// determine if this is true. instead, keep an array
20// of addresses that have been constructed and remove the element
21// when it's destructed (and vice-versa)
22
23//*** setup
24extern "C" {
25typedef unsigned long int size_t;
26#define NULL 0
27void * malloc(size_t);
28void free(void *);
29#define assert(cond) if (! (cond)) { printf("Assertion failed: (%s) at %s:%d\n", #cond, __FILE__, __LINE__); abort(); }
30int printf(const char *, ...);
31void *memset(void *s, int c, size_t n);
32}
33
34// dummy type
35struct init_once { int * x; };
36
37// array and operations
38// const int size = 1024;
39#define size 1024
40struct array {
41 init_once * elems[size];
42 int length;
43};
44void remove(array * arr, init_once * x) {
45 for (int i = 0; i < arr->length; i++) {
46 if ( arr->elems[i] == x ) {
47 arr->elems[i] = arr->elems[--arr->length];
48 return;
49 }
50 }
51}
52void insert(array * arr, init_once * x) {
53 assert( arr->length < size );
54 arr->elems[arr->length++] = x;
55}
56int find(array * arr, init_once * x) {
57 for (int i = 0; i < arr->length; i++) {
58 if ( arr->elems[i] == x ) {
59 return i;
60 }
61 }
62 return -1;
63}
64void ?{}(array * arr) {
65 memset(arr->elems, 0, sizeof(arr->elems));
66 arr->length = 0;
67}
68array constructed;
69array destructed;
70
71void ?{}(init_once * x) {
72 assert( find( &constructed, x ) == -1 );
73 remove( &destructed, x );
74 insert( &constructed, x );
75
76 x->x = malloc(sizeof(int));
77}
78
79void ?{}(init_once * x, init_once other) {
80 x{}; // reuse default ctor
81}
82
83void ^?{}(init_once * x) {
84 assert( find( &destructed, x ) == -1 );
85 remove( &constructed, x );
86 insert( &destructed, x );
87
88 free(x->x);
89}
90//*** end setup
91
92// test globals
93init_once x;
94init_once y = x;
95
96int main() {
97 // local variables
98 init_once x;
99 init_once y = x;
100
101 // block scoped variables
102 {
103 init_once x;
104 init_once y = x;
105 }
106
107 // loop variables
108 for (int i = 0 ; i < 10; i++) {
109 init_once x;
110 init_once y = x;
111 }
112 int i = 0;
113 while (i < 10) {
114 init_once x;
115 init_once y = x;
116 i++;
117 }
118
119 // declared in a switch block with a break
120 for (int i = 0; i < 10; i++) {
121 switch (10) {
122 case 1: {
123 init_once x;
124 init_once y = x;
125 (&x) {}; // ensure this doesn't execute
126 break;
127 }
128 case 10: {
129 init_once x;
130 init_once y = x;
131 } // fall through
132 default: {
133 init_once x;
134 init_once y = x;
135 break;
136 }
137 }
138 }
139
140 // labeled break/continue
141 L3: for (int k = 0; k < 10; k++) {
142 init_once x;
143 init_once y = x;
144 L1: for (int i = 0; i < 10; i++){
145 init_once x;
146 init_once y = x;
147 L2: for (int j = 0; j < 10; j++) {
148 init_once x;
149 init_once y = x;
150
151 if (i == 0) continue L1;
152 if (i == 1) continue L2;
153 if (i == 2) break L2;
154 if (i == 3) break L1;
155 if (i == 4) continue L3;
156 if (i == 9) break L3;
157 // if (i == 5) goto ;
158 }
159 }
160 }
161
162
163}
164
165// Local Variables: //
166// tab-width: 4 //
167// compile-command: "cfa init_once.c" //
168// End: //
Note: See TracBrowser for help on using the repository browser.