Home | History | Annotate | Download | only in BlocksRuntime
      1 //
      2 //                     The LLVM Compiler Infrastructure
      3 //
      4 // This file is distributed under the University of Illinois Open Source
      5 // License. See LICENSE.TXT for details.
      6 
      7 #include <stdio.h>
      8 #include <Block.h>
      9 #include <Block_private.h>
     10 #include <stdlib.h>
     11 
     12 // CONFIG
     13 
     14 
     15 int cumulation = 0;
     16 
     17 int doSomething(int i) {
     18     cumulation += i;
     19     return cumulation;
     20 }
     21 
     22 void dirtyStack() {
     23     int i = random();
     24     int j = doSomething(i);
     25     int k = doSomething(j);
     26     doSomething(i + j + k);
     27 }
     28 
     29 typedef void (^voidVoid)(void);
     30 
     31 voidVoid testFunction() {
     32     int i = random();
     33     __block voidVoid inner = ^{ doSomething(i); };
     34     //printf("inner, on stack, is %p\n", (void*)inner);
     35     /*__block*/ voidVoid outer = ^{
     36         //printf("will call inner block %p\n", (void *)inner);
     37         inner();
     38     };
     39     //printf("outer looks like: %s\n", _Block_dump(outer));
     40     voidVoid result = Block_copy(outer);
     41     //Block_release(inner);
     42     return result;
     43 }
     44 
     45 
     46 int main(int argc, char **argv) {
     47     voidVoid block = testFunction();
     48     dirtyStack();
     49     block();
     50     Block_release(block);
     51 
     52     printf("%s: success\n", argv[0]);
     53 
     54     return 0;
     55 }
     56