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 : Rob Schluntz
|
---|
12 | // Last Modified On : Tue Jun 14 15:45:03 2016
|
---|
13 | // Update Count : 1
|
---|
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
|
---|
24 | extern "C" {
|
---|
25 | typedef unsigned long int size_t;
|
---|
26 | #define NULL 0
|
---|
27 | void * malloc(size_t);
|
---|
28 | void free(void *);
|
---|
29 | #define assert(cond) if (! (cond)) { printf("Assertion failed: (%s) at %s:%d\n", #cond, __FILE__, __LINE__); abort(); }
|
---|
30 | void abort();
|
---|
31 | int printf(const char *, ...);
|
---|
32 | void *memset(void *s, int c, size_t n);
|
---|
33 | }
|
---|
34 |
|
---|
35 | // dummy type
|
---|
36 | struct init_once { int * x; };
|
---|
37 |
|
---|
38 | // array and operations
|
---|
39 | // const int size = 1024;
|
---|
40 | #define size 1024
|
---|
41 | struct array {
|
---|
42 | init_once * elems[size];
|
---|
43 | int length;
|
---|
44 | };
|
---|
45 | void remove(array * arr, init_once * x) {
|
---|
46 | for (int i = 0; i < arr->length; i++) {
|
---|
47 | if ( arr->elems[i] == x ) {
|
---|
48 | arr->elems[i] = arr->elems[--arr->length];
|
---|
49 | return;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | void insert(array * arr, init_once * x) {
|
---|
54 | assert( arr->length < size );
|
---|
55 | arr->elems[arr->length++] = x;
|
---|
56 | }
|
---|
57 | int find(array * arr, init_once * x) {
|
---|
58 | for (int i = 0; i < arr->length; i++) {
|
---|
59 | if ( arr->elems[i] == x ) {
|
---|
60 | return i;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | return -1;
|
---|
64 | }
|
---|
65 | void ?{}(array * arr) {
|
---|
66 | memset(arr->elems, 0, sizeof(arr->elems));
|
---|
67 | arr->length = 0;
|
---|
68 | }
|
---|
69 | array constructed;
|
---|
70 | array destructed;
|
---|
71 |
|
---|
72 | void ?{}(init_once * x) {
|
---|
73 | assert( find( &constructed, x ) == -1 );
|
---|
74 | remove( &destructed, x );
|
---|
75 | insert( &constructed, x );
|
---|
76 |
|
---|
77 | x->x = malloc(sizeof(int));
|
---|
78 | }
|
---|
79 |
|
---|
80 | void ?{}(init_once * x, init_once other) {
|
---|
81 | x{}; // reuse default ctor
|
---|
82 | }
|
---|
83 |
|
---|
84 | void ^?{}(init_once * x) {
|
---|
85 | assert( find( &destructed, x ) == -1 );
|
---|
86 | remove( &constructed, x );
|
---|
87 | insert( &destructed, x );
|
---|
88 |
|
---|
89 | free(x->x);
|
---|
90 | }
|
---|
91 | //*** end setup
|
---|
92 |
|
---|
93 | // test globals
|
---|
94 | init_once x;
|
---|
95 | init_once y = x;
|
---|
96 |
|
---|
97 | int main() {
|
---|
98 | // local variables
|
---|
99 | init_once x;
|
---|
100 | init_once y = x;
|
---|
101 |
|
---|
102 | // block scoped variables
|
---|
103 | {
|
---|
104 | init_once x;
|
---|
105 | init_once y = x;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // loop variables
|
---|
109 | for (int i = 0 ; i < 10; i++) {
|
---|
110 | init_once x;
|
---|
111 | init_once y = x;
|
---|
112 | }
|
---|
113 | int i = 0;
|
---|
114 | while (i < 10) {
|
---|
115 | init_once x;
|
---|
116 | init_once y = x;
|
---|
117 | i++;
|
---|
118 | }
|
---|
119 |
|
---|
120 | // declared in a switch block with a break
|
---|
121 | for (int i = 0; i < 10; i++) {
|
---|
122 | switch (10) {
|
---|
123 | case 1: {
|
---|
124 | init_once x;
|
---|
125 | init_once y = x;
|
---|
126 | (&x) {}; // ensure this doesn't execute
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | case 10: {
|
---|
130 | init_once x;
|
---|
131 | init_once y = x;
|
---|
132 | } // fall through
|
---|
133 | default: {
|
---|
134 | init_once x;
|
---|
135 | init_once y = x;
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | // labeled break/continue
|
---|
142 | L3: for (int k = 0; k < 10; k++) {
|
---|
143 | init_once x;
|
---|
144 | init_once y = x;
|
---|
145 | L1: for (int i = 0; i < 10; i++){
|
---|
146 | init_once x;
|
---|
147 | init_once y = x;
|
---|
148 | L2: for (int j = 0; j < 10; j++) {
|
---|
149 | init_once x;
|
---|
150 | init_once y = x;
|
---|
151 |
|
---|
152 | if (i == 0) continue L1;
|
---|
153 | if (i == 1) continue L2;
|
---|
154 | if (i == 2) break L2;
|
---|
155 | if (i == 3) break L1;
|
---|
156 | if (i == 4) continue L3;
|
---|
157 | if (i == 9) break L3;
|
---|
158 | // if (i == 5) goto ;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | // Local Variables: //
|
---|
167 | // tab-width: 4 //
|
---|
168 | // compile-command: "cfa init_once.c" //
|
---|
169 | // End: //
|
---|