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-exception
5
6/*
7 * objectRRGC.c
8 * testObjects
9 *
10 * Created by Blaine Garst on 10/31/08.
11 *
12 * Test that the runtime honors the new callouts properly for retain/release and GC
13 * CON FIG C rdar://6175959
14 */
15
16
17
18#include <stdio.h>
19#include <Block_private.h>
20
21
22int AssignCalled = 0;
23int DisposeCalled = 0;
24
25// local copy instead of libSystem.B.dylib copy
26void _Block_object_assign(void *destAddr, const void *object, const int isWeak) {
27 //printf("_Block_object_assign(%p, %p, %d) called\n", destAddr, object, isWeak);
28 AssignCalled = 1;
29}
30
31void _Block_object_dispose(const void *object, const int isWeak) {
32 //printf("_Block_object_dispose(%p, %d) called\n", object, isWeak);
33 DisposeCalled = 1;
34}
35
36struct MyStruct {
37 long isa;
38 long field;
39};
40
41typedef struct MyStruct *__attribute__((NSObject)) MyStruct_t;
42
43int main(int argc, char *argv[]) {
44 // create a block
45 struct MyStruct X;
46 MyStruct_t xp = (MyStruct_t)&X;
47 xp->field = 10;
48 void (^myBlock)(void) = ^{ printf(format: "field is %ld\n", xp->field); };
49 // should be a copy helper generated with a calls to above routines
50 // Lets find out!
51 struct Block_layout *bl = (struct Block_layout *)(void *)myBlock;
52 if ((bl->flags & BLOCK_HAS_DESCRIPTOR) != BLOCK_HAS_DESCRIPTOR) {
53 printf(format: "using old runtime layout!\n");
54 return 1;
55 }
56 if ((bl->flags & BLOCK_HAS_COPY_DISPOSE) != BLOCK_HAS_COPY_DISPOSE) {
57 printf(format: "no copy dispose!!!!\n");
58 return 1;
59 }
60 // call helper routines directly. These will, in turn, we hope, call the stubs above
61 long destBuffer[256];
62 //printf("destbuffer is at %p, block at %p\n", destBuffer, (void *)bl);
63 //printf("dump is %s\n", _Block_dump(myBlock));
64 bl->descriptor->copy(destBuffer, bl);
65 bl->descriptor->dispose(bl);
66 if (AssignCalled == 0) {
67 printf(format: "did not call assign helper!\n");
68 return 1;
69 }
70 if (DisposeCalled == 0) {
71 printf(format: "did not call dispose helper\n");
72 return 1;
73 }
74 printf(format: "%s: Success!\n", argv[0]);
75 return 0;
76}
77

source code of compiler-rt/test/BlocksRuntime/objectRRGC.c