blob: f7cad030764d2241853062ee9fb31193c18adf06 [file] [log] [blame]
Daniel Dunbar8cbe1632009-06-26 16:21:04 +00001/*
2 * Block_private.h
Daniel Dunbar8cbe1632009-06-26 16:21:04 +00003 *
Daniel Dunbarb3a69012009-06-26 16:47:03 +00004 * Copyright 2008-2009 Apple, Inc. Permission is hereby granted, free of charge,
5 * to any person obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the following
10 * conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000023 */
24
25#ifndef _BLOCK_PRIVATE_H_
26#define _BLOCK_PRIVATE_H_
27
28#if !defined(BLOCK_EXPORT)
29# if defined(__cplusplus)
30# define BLOCK_EXPORT extern "C"
31# else
32# define BLOCK_EXPORT extern
33# endif
34#endif
35
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000036#include <stdbool.h>
37
Shantonu Senb4c3b6f2009-09-22 00:49:12 +000038#if defined(__cplusplus)
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000039extern "C" {
40#endif
41
42
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000043enum {
44 BLOCK_REFCOUNT_MASK = (0xffff),
45 BLOCK_NEEDS_FREE = (1 << 24),
46 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
Edward O'Callaghan09870642009-09-12 15:47:39 +000047 BLOCK_HAS_CTOR = (1 << 26), /* Helpers have C++ code. */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000048 BLOCK_IS_GC = (1 << 27),
49 BLOCK_IS_GLOBAL = (1 << 28),
Edward O'Callaghan09870642009-09-12 15:47:39 +000050 BLOCK_HAS_DESCRIPTOR = (1 << 29)
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000051};
52
Edward O'Callaghan09870642009-09-12 15:47:39 +000053
54/* Revised new layout. */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000055struct Block_descriptor {
56 unsigned long int reserved;
57 unsigned long int size;
58 void (*copy)(void *dst, void *src);
59 void (*dispose)(void *);
60};
61
Edward O'Callaghan09870642009-09-12 15:47:39 +000062
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000063struct Block_layout {
64 void *isa;
65 int flags;
66 int reserved;
67 void (*invoke)(void *, ...);
68 struct Block_descriptor *descriptor;
Edward O'Callaghan09870642009-09-12 15:47:39 +000069 /* Imported variables. */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000070};
71
72
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000073struct Block_byref {
74 void *isa;
75 struct Block_byref *forwarding;
Edward O'Callaghan0898ee92009-08-05 19:57:20 +000076 int flags; /* refcount; */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000077 int size;
78 void (*byref_keep)(struct Block_byref *dst, struct Block_byref *src);
79 void (*byref_destroy)(struct Block_byref *);
Edward O'Callaghan0898ee92009-08-05 19:57:20 +000080 /* long shared[0]; */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000081};
82
Edward O'Callaghan09870642009-09-12 15:47:39 +000083
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000084struct Block_byref_header {
85 void *isa;
86 struct Block_byref *forwarding;
87 int flags;
88 int size;
89};
90
91
Edward O'Callaghan09870642009-09-12 15:47:39 +000092/* Runtime support functions used by compiler when generating copy/dispose helpers. */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +000093
94enum {
Edward O'Callaghan09870642009-09-12 15:47:39 +000095 /* See function implementation for a more complete description of these fields and combinations */
Edward O'Callaghan0898ee92009-08-05 19:57:20 +000096 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), block, ... */
97 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
98 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block variable */
99 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy helpers */
Edward O'Callaghan09870642009-09-12 15:47:39 +0000100 BLOCK_BYREF_CALLER = 128 /* called from __block (byref) copy/dispose support routines. */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000101};
102
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000103/* Runtime entry point called by compiler when assigning objects inside copy helper routines */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000104BLOCK_EXPORT void _Block_object_assign(void *destAddr, const void *object, const int flags);
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000105 /* BLOCK_FIELD_IS_BYREF is only used from within block copy helpers */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000106
107
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000108/* runtime entry point called by the compiler when disposing of objects inside dispose helper routine */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000109BLOCK_EXPORT void _Block_object_dispose(const void *object, const int flags);
110
111
112
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000113/* Other support functions */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000114
Edward O'Callaghan09870642009-09-12 15:47:39 +0000115/* Runtime entry to get total size of a closure */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000116BLOCK_EXPORT unsigned long int Block_size(void *block_basic);
117
118
119
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000120/* the raw data space for runtime classes for blocks */
121/* class+meta used for stack, malloc, and collectable based blocks */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000122BLOCK_EXPORT void * _NSConcreteStackBlock[32];
123BLOCK_EXPORT void * _NSConcreteMallocBlock[32];
124BLOCK_EXPORT void * _NSConcreteAutoBlock[32];
125BLOCK_EXPORT void * _NSConcreteFinalizingBlock[32];
126BLOCK_EXPORT void * _NSConcreteGlobalBlock[32];
127BLOCK_EXPORT void * _NSConcreteWeakBlockVariable[32];
128
129
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000130/* the intercept routines that must be used under GC */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000131BLOCK_EXPORT void _Block_use_GC( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),
132 void (*setHasRefcount)(const void *, const bool),
133 void (*gc_assign_strong)(void *, void **),
134 void (*gc_assign_weak)(const void *, void *),
135 void (*gc_memmove)(void *, void *, unsigned long));
136
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000137/* earlier version, now simply transitional */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000138BLOCK_EXPORT void _Block_use_GC5( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),
139 void (*setHasRefcount)(const void *, const bool),
140 void (*gc_assign_strong)(void *, void **),
141 void (*gc_assign_weak)(const void *, void *));
142
143BLOCK_EXPORT void _Block_use_RR( void (*retain)(const void *),
144 void (*release)(const void *));
145
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000146/* make a collectable GC heap based Block. Not useful under non-GC. */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000147BLOCK_EXPORT void *_Block_copy_collectable(const void *aBlock);
148
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000149/* thread-unsafe diagnostic */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000150BLOCK_EXPORT const char *_Block_dump(const void *block);
151
152
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000153/* Obsolete */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000154
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000155/* first layout */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000156struct Block_basic {
157 void *isa;
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000158 int Block_flags; /* int32_t */
Edward O'Callaghan09870642009-09-12 15:47:39 +0000159 int Block_size; /* XXX should be packed into Block_flags */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000160 void (*Block_invoke)(void *);
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000161 void (*Block_copy)(void *dst, void *src); /* iff BLOCK_HAS_COPY_DISPOSE */
162 void (*Block_dispose)(void *); /* iff BLOCK_HAS_COPY_DISPOSE */
163 /* long params[0]; // where const imports, __block storage references, etc. get laid down */
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000164};
165
166
Shantonu Senb4c3b6f2009-09-22 00:49:12 +0000167#if defined(__cplusplus)
Daniel Dunbar8cbe1632009-06-26 16:21:04 +0000168}
169#endif
170
171
Shantonu Senb4c3b6f2009-09-22 00:49:12 +0000172#endif /* _BLOCK_PRIVATE_H_ */