55 lines · c
1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5 6#include <stdio.h>7#include <Block.h>8#include <Block_private.h>9#include <stdlib.h>10 11// CONFIG12 13 14int cumulation = 0;15 16int doSomething(int i) {17 cumulation += i;18 return cumulation;19}20 21void dirtyStack() {22 int i = random();23 int j = doSomething(i);24 int k = doSomething(j);25 doSomething(i + j + k);26}27 28typedef void (^voidVoid)(void);29 30voidVoid testFunction() {31 int i = random();32 __block voidVoid inner = ^{ doSomething(i); };33 //printf("inner, on stack, is %p\n", (void*)inner);34 /*__block*/ voidVoid outer = ^{35 //printf("will call inner block %p\n", (void *)inner);36 inner();37 };38 //printf("outer looks like: %s\n", _Block_dump(outer));39 voidVoid result = Block_copy(outer);40 //Block_release(inner);41 return result;42}43 44 45int main(int argc, char **argv) {46 voidVoid block = testFunction();47 dirtyStack();48 block();49 Block_release(block);50 51 printf("%s: success\n", argv[0]);52 53 return 0;54}55