2009-06-11 02:41:48 +08:00
|
|
|
/*
|
|
|
|
* Block_private.h
|
|
|
|
*
|
2009-06-27 00:47:03 +08:00
|
|
|
* Copyright 2008-2009 Apple, Inc. Permission is hereby granted, free of charge,
|
|
|
|
* to any person obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
* persons to whom the Software is furnished to do so, subject to the following
|
|
|
|
* conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
2009-06-11 02:41:48 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BLOCK_PRIVATE_H_
|
|
|
|
#define _BLOCK_PRIVATE_H_
|
|
|
|
|
|
|
|
#if !defined(BLOCK_EXPORT)
|
|
|
|
# if defined(__cplusplus)
|
|
|
|
# define BLOCK_EXPORT extern "C"
|
|
|
|
# else
|
|
|
|
# define BLOCK_EXPORT extern
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2009-09-22 08:49:12 +08:00
|
|
|
#if defined(__cplusplus)
|
2009-06-11 02:41:48 +08:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
BLOCK_REFCOUNT_MASK = (0xffff),
|
|
|
|
BLOCK_NEEDS_FREE = (1 << 24),
|
|
|
|
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
|
2009-09-12 23:47:39 +08:00
|
|
|
BLOCK_HAS_CTOR = (1 << 26), /* Helpers have C++ code. */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_IS_GC = (1 << 27),
|
|
|
|
BLOCK_IS_GLOBAL = (1 << 28),
|
2009-09-12 23:47:39 +08:00
|
|
|
BLOCK_HAS_DESCRIPTOR = (1 << 29)
|
2009-06-11 02:41:48 +08:00
|
|
|
};
|
|
|
|
|
2009-09-12 23:47:39 +08:00
|
|
|
|
|
|
|
/* Revised new layout. */
|
2009-06-11 02:41:48 +08:00
|
|
|
struct Block_descriptor {
|
|
|
|
unsigned long int reserved;
|
|
|
|
unsigned long int size;
|
|
|
|
void (*copy)(void *dst, void *src);
|
|
|
|
void (*dispose)(void *);
|
|
|
|
};
|
|
|
|
|
2009-09-12 23:47:39 +08:00
|
|
|
|
2009-06-11 02:41:48 +08:00
|
|
|
struct Block_layout {
|
|
|
|
void *isa;
|
|
|
|
int flags;
|
|
|
|
int reserved;
|
|
|
|
void (*invoke)(void *, ...);
|
|
|
|
struct Block_descriptor *descriptor;
|
2009-09-12 23:47:39 +08:00
|
|
|
/* Imported variables. */
|
2009-06-11 02:41:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Block_byref {
|
|
|
|
void *isa;
|
|
|
|
struct Block_byref *forwarding;
|
2009-08-06 03:57:20 +08:00
|
|
|
int flags; /* refcount; */
|
2009-06-11 02:41:48 +08:00
|
|
|
int size;
|
|
|
|
void (*byref_keep)(struct Block_byref *dst, struct Block_byref *src);
|
|
|
|
void (*byref_destroy)(struct Block_byref *);
|
2009-08-06 03:57:20 +08:00
|
|
|
/* long shared[0]; */
|
2009-06-11 02:41:48 +08:00
|
|
|
};
|
|
|
|
|
2009-09-12 23:47:39 +08:00
|
|
|
|
2009-06-11 02:41:48 +08:00
|
|
|
struct Block_byref_header {
|
|
|
|
void *isa;
|
|
|
|
struct Block_byref *forwarding;
|
|
|
|
int flags;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-09-12 23:47:39 +08:00
|
|
|
/* Runtime support functions used by compiler when generating copy/dispose helpers. */
|
2009-06-11 02:41:48 +08:00
|
|
|
|
|
|
|
enum {
|
2009-09-12 23:47:39 +08:00
|
|
|
/* See function implementation for a more complete description of these fields and combinations */
|
2009-08-06 03:57:20 +08:00
|
|
|
BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), block, ... */
|
|
|
|
BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
|
|
|
|
BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block variable */
|
|
|
|
BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy helpers */
|
2009-09-12 23:47:39 +08:00
|
|
|
BLOCK_BYREF_CALLER = 128 /* called from __block (byref) copy/dispose support routines. */
|
2009-06-11 02:41:48 +08:00
|
|
|
};
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* Runtime entry point called by compiler when assigning objects inside copy helper routines */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT void _Block_object_assign(void *destAddr, const void *object, const int flags);
|
2009-08-06 03:57:20 +08:00
|
|
|
/* BLOCK_FIELD_IS_BYREF is only used from within block copy helpers */
|
2009-06-11 02:41:48 +08:00
|
|
|
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* runtime entry point called by the compiler when disposing of objects inside dispose helper routine */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT void _Block_object_dispose(const void *object, const int flags);
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* Other support functions */
|
2009-06-11 02:41:48 +08:00
|
|
|
|
2009-09-12 23:47:39 +08:00
|
|
|
/* Runtime entry to get total size of a closure */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT unsigned long int Block_size(void *block_basic);
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* the raw data space for runtime classes for blocks */
|
|
|
|
/* class+meta used for stack, malloc, and collectable based blocks */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT void * _NSConcreteStackBlock[32];
|
|
|
|
BLOCK_EXPORT void * _NSConcreteMallocBlock[32];
|
|
|
|
BLOCK_EXPORT void * _NSConcreteAutoBlock[32];
|
|
|
|
BLOCK_EXPORT void * _NSConcreteFinalizingBlock[32];
|
|
|
|
BLOCK_EXPORT void * _NSConcreteGlobalBlock[32];
|
|
|
|
BLOCK_EXPORT void * _NSConcreteWeakBlockVariable[32];
|
|
|
|
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* the intercept routines that must be used under GC */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT void _Block_use_GC( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),
|
|
|
|
void (*setHasRefcount)(const void *, const bool),
|
|
|
|
void (*gc_assign_strong)(void *, void **),
|
|
|
|
void (*gc_assign_weak)(const void *, void *),
|
|
|
|
void (*gc_memmove)(void *, void *, unsigned long));
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* earlier version, now simply transitional */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT void _Block_use_GC5( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),
|
|
|
|
void (*setHasRefcount)(const void *, const bool),
|
|
|
|
void (*gc_assign_strong)(void *, void **),
|
|
|
|
void (*gc_assign_weak)(const void *, void *));
|
|
|
|
|
|
|
|
BLOCK_EXPORT void _Block_use_RR( void (*retain)(const void *),
|
|
|
|
void (*release)(const void *));
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* make a collectable GC heap based Block. Not useful under non-GC. */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT void *_Block_copy_collectable(const void *aBlock);
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* thread-unsafe diagnostic */
|
2009-06-11 02:41:48 +08:00
|
|
|
BLOCK_EXPORT const char *_Block_dump(const void *block);
|
|
|
|
|
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* Obsolete */
|
2009-06-11 02:41:48 +08:00
|
|
|
|
2009-08-06 03:57:20 +08:00
|
|
|
/* first layout */
|
2009-06-11 02:41:48 +08:00
|
|
|
struct Block_basic {
|
|
|
|
void *isa;
|
2009-08-06 03:57:20 +08:00
|
|
|
int Block_flags; /* int32_t */
|
2009-09-12 23:47:39 +08:00
|
|
|
int Block_size; /* XXX should be packed into Block_flags */
|
2009-06-11 02:41:48 +08:00
|
|
|
void (*Block_invoke)(void *);
|
2009-08-06 03:57:20 +08:00
|
|
|
void (*Block_copy)(void *dst, void *src); /* iff BLOCK_HAS_COPY_DISPOSE */
|
|
|
|
void (*Block_dispose)(void *); /* iff BLOCK_HAS_COPY_DISPOSE */
|
|
|
|
/* long params[0]; // where const imports, __block storage references, etc. get laid down */
|
2009-06-11 02:41:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-09-22 08:49:12 +08:00
|
|
|
#if defined(__cplusplus)
|
2009-06-11 02:41:48 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-09-22 08:49:12 +08:00
|
|
|
#endif /* _BLOCK_PRIVATE_H_ */
|