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