1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LLVM_LICENSE_HEADER@
5 */
6
7/*
8 *  objectRRGC.c
9 *  testObjects
10 *
11 *  Created by Blaine Garst on 10/31/08.
12 *  Copyright 2008 __MyCompanyName__. All rights reserved.
13 *
14 * Test that the runtime honors the new callouts properly for retain/release and GC
15 */
16
17// TEST_DISABLED
18
19#include <stdio.h>
20#include <Block_private.h>
21#include "test.h"
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() {
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        fail("using old runtime layout!");
55    }
56    if ((bl->flags & BLOCK_HAS_COPY_DISPOSE) != BLOCK_HAS_COPY_DISPOSE) {
57        fail("no copy dispose!!!!");
58    }
59    // call helper routines directly.  These will, in turn, we hope, call the stubs above
60    long destBuffer[256];
61    //printf("destbuffer is at %p, block at %p\n", destBuffer, (void *)bl);
62    //printf("dump is %s\n", _Block_dump(myBlock));
63    bl->descriptor->copy(destBuffer, bl);
64    bl->descriptor->dispose(bl);
65    if (AssignCalled == 0) {
66        fail("did not call assign helper!");
67    }
68    if (DisposeCalled == 0) {
69        fail("did not call dispose helper");
70    }
71
72    succeed(__FILE__);
73}
74