18c1441f8SAlexey Samsonov //
2*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
4*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
58c1441f8SAlexey Samsonov
68c1441f8SAlexey Samsonov /*
78c1441f8SAlexey Samsonov * objectRRGC.c
88c1441f8SAlexey Samsonov * testObjects
98c1441f8SAlexey Samsonov *
108c1441f8SAlexey Samsonov * Created by Blaine Garst on 10/31/08.
118c1441f8SAlexey Samsonov *
128c1441f8SAlexey Samsonov * Test that the runtime honors the new callouts properly for retain/release and GC
138c1441f8SAlexey Samsonov * CON FIG C rdar://6175959
148c1441f8SAlexey Samsonov */
158c1441f8SAlexey Samsonov
168c1441f8SAlexey Samsonov
178c1441f8SAlexey Samsonov
188c1441f8SAlexey Samsonov #include <stdio.h>
198c1441f8SAlexey Samsonov #include <Block_private.h>
208c1441f8SAlexey Samsonov
218c1441f8SAlexey Samsonov
228c1441f8SAlexey Samsonov int AssignCalled = 0;
238c1441f8SAlexey Samsonov int DisposeCalled = 0;
248c1441f8SAlexey Samsonov
258c1441f8SAlexey Samsonov // local copy instead of libSystem.B.dylib copy
_Block_object_assign(void * destAddr,const void * object,const int isWeak)268c1441f8SAlexey Samsonov void _Block_object_assign(void *destAddr, const void *object, const int isWeak) {
278c1441f8SAlexey Samsonov //printf("_Block_object_assign(%p, %p, %d) called\n", destAddr, object, isWeak);
288c1441f8SAlexey Samsonov AssignCalled = 1;
298c1441f8SAlexey Samsonov }
308c1441f8SAlexey Samsonov
_Block_object_dispose(const void * object,const int isWeak)318c1441f8SAlexey Samsonov void _Block_object_dispose(const void *object, const int isWeak) {
328c1441f8SAlexey Samsonov //printf("_Block_object_dispose(%p, %d) called\n", object, isWeak);
338c1441f8SAlexey Samsonov DisposeCalled = 1;
348c1441f8SAlexey Samsonov }
358c1441f8SAlexey Samsonov
368c1441f8SAlexey Samsonov struct MyStruct {
378c1441f8SAlexey Samsonov long isa;
388c1441f8SAlexey Samsonov long field;
398c1441f8SAlexey Samsonov };
408c1441f8SAlexey Samsonov
418c1441f8SAlexey Samsonov typedef struct MyStruct *__attribute__((NSObject)) MyStruct_t;
428c1441f8SAlexey Samsonov
main(int argc,char * argv[])438c1441f8SAlexey Samsonov int main(int argc, char *argv[]) {
448c1441f8SAlexey Samsonov // create a block
458c1441f8SAlexey Samsonov struct MyStruct X;
468c1441f8SAlexey Samsonov MyStruct_t xp = (MyStruct_t)&X;
478c1441f8SAlexey Samsonov xp->field = 10;
488c1441f8SAlexey Samsonov void (^myBlock)(void) = ^{ printf("field is %ld\n", xp->field); };
498c1441f8SAlexey Samsonov // should be a copy helper generated with a calls to above routines
508c1441f8SAlexey Samsonov // Lets find out!
518c1441f8SAlexey Samsonov struct Block_layout *bl = (struct Block_layout *)(void *)myBlock;
528c1441f8SAlexey Samsonov if ((bl->flags & BLOCK_HAS_DESCRIPTOR) != BLOCK_HAS_DESCRIPTOR) {
538c1441f8SAlexey Samsonov printf("using old runtime layout!\n");
548c1441f8SAlexey Samsonov return 1;
558c1441f8SAlexey Samsonov }
568c1441f8SAlexey Samsonov if ((bl->flags & BLOCK_HAS_COPY_DISPOSE) != BLOCK_HAS_COPY_DISPOSE) {
578c1441f8SAlexey Samsonov printf("no copy dispose!!!!\n");
588c1441f8SAlexey Samsonov return 1;
598c1441f8SAlexey Samsonov }
608c1441f8SAlexey Samsonov // call helper routines directly. These will, in turn, we hope, call the stubs above
618c1441f8SAlexey Samsonov long destBuffer[256];
628c1441f8SAlexey Samsonov //printf("destbuffer is at %p, block at %p\n", destBuffer, (void *)bl);
638c1441f8SAlexey Samsonov //printf("dump is %s\n", _Block_dump(myBlock));
648c1441f8SAlexey Samsonov bl->descriptor->copy(destBuffer, bl);
658c1441f8SAlexey Samsonov bl->descriptor->dispose(bl);
668c1441f8SAlexey Samsonov if (AssignCalled == 0) {
678c1441f8SAlexey Samsonov printf("did not call assign helper!\n");
688c1441f8SAlexey Samsonov return 1;
698c1441f8SAlexey Samsonov }
708c1441f8SAlexey Samsonov if (DisposeCalled == 0) {
718c1441f8SAlexey Samsonov printf("did not call dispose helper\n");
728c1441f8SAlexey Samsonov return 1;
738c1441f8SAlexey Samsonov }
748c1441f8SAlexey Samsonov printf("%s: Success!\n", argv[0]);
758c1441f8SAlexey Samsonov return 0;
768c1441f8SAlexey Samsonov }
77