[ create a new paste ] login | about

Link: http://codepad.org/YLV13z1K    [ raw code | output | fork ]

C, pasted on Jun 23:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  /*
   * To split into separate files, remove all
   * lines containing 'ALL_IN_ONE'
   */
  #define ALL_IN_ONE 1

  /****
   * cleanup.h
   * 2011, Shao Miller
   */
  #ifndef CLEANUP_H_
  #include <stddef.h>

  /*** Macros */
  #define CLEANUP_H_

  /**
   * Block cleanup details
   */
  #define CLEANUP_BLOCK_ cleanup_do_(cleanup_);

  /**
   * Function cleanup details
   */
  #define CLEANUP_FUNC_ cleanup_chain_(cleanup_);

  /**
   * Perform block cleanup and jump to a label.
   * Only use for jumping from an inner to an
   * outer scope.
   * Note the trailing space after 'goto'.
   */
  #define c_goto                \
    switch (1)                  \
      while (1)                 \
        if (0) {                \
            default: {          \
                CLEANUP_BLOCK_; \
              }                 \
          } else                \
          goto 

  /**
   * Perform block cleanup and continue a loop
   */
  #define c_continue        \
    if (1) {                \
        CLEANUP_BLOCK_;     \
        continue;           \
      } else do ; while (0)

  /**
   * Perform block cleanup and break out of a
   * a loop or 'switch' statement
   */
  #define c_break           \
    if (1) {                \
        CLEANUP_BLOCK_;     \
        break;              \
      } else do ; while (0)

  /**
   * Perform function cleanup and return from
   * a function.
   * Note the trailing space after 'goto'
   */
  #define c_return              \
    switch (1)                  \
      while (1)                 \
        if (0) {                \
            default: {          \
                CLEANUP_FUNC_;  \
              }                 \
          } else                \
          return 

  /**
   * An integer constant expression which
   * declares an enumeration value that is one
   * greater than the same-name enumeration
   * value in the containing scope.  This
   * is for internal use; don't use it
   */
  #define CV_CLEANUP_ ((enum {      \
      cv_cleanup_ = cv_cleanup_ + 1 \
    })1)

  /**
   * This macro establishes cleanup context for
   * a function or compound statement.  Use it
   * only where declarations are allowed, such as
   * at the beginning of a function or compound
   * statement in C89.
   */
  #define HAS_CLEANUP                    \
    cleanup_t_                           \
      new_cleanup_[CV_CLEANUP_] = {{0}}, \
      * p_new_cleanup_ = cleanup_init_(  \
          new_cleanup_,                  \
          cleanup_,                      \
          cv_cleanup_                    \
        ),                               \
      * cleanup_ = p_new_cleanup_

  #ifndef ALL_IN_ONE
  /* Wrap malloc */
  #ifdef malloc
  #undef malloc
  #endif
  #define malloc(size) c_malloc_(cleanup_, (size))
  #endif

  /*** Constants */
  enum { cv_cleanup_ };

  /*** Object types */

  /* Internal use; don't use it */
  struct s_cleanup_ {
      struct s_cleanup_ * prev;
      void * item;
    };
  typedef struct s_cleanup_ cleanup_t_;

  /*** Objects */

  /**
   * Top-level cleanup context (empty).
   * Internal use; don't use it
   */
  extern cleanup_t_ cleanup_[1];

  /*** Functions */
  
  /* Internal use; don't use these */
  extern cleanup_t_ * cleanup_init_(
      cleanup_t_ *,
      cleanup_t_ *,
      int
    );
  extern void * c_malloc_(cleanup_t_ *, size_t);
  extern void cleanup_do_(cleanup_t_ *);
  extern void cleanup_chain_(cleanup_t_ *);

  #endif /* CLEANUP_H_ */


  /****
   * cleanup.c
   * 2011, Shao Miller
   */
  #include <stdlib.h>
  #include <stdio.h>
  #ifndef ALL_IN_ONE
  #include "cleanup.h"
  #endif /* ALL_IN_ONE */

  /*** Object types */

  /* A cleanable item */
  struct s_cleanup_item_ {
      void * ref;
      struct s_cleanup_item_ * next;
    };
  typedef struct s_cleanup_item_ s_cleanup_item;

  /*** Objects */
  cleanup_t_ cleanup_[1] = {{0, 0}};

  /*** Functions */
  cleanup_t_ * cleanup_init_(
      cleanup_t_ * new,
      cleanup_t_ * prev,
      int level
    ) {
      new->prev = prev;
      return new;
    }

  void * c_malloc_(
      cleanup_t_ * cleanup,
      size_t size
    ) {
      s_cleanup_item * item;
      void * obj;

      item = (malloc)(sizeof *item);
      if (!item)
        return item;

      obj = (malloc)(size);
      if (!obj) {
          free(item);
          return obj;
        }

      item->ref = obj;
      /* Insert at beginning */
      item->next = cleanup->item;
      cleanup->item = item;
      return obj;
    }

  static char * at_root(cleanup_t_ * cleanup) {
      if (cleanup == cleanup_)
        return " (root)";
      return "";
    }

  void cleanup_do_(cleanup_t_ * cleanup) {
      s_cleanup_item * item, * next;

      printf(
          "Cleanup @ %p%s\n",
          (void *) cleanup,
          at_root(cleanup)
        );
      item = cleanup->item;
      cleanup->item = 0;
      while (item) {
          next = item->next;
          printf("Freeing object @ %p\n", item->ref);
          free(item->ref);
          free(item);
          item = next;
        }
      return;
    }

  void cleanup_chain_(cleanup_t_ * cleanup) {
      printf(
          "Cleaning chain @ %p%s\n",
          (void *) cleanup,
          at_root(cleanup)
        );
      while (cleanup) {
          cleanup_t_ * next;

          cleanup_do_(cleanup);
          next = cleanup->prev;
          cleanup->prev = 0;
          cleanup = next;
        }
      return;
    }


  /**** Test program */

  #ifndef ALL_IN_ONE
  #include <stdio.h>
  #include "cleanup.h"
  #endif /* ALL_IN_ONE */

  #ifndef malloc
  /* Wrap malloc the same as cleanup.h does */
  #ifdef malloc
  #undef malloc
  #endif
  #define malloc(size) c_malloc_(cleanup_, (size))
  #endif

  /*** Functions */

  /* Dummy */
  int true_func(int x) { return x + 1; }

  void test1_c_goto(void) {
      puts("\ntest1_c_goto():");
      /* Inner scope, below */
      {
          HAS_CLEANUP;
          int * ip1 = malloc(sizeof *ip1);
          int * ip2 = malloc(sizeof *ip2);

          printf("ip1: %p\n", (void *) ip1);
          printf("ip2: %p\n", (void *) ip2);
          /* Used in an 'if'-'else' */
          if (true_func(0))
            c_goto outer_scope;
            else
            puts("Failed!");
          return;
        }
      outer_scope:
      puts("Exiting");
      return;
    }

  void test2_c_goto(void) {
      puts("\ntest2_c_goto():");
      /* Inner scope, below */
      {
          HAS_CLEANUP;
          int * ip1 = malloc(sizeof *ip1);
          int * ip2 = malloc(sizeof *ip2);

          printf("ip1: %p\n", (void *) ip1);
          printf("ip2: %p\n", (void *) ip2);
          c_goto outer_scope;
          puts("Failed!");
          return;
        }
      outer_scope:
      puts("Exiting");
      return;
    }

  void test3_c_continue(void) {
      /* Expecting: 4 loops */
      int x = 5;

      puts("\ntest3_c_continue():");
      while (--x) {
          HAS_CLEANUP;
          int * ip1 = malloc(sizeof *ip1);
          int * ip2 = malloc(sizeof *ip2);

          printf("ip1: %p\n", (void *) ip1);
          printf("ip2: %p\n", (void *) ip2);
          if (true_func(0))
            c_continue;
            else
            puts("Oops!");
          puts("Failed!");
          return;
        }
      puts("Exiting");
      return;
    }

  void test4_c_break(void) {
      /* Expecting: 1 loop */
      int x = 5;

      puts("\ntest4_c_break():");
      while (--x) {
          HAS_CLEANUP;
          int * ip1 = malloc(sizeof *ip1);
          int * ip2 = malloc(sizeof *ip2);

          printf("ip1: %p\n", (void *) ip1);
          printf("ip2: %p\n", (void *) ip2);
          if (true_func(0))
            c_break;
            else
            puts("Oops!");
          puts("Failed!");
          return;
        }
      puts("Exiting");
      return;
    }

  /** Function "returning 'void'" */
  void test5_c_return(void) {
      HAS_CLEANUP;
      int * ip1 = malloc(sizeof *ip1);
      int * ip2 = malloc(sizeof *ip2);

      puts("\ntest5_c_return():");
      /* Inner scope, below */
      {
          HAS_CLEANUP;
          int * ip3 = malloc(sizeof *ip3);
          int * ip4 = malloc(sizeof *ip4);

          printf("ip1: %p\n", (void *) ip1);
          printf("ip2: %p\n", (void *) ip2);
          printf("ip3: %p\n", (void *) ip3);
          printf("ip4: %p\n", (void *) ip4);
          if (true_func(0)) {
              puts("Attempting c_return");
              c_return;
            } else
            puts("Oops!");
        }
      puts("Failed!");
      return;
    }

  /** Function returning 'int' */
  int test6_c_return(void) {
      HAS_CLEANUP;
      int * ip1 = malloc(sizeof *ip1);
      int * ip2 = malloc(sizeof *ip2);

      puts("\ntest6_c_return():");
      /* Inner scope, below */
      {
          HAS_CLEANUP;
          int * ip3 = malloc(sizeof *ip3);
          int * ip4 = malloc(sizeof *ip4);

          printf("ip1: %p\n", (void *) ip1);
          printf("ip2: %p\n", (void *) ip2);
          printf("ip3: %p\n", (void *) ip3);
          printf("ip4: %p\n", (void *) ip4);
          if (true_func(0)) {
              puts("Attempting c_return");
              c_return 1;
            } else
            puts("Oops!");
        }
      puts("Failed!");
      return 0;
    }

  /** Function returning 'int' */
  int test7_c_return(void) {
      HAS_CLEANUP;
      int * ip1 = malloc(sizeof *ip1);
      int * ip2 = malloc(sizeof *ip2);

      puts("\ntest7_c_return():");
      /* Inner scope, below */
      {
          HAS_CLEANUP;
          int * ip3 = malloc(sizeof *ip3);
          int * ip4 = malloc(sizeof *ip4);

          /* Another inner scope, below */
          {
              HAS_CLEANUP;
              int * ip5 = malloc(sizeof *ip5);
              int * ip6 = malloc(sizeof *ip6);

              printf("ip1: %p\n", (void *) ip1);
              printf("ip2: %p\n", (void *) ip2);
              printf("ip3: %p\n", (void *) ip3);
              printf("ip4: %p\n", (void *) ip4);
              printf("ip5: %p\n", (void *) ip5);
              printf("ip6: %p\n", (void *) ip6);
              if (true_func(0)) {
                  puts("Attempting c_return");
                  c_return 1;
                } else
                puts("Oops!");
            }
        }
      puts("Failed!");
      return 0;
    }

  /** Program entry-point */
  int main(void) {
      test1_c_goto();
      test2_c_goto();
      test3_c_continue();
      test4_c_break();
      test5_c_return();
      if (!test6_c_return())
        puts("Failed!");
      if (!test7_c_return())
        puts("Failed!");
      return 0;
    }


Output:

test1_c_goto():
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Exiting

test2_c_goto():
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Exiting

test3_c_continue():
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Exiting

test4_c_break():
ip1: 0x804b0a0
ip2: 0x804b0f0
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Exiting

test5_c_return():
ip1: 0x804b0a0
ip2: 0x804b0f0
ip3: 0x804b140
ip4: 0x804b190
Attempting c_return
Cleaning chain @ 0xbf9f2c88
Cleanup @ 0xbf9f2c88
Freeing object @ 0x804b190
Freeing object @ 0x804b140
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Cleanup @ 0x804a2ec (root)

test6_c_return():
ip1: 0x804b0a0
ip2: 0x804b0f0
ip3: 0x804b140
ip4: 0x804b190
Attempting c_return
Cleaning chain @ 0xbf9f2c88
Cleanup @ 0xbf9f2c88
Freeing object @ 0x804b190
Freeing object @ 0x804b140
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Cleanup @ 0x804a2ec (root)

test7_c_return():
ip1: 0x804b0a0
ip2: 0x804b0f0
ip3: 0x804b140
ip4: 0x804b190
ip5: 0x804b1e0
ip6: 0x804b230
Attempting c_return
Cleaning chain @ 0xbf9f2c80
Cleanup @ 0xbf9f2c80
Freeing object @ 0x804b230
Freeing object @ 0x804b1e0
Cleanup @ 0xbf9f2c88
Freeing object @ 0x804b190
Freeing object @ 0x804b140
Cleanup @ 0xbf9f2c90
Freeing object @ 0x804b0f0
Freeing object @ 0x804b0a0
Cleanup @ 0x804a2ec (root)


Create a new paste based on this one


Comments: