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