2009-09-08 16:18:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Remy Demarest
|
|
|
|
* Portions Copyright (c) 2009 David Chisnall
|
2012-03-11 21:45:08 +00:00
|
|
|
*
|
2009-09-08 16:18:59 +00:00
|
|
|
* 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:
|
2012-03-11 21:45:08 +00:00
|
|
|
*
|
2009-09-08 16:18:59 +00:00
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
2012-03-11 21:45:08 +00:00
|
|
|
*
|
2009-09-08 16:18:59 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#import "objc/blocks_runtime.h"
|
|
|
|
#import "objc/runtime.h"
|
2011-07-04 20:58:46 +00:00
|
|
|
#import "objc/objc-arc.h"
|
2011-10-09 19:51:54 +00:00
|
|
|
#include "blocks_runtime.h"
|
2011-05-23 16:24:45 +00:00
|
|
|
#include "gc_ops.h"
|
2011-07-04 23:27:29 +00:00
|
|
|
#include "visibility.h"
|
2009-09-08 16:18:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2010-10-26 15:44:43 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2010-10-01 13:11:48 +00:00
|
|
|
|
2011-10-09 19:51:54 +00:00
|
|
|
static void *_HeapBlockByRef = (void*)1;
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2011-02-13 11:18:30 +00:00
|
|
|
|
2010-10-26 15:44:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the Objective-C type encoding for the block.
|
|
|
|
*/
|
2010-10-01 13:11:48 +00:00
|
|
|
const char *block_getType_np(void *b)
|
|
|
|
{
|
2012-03-11 21:45:08 +00:00
|
|
|
struct Block_layout *block = b;
|
2010-10-01 13:11:48 +00:00
|
|
|
if ((NULL == block) || !(block->flags & BLOCK_HAS_SIGNATURE))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(block->flags & BLOCK_HAS_COPY_DISPOSE))
|
|
|
|
{
|
2012-03-11 21:45:08 +00:00
|
|
|
return ((struct Block_descriptor_basic*)block->descriptor)->encoding;
|
2010-10-01 13:11:48 +00:00
|
|
|
}
|
|
|
|
return block->descriptor->encoding;
|
|
|
|
}
|
|
|
|
|
2010-10-26 15:44:43 +00:00
|
|
|
static int increment24(int *ref)
|
|
|
|
{
|
|
|
|
int old = *ref;
|
|
|
|
int val = old & BLOCK_REFCOUNT_MASK;
|
|
|
|
// FIXME: We should gracefully handle refcount overflow, but for now we
|
|
|
|
// just give up
|
|
|
|
assert(val < BLOCK_REFCOUNT_MASK);
|
|
|
|
if (!__sync_bool_compare_and_swap(ref, old, old+1))
|
|
|
|
{
|
|
|
|
return increment24(ref);
|
|
|
|
}
|
|
|
|
return val + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decrement24(int *ref)
|
|
|
|
{
|
|
|
|
int old = *ref;
|
|
|
|
int val = old & BLOCK_REFCOUNT_MASK;
|
|
|
|
// FIXME: We should gracefully handle refcount overflow, but for now we
|
|
|
|
// just give up
|
|
|
|
assert(val > 0);
|
|
|
|
if (!__sync_bool_compare_and_swap(ref, old, old-1))
|
|
|
|
{
|
|
|
|
return decrement24(ref);
|
|
|
|
}
|
|
|
|
return val - 1;
|
|
|
|
}
|
2010-10-01 13:11:48 +00:00
|
|
|
|
2011-02-13 11:18:30 +00:00
|
|
|
// This is a really ugly hack that works around a buggy register allocator in
|
|
|
|
// GCC. Compiling nontrivial code using __sync_bool_compare_and_swap() with
|
|
|
|
// GCC (4.2.1, at least), causes the register allocator to run out of registers
|
|
|
|
// and fall over and die. We work around this by wrapping this CAS in a
|
|
|
|
// function, which means the register allocator can trivially handle it. Do
|
|
|
|
// not remove the noinline attribute - without it, gcc will inline it early on
|
|
|
|
// and then crash later.
|
|
|
|
#ifndef __clang__
|
|
|
|
__attribute__((noinline))
|
|
|
|
static int cas(void *ptr, void *old, void *new)
|
|
|
|
{
|
|
|
|
return __sync_bool_compare_and_swap((void**)ptr, old, new);
|
|
|
|
}
|
|
|
|
#define __sync_bool_compare_and_swap cas
|
|
|
|
#endif
|
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
/* Certain field types require runtime assistance when being copied to the
|
|
|
|
* heap. The following function is used to copy fields of types: blocks,
|
|
|
|
* pointers to byref structures, and objects (including
|
|
|
|
* __attribute__((NSObject)) pointers. BLOCK_FIELD_IS_WEAK is orthogonal to
|
|
|
|
* the other choices which are mutually exclusive. Only in a Block copy helper
|
|
|
|
* will one see BLOCK_FIELD_IS_BYREF.
|
|
|
|
*/
|
2010-09-09 10:43:42 +00:00
|
|
|
void _Block_object_assign(void *destAddr, const void *object, const int flags)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
|
|
|
//printf("Copying %x to %x with flags %x\n", object, destAddr, flags);
|
2010-09-09 10:43:42 +00:00
|
|
|
// FIXME: Needs to be implemented
|
2010-10-26 15:44:43 +00:00
|
|
|
//if(flags & BLOCK_FIELD_IS_WEAK)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
|
|
|
}
|
2010-10-26 15:44:43 +00:00
|
|
|
//else
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-06-29 10:41:26 +00:00
|
|
|
if (IS_SET(flags, BLOCK_FIELD_IS_BYREF))
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2010-10-26 15:44:43 +00:00
|
|
|
struct block_byref_obj *src = (struct block_byref_obj *)object;
|
|
|
|
struct block_byref_obj **dst = destAddr;
|
2011-06-29 10:41:26 +00:00
|
|
|
src = src->forwarding;
|
2012-03-11 21:45:08 +00:00
|
|
|
|
2010-10-26 15:44:43 +00:00
|
|
|
if ((src->flags & BLOCK_REFCOUNT_MASK) == 0)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-05-23 16:24:45 +00:00
|
|
|
*dst = gc->malloc(src->size);
|
2010-09-09 10:43:42 +00:00
|
|
|
memcpy(*dst, src, src->size);
|
2010-10-26 15:44:43 +00:00
|
|
|
(*dst)->isa = _HeapBlockByRef;
|
|
|
|
// Refcount must be two; one for the copy and one for the
|
|
|
|
// on-stack version that will point to it.
|
|
|
|
(*dst)->flags += 2;
|
2011-07-17 12:35:50 +00:00
|
|
|
if (IS_SET(src->flags, BLOCK_HAS_COPY_DISPOSE))
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-10-26 15:44:43 +00:00
|
|
|
src->byref_keep(*dst, src);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
2010-10-26 15:44:43 +00:00
|
|
|
(*dst)->forwarding = *dst;
|
|
|
|
// Concurrency. If we try copying the same byref structure
|
|
|
|
// from two threads simultaneously, we could end up with two
|
|
|
|
// versions on the heap that are unaware of each other. That
|
|
|
|
// would be bad. So we first set up the copy, then try to do
|
|
|
|
// an atomic compare-and-exchange to point the old version at
|
|
|
|
// it. If the forwarding pointer in src has changed, then we
|
|
|
|
// recover - clean up and then return the structure that the
|
|
|
|
// other thread created.
|
|
|
|
if (!__sync_bool_compare_and_swap(&src->forwarding, src, *dst))
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-10-26 15:44:43 +00:00
|
|
|
if((size_t)src->size >= sizeof(struct block_byref_obj))
|
|
|
|
{
|
|
|
|
src->byref_dispose(*dst);
|
|
|
|
}
|
2011-05-23 16:24:45 +00:00
|
|
|
gc->free(*dst);
|
2010-10-26 15:44:43 +00:00
|
|
|
*dst = src->forwarding;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
2010-10-26 15:44:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*dst = (struct block_byref_obj*)src;
|
2011-02-12 22:39:01 +00:00
|
|
|
increment24(&(*dst)->flags);
|
2010-10-26 15:44:43 +00:00
|
|
|
}
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
2011-06-29 10:41:26 +00:00
|
|
|
else if (IS_SET(flags, BLOCK_FIELD_IS_BLOCK))
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2012-03-11 21:45:08 +00:00
|
|
|
struct Block_layout *src = (struct Block_layout*)object;
|
|
|
|
struct Block_layout **dst = destAddr;
|
|
|
|
|
2010-09-09 10:43:42 +00:00
|
|
|
*dst = Block_copy(src);
|
|
|
|
}
|
2011-06-29 10:41:26 +00:00
|
|
|
else if (IS_SET(flags, BLOCK_FIELD_IS_OBJECT) &&
|
|
|
|
!IS_SET(flags, BLOCK_BYREF_CALLER))
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2010-09-25 10:52:01 +00:00
|
|
|
id src = (id)object;
|
2011-05-29 13:50:46 +00:00
|
|
|
void **dst = destAddr;
|
|
|
|
*dst = src;
|
|
|
|
if (!isGCEnabled)
|
|
|
|
{
|
2011-07-04 20:58:46 +00:00
|
|
|
*dst = objc_retain(src);
|
2011-05-29 13:50:46 +00:00
|
|
|
}
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Similarly a compiler generated dispose helper needs to call back for each
|
|
|
|
* field of the byref data structure. (Currently the implementation only packs
|
|
|
|
* one field into the byref structure but in principle there could be more).
|
|
|
|
* The same flags used in the copy helper should be used for each call
|
|
|
|
* generated to this function:
|
|
|
|
*/
|
2010-09-09 10:43:42 +00:00
|
|
|
void _Block_object_dispose(const void *object, const int flags)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-09-09 10:43:42 +00:00
|
|
|
// FIXME: Needs to be implemented
|
2010-10-26 15:44:43 +00:00
|
|
|
//if(flags & BLOCK_FIELD_IS_WEAK)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
|
|
|
}
|
2010-10-26 15:44:43 +00:00
|
|
|
//else
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-06-29 10:41:26 +00:00
|
|
|
if (IS_SET(flags, BLOCK_FIELD_IS_BYREF))
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2012-03-11 21:45:08 +00:00
|
|
|
struct block_byref_obj *src =
|
2010-10-26 15:44:43 +00:00
|
|
|
(struct block_byref_obj*)object;
|
2012-04-03 09:45:26 +00:00
|
|
|
src = src->forwarding;
|
2010-10-26 15:44:43 +00:00
|
|
|
if (src->isa == _HeapBlockByRef)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-02-12 22:39:01 +00:00
|
|
|
int refcount = (src->flags & BLOCK_REFCOUNT_MASK) == 0 ? 0 : decrement24(&src->flags);
|
2010-10-26 15:44:43 +00:00
|
|
|
if (refcount == 0)
|
|
|
|
{
|
2012-03-01 19:33:26 +00:00
|
|
|
if(IS_SET(src->flags, BLOCK_HAS_COPY_DISPOSE) && (0 != src->byref_dispose))
|
2010-10-26 15:44:43 +00:00
|
|
|
{
|
|
|
|
src->byref_dispose(src);
|
|
|
|
}
|
2011-05-23 16:24:45 +00:00
|
|
|
gc->free(src);
|
2010-10-26 15:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
2011-06-29 10:41:26 +00:00
|
|
|
else if (IS_SET(flags, BLOCK_FIELD_IS_BLOCK))
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2012-03-11 21:45:08 +00:00
|
|
|
struct Block_layout *src = (struct Block_layout*)object;
|
2010-09-09 10:43:42 +00:00
|
|
|
Block_release(src);
|
|
|
|
}
|
2011-06-29 10:41:26 +00:00
|
|
|
else if (IS_SET(flags, BLOCK_FIELD_IS_OBJECT) &&
|
|
|
|
!IS_SET(flags, BLOCK_BYREF_CALLER))
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2010-09-25 10:52:01 +00:00
|
|
|
id src = (id)object;
|
2011-05-29 13:50:46 +00:00
|
|
|
if (!isGCEnabled)
|
|
|
|
{
|
2011-07-04 20:58:46 +00:00
|
|
|
objc_release(src);
|
2011-05-29 13:50:46 +00:00
|
|
|
}
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Copy a block to the heap if it's still on the stack or increments its retain count.
|
2009-09-10 01:24:25 +00:00
|
|
|
void *_Block_copy(void *src)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2011-07-14 21:10:59 +00:00
|
|
|
if (NULL == src) { return NULL; }
|
2012-03-11 21:45:08 +00:00
|
|
|
struct Block_layout *self = src;
|
|
|
|
struct Block_layout *ret = self;
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2010-10-26 15:44:43 +00:00
|
|
|
extern void _NSConcreteStackBlock;
|
2011-08-07 15:41:46 +00:00
|
|
|
extern void _NSConcreteMallocBlock;
|
2012-03-11 21:45:08 +00:00
|
|
|
|
2010-09-09 10:43:42 +00:00
|
|
|
// If the block is Global, there's no need to copy it on the heap.
|
2010-10-10 21:35:37 +00:00
|
|
|
if(self->isa == &_NSConcreteStackBlock)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-08-07 15:41:46 +00:00
|
|
|
ret = gc->malloc(self->descriptor->size);
|
|
|
|
memcpy(ret, self, self->descriptor->size);
|
|
|
|
ret->isa = &_NSConcreteMallocBlock;
|
|
|
|
if(self->flags & BLOCK_HAS_COPY_DISPOSE)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-08-07 15:41:46 +00:00
|
|
|
self->descriptor->copy_helper(ret, self);
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
2011-08-07 15:41:46 +00:00
|
|
|
// We don't need any atomic operations here, because on-stack blocks
|
|
|
|
// can not be aliased across threads (unless you've done something
|
|
|
|
// badly wrong).
|
|
|
|
ret->reserved = 1;
|
|
|
|
}
|
|
|
|
else if (self->isa == &_NSConcreteMallocBlock)
|
|
|
|
{
|
|
|
|
// We need an atomic increment for malloc'd blocks, because they may be
|
|
|
|
// shared.
|
|
|
|
__sync_fetch_and_add(&ret->reserved, 1);
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release a block and frees the memory when the retain count hits zero.
|
2009-09-10 01:24:25 +00:00
|
|
|
void _Block_release(void *src)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2011-07-14 21:10:59 +00:00
|
|
|
if (NULL == src) { return; }
|
2012-03-11 21:45:08 +00:00
|
|
|
struct Block_layout *self = src;
|
|
|
|
|
2010-10-26 15:44:43 +00:00
|
|
|
extern void _NSConcreteStackBlock;
|
2011-08-07 15:41:46 +00:00
|
|
|
extern void _NSConcreteMallocBlock;
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2011-08-07 15:41:46 +00:00
|
|
|
if (&_NSConcreteStackBlock == self->isa)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Block_release called upon a stack Block: %p, ignored\n", self);
|
|
|
|
}
|
|
|
|
else if (&_NSConcreteMallocBlock == self->isa)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
2011-08-07 15:41:46 +00:00
|
|
|
if (__sync_sub_and_fetch(&self->reserved, 1) == 0)
|
2010-09-09 10:43:42 +00:00
|
|
|
{
|
|
|
|
if(self->flags & BLOCK_HAS_COPY_DISPOSE)
|
|
|
|
self->descriptor->dispose_helper(self);
|
2011-07-04 23:27:29 +00:00
|
|
|
objc_delete_weak_refs((id)self);
|
2011-05-23 16:24:45 +00:00
|
|
|
gc->free(self);
|
2010-09-09 10:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
2011-07-04 23:27:29 +00:00
|
|
|
|
|
|
|
PRIVATE void* block_load_weak(void *block)
|
|
|
|
{
|
2012-03-11 21:45:08 +00:00
|
|
|
struct Block_layout *self = block;
|
2011-07-04 23:27:29 +00:00
|
|
|
return (self->reserved) > 0 ? block : 0;
|
|
|
|
}
|