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