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
|
---|
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 | int printf(const char *, ...);
|
---|
31 | void *memset(void *s, int c, size_t n);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // dummy type
|
---|
35 | struct init_once { int * x; };
|
---|
36 |
|
---|
37 | // array and operations
|
---|
38 | // const int size = 1024;
|
---|
39 | #define size 1024
|
---|
40 | struct array {
|
---|
41 | init_once * elems[size];
|
---|
42 | int length;
|
---|
43 | };
|
---|
44 | void 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 | }
|
---|
52 | void insert(array * arr, init_once * x) {
|
---|
53 | assert( arr->length < size );
|
---|
54 | arr->elems[arr->length++] = x;
|
---|
55 | }
|
---|
56 | int 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 | }
|
---|
64 | void ?{}(array * arr) {
|
---|
65 | memset(arr->elems, 0, sizeof(arr->elems));
|
---|
66 | arr->length = 0;
|
---|
67 | }
|
---|
68 | array constructed;
|
---|
69 | array destructed;
|
---|
70 |
|
---|
71 | void ?{}(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 |
|
---|
79 | void ?{}(init_once * x, init_once other) {
|
---|
80 | x{}; // reuse default ctor
|
---|
81 | }
|
---|
82 |
|
---|
83 | void ^?{}(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
|
---|
93 | init_once x;
|
---|
94 | init_once y = x;
|
---|
95 |
|
---|
96 | int 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: //
|
---|