├── Block.h ├── Block_private.h ├── ChangeLog ├── Makefile.am ├── config.inc ├── config2.h ├── configure.ac ├── configure.rb ├── data.c ├── ports └── debian │ ├── README.source │ ├── changelog │ ├── compat │ ├── control │ ├── copyright │ ├── libblocksruntime-dev.dirs │ ├── libblocksruntime-dev.install │ ├── libblocksruntime0.dirs │ ├── libblocksruntime0.install │ ├── rules │ └── source │ └── format ├── rpm.spec.in ├── runtime.c └── test └── main.c /Block.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Block.h 3 | * 4 | * Copyright 2008-2010 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 | * 23 | */ 24 | 25 | #ifndef _BLOCK_H_ 26 | #define _BLOCK_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 | 36 | #if defined(__cplusplus) 37 | extern "C" { 38 | #endif 39 | 40 | /* Create a heap based copy of a Block or simply add a reference to an existing one. 41 | * This must be paired with Block_release to recover memory, even when running 42 | * under Objective-C Garbage Collection. 43 | */ 44 | BLOCK_EXPORT void *_Block_copy(const void *aBlock); 45 | 46 | /* Lose the reference, and if heap based and last reference, recover the memory. */ 47 | BLOCK_EXPORT void _Block_release(const void *aBlock); 48 | 49 | #if defined(__cplusplus) 50 | } 51 | #endif 52 | 53 | /* Type correct macros. */ 54 | 55 | #define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__))) 56 | #define Block_release(...) _Block_release((const void *)(__VA_ARGS__)) 57 | 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /Block_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Block_private.h 3 | * 4 | * Copyright 2008-2010 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 | * 23 | */ 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 | 36 | #ifndef _MSC_VER 37 | #include 38 | #else 39 | /* MSVC doesn't have . Compensate. */ 40 | typedef char bool; 41 | #define true (bool)1 42 | #define false (bool)0 43 | #endif 44 | 45 | #if defined(__cplusplus) 46 | extern "C" { 47 | #endif 48 | 49 | 50 | enum { 51 | BLOCK_REFCOUNT_MASK = (0xffff), 52 | BLOCK_NEEDS_FREE = (1 << 24), 53 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), 54 | BLOCK_HAS_CTOR = (1 << 26), /* Helpers have C++ code. */ 55 | BLOCK_IS_GC = (1 << 27), 56 | BLOCK_IS_GLOBAL = (1 << 28), 57 | BLOCK_HAS_DESCRIPTOR = (1 << 29) 58 | }; 59 | 60 | 61 | /* Revised new layout. */ 62 | struct Block_descriptor { 63 | unsigned long int reserved; 64 | unsigned long int size; 65 | void (*copy)(void *dst, void *src); 66 | void (*dispose)(void *); 67 | }; 68 | 69 | 70 | struct Block_layout { 71 | void *isa; 72 | int flags; 73 | int reserved; 74 | void (*invoke)(void *, ...); 75 | struct Block_descriptor *descriptor; 76 | /* Imported variables. */ 77 | }; 78 | 79 | 80 | struct Block_byref { 81 | void *isa; 82 | struct Block_byref *forwarding; 83 | int flags; /* refcount; */ 84 | int size; 85 | void (*byref_keep)(struct Block_byref *dst, struct Block_byref *src); 86 | void (*byref_destroy)(struct Block_byref *); 87 | /* long shared[0]; */ 88 | }; 89 | 90 | 91 | struct Block_byref_header { 92 | void *isa; 93 | struct Block_byref *forwarding; 94 | int flags; 95 | int size; 96 | }; 97 | 98 | 99 | /* Runtime support functions used by compiler when generating copy/dispose helpers. */ 100 | 101 | enum { 102 | /* See function implementation for a more complete description of these fields and combinations */ 103 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), block, ... */ 104 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ 105 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block variable */ 106 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy helpers */ 107 | BLOCK_BYREF_CALLER = 128 /* called from __block (byref) copy/dispose support routines. */ 108 | }; 109 | 110 | /* Runtime entry point called by compiler when assigning objects inside copy helper routines */ 111 | BLOCK_EXPORT void _Block_object_assign(void *destAddr, const void *object, const int flags); 112 | /* BLOCK_FIELD_IS_BYREF is only used from within block copy helpers */ 113 | 114 | 115 | /* runtime entry point called by the compiler when disposing of objects inside dispose helper routine */ 116 | BLOCK_EXPORT void _Block_object_dispose(const void *object, const int flags); 117 | 118 | 119 | 120 | /* Other support functions */ 121 | 122 | /* Runtime entry to get total size of a closure */ 123 | BLOCK_EXPORT unsigned long int Block_size(void *block_basic); 124 | 125 | 126 | 127 | /* the raw data space for runtime classes for blocks */ 128 | /* class+meta used for stack, malloc, and collectable based blocks */ 129 | BLOCK_EXPORT void * _NSConcreteStackBlock[32]; 130 | BLOCK_EXPORT void * _NSConcreteMallocBlock[32]; 131 | BLOCK_EXPORT void * _NSConcreteAutoBlock[32]; 132 | BLOCK_EXPORT void * _NSConcreteFinalizingBlock[32]; 133 | BLOCK_EXPORT void * _NSConcreteGlobalBlock[32]; 134 | BLOCK_EXPORT void * _NSConcreteWeakBlockVariable[32]; 135 | 136 | 137 | /* the intercept routines that must be used under GC */ 138 | BLOCK_EXPORT void _Block_use_GC( 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 | void (*gc_memmove)(void *, void *, unsigned long)); 143 | 144 | /* earlier version, now simply transitional */ 145 | BLOCK_EXPORT void _Block_use_GC5( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject), 146 | void (*setHasRefcount)(const void *, const bool), 147 | void (*gc_assign_strong)(void *, void **), 148 | void (*gc_assign_weak)(const void *, void *)); 149 | 150 | BLOCK_EXPORT void _Block_use_RR( void (*retain)(const void *), 151 | void (*release)(const void *)); 152 | 153 | /* make a collectable GC heap based Block. Not useful under non-GC. */ 154 | BLOCK_EXPORT void *_Block_copy_collectable(const void *aBlock); 155 | 156 | /* thread-unsafe diagnostic */ 157 | BLOCK_EXPORT const char *_Block_dump(const void *block); 158 | 159 | 160 | /* Obsolete */ 161 | 162 | /* first layout */ 163 | struct Block_basic { 164 | void *isa; 165 | int Block_flags; /* int32_t */ 166 | int Block_size; /* XXX should be packed into Block_flags */ 167 | void (*Block_invoke)(void *); 168 | void (*Block_copy)(void *dst, void *src); /* iff BLOCK_HAS_COPY_DISPOSE */ 169 | void (*Block_dispose)(void *); /* iff BLOCK_HAS_COPY_DISPOSE */ 170 | /* long params[0]; // where const imports, __block storage references, etc. get laid down */ 171 | }; 172 | 173 | 174 | #if defined(__cplusplus) 175 | } 176 | #endif 177 | 178 | 179 | #endif /* _BLOCK_PRIVATE_H_ */ 180 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2012-12-18 Mark Heily 2 | 3 | * Merge code from the objc2 branch @r29 back into the trunk. 4 | 5 | 2012-11-04 Mark Heily 6 | 7 | * Migrate SVN repository to sourceforge.net 8 | * Make all exported symbols weak, to coexist with libobjc2 9 | * Release version 0.2 (r27) 10 | 11 | 2010-06-24 Mark Heily 12 | 13 | * Initial release of libBlocksRuntime from complier-rt r104787 14 | 15 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | # FIXME - Only supports Linux for now 3 | 4 | lib_LTLIBRARIES = libBlocksRuntime.la 5 | include_HEADERS = Block.h Block_private.h 6 | 7 | libBlocksRuntime_la_CFLAGS = -std=c99 -Wall -Wextra -W -pedantic 8 | 9 | libBlocksRuntime_la_SOURCES = runtime.c data.c config2.h 10 | 11 | check_PROGRAMS = blockrt-test 12 | 13 | TESTS = blockrt-test 14 | 15 | blockrt_test_SOURCES = test/main.c 16 | 17 | blockrt_test_CFLAGS = -I. -fblocks 18 | 19 | blockrt_test_LDADD = libBlocksRuntime.la 20 | 21 | 22 | -------------------------------------------------------------------------------- /config.inc: -------------------------------------------------------------------------------- 1 | program="libBlocksRuntime" 2 | version="0.1" 3 | abi_major="0" 4 | abi_minor="0" 5 | abi_version="$abi_major.$abi_minor" 6 | cflags="-DBlocksRuntime_EXPORTS -fPIC -std=c99 -Wall -Wextra -W -pedantic -Wno-unused-parameter" 7 | ldflags="" 8 | sources="runtime.c data.c" 9 | libdepends="" 10 | deps="" 11 | mans="" 12 | headers="Block.h Block_private.h" 13 | extra_dist="" 14 | subdirs="" 15 | 16 | # Package metadata 17 | pkg_summary="Blocks Runtime library" 18 | pkg_description="Blocks Runtime library" 19 | license="LLVM" 20 | author="LLVM Development Team" 21 | 22 | pre_configure_hook() { 23 | return 24 | } 25 | 26 | post_configure_hook() { 27 | # FIXME - Need to compile a test program to confirm these are available 28 | echo " 29 | #define HAVE_SYNC_BOOL_COMPARE_AND_SWAP_INT 1 30 | #define HAVE_SYNC_BOOL_COMPARE_AND_SWAP_LONG 1" >> config.h 31 | } 32 | -------------------------------------------------------------------------------- /config2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * config2.h 3 | * 4 | * Copyright 2013 Mark Heily. 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 | * 23 | */ 24 | 25 | /* 26 | 27 | WORKAROUND: 28 | 29 | This makes it easier to include data.c & runtime.c in OpenGCD. 30 | 31 | These constants should be generated by the ./configure script 32 | but are not (yet) 33 | */ 34 | #ifndef _CONFIG2_H_ 35 | #define _CONFIG2_H_ 36 | 37 | #define BlocksRuntime_EXPORTS 1 38 | #define HAVE_SYNC_BOOL_COMPARE_AND_SWAP_INT 1 39 | #define HAVE_SYNC_BOOL_COMPARE_AND_SWAP_LONG 1 40 | 41 | #pragma GCC diagnostic ignored "-Wunused-parameter" 42 | 43 | #endif /* _CONFIG2_H_ */ 44 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_INIT([libBlocksRuntime], [0.4.1]) 5 | AC_PROG_CC([clang]) 6 | LT_INIT 7 | AM_INIT_AUTOMAKE([foreign subdir-objects]) 8 | AC_CONFIG_SRCDIR([configure.ac]) 9 | AC_CONFIG_HEADERS([config.h]) 10 | AC_CONFIG_FILES([Makefile]) 11 | AC_OUTPUT 12 | -------------------------------------------------------------------------------- /configure.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # Copyright (c) 2011 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | begin 19 | require 'makeconf' 20 | rescue LoadError 21 | $LOAD_PATH << "makeconf" 22 | require 'makeconf' 23 | end 24 | 25 | mc = Makeconf.new 26 | 27 | cc = CCompiler.new( 28 | :search => %w{clang} 29 | ) 30 | 31 | project = Project.new( 32 | :id => 'libBlocksRuntime', 33 | :version => '0.3', 34 | :cc => cc 35 | ) 36 | 37 | project.add( 38 | Library.new( 39 | :id => 'libBlocksRuntime', 40 | :cflags => '-std=c99 -Wall -Wextra -W -pedantic', 41 | :sources => %w{runtime.c data.c } 42 | ), 43 | Header.new( :id => 'headers', :sources => %w{ Block.h Block_private.h } ), 44 | Test.new( 45 | :id => 'blktest', 46 | :cflags => '-I. -fblocks', 47 | :sources => %w{test/main.c}, 48 | :ldadd => 'libBlocksRuntime.a' 49 | ) 50 | ) 51 | 52 | # Require the use of an unofficial Clang toolchain for Android. 53 | if SystemType.host =~ /-androideabi$/ 54 | project.ndk_toolchain_version = 'clang3.2' 55 | end 56 | 57 | mc.configure project 58 | 59 | #package: 60 | # summary: "Blocks Runtime library" 61 | # description: "Blocks Runtime library" 62 | # license: LLVM 63 | # author: "LLVM Development Team" 64 | -------------------------------------------------------------------------------- /data.c: -------------------------------------------------------------------------------- 1 | /* 2 | * data.c 3 | * 4 | * Copyright 2008-2010 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 | * 23 | */ 24 | 25 | /******************** 26 | NSBlock support 27 | 28 | We allocate space and export a symbol to be used as the Class for the on-stack and malloc'ed copies until ObjC arrives on the scene. These data areas are set up by Foundation to link in as real classes post facto. 29 | 30 | We keep these in a separate file so that we can include the runtime code in test subprojects but not include the data so that compiled code that sees the data in libSystem doesn't get confused by a second copy. Somehow these don't get unified in a common block. 31 | **********************/ 32 | 33 | void * __attribute__((weak)) _NSConcreteStackBlock[32] = { 0 }; 34 | void * __attribute__((weak)) _NSConcreteMallocBlock[32] = { 0 }; 35 | void * __attribute__((weak)) _NSConcreteAutoBlock[32] = { 0 }; 36 | void * __attribute__((weak)) _NSConcreteFinalizingBlock[32] = { 0 }; 37 | void * __attribute__((weak)) _NSConcreteGlobalBlock[32] = { 0 }; 38 | void * __attribute__((weak)) _NSConcreteWeakBlockVariable[32] = { 0 }; 39 | 40 | #if 0 41 | void _Block_copy_error(void) { 42 | } 43 | #endif 44 | -------------------------------------------------------------------------------- /ports/debian/README.source: -------------------------------------------------------------------------------- 1 | This source tree is based on the BlocksRuntime directory of the compiler-rt 2 | sources. There is no interdependency between libBlocksRuntime and the rest 3 | of compiler-rt, so it is somewhat odd that they are distributed together. 4 | 5 | compiler-rt uses CMake, which is overkill for a tiny library like libBlocksRuntime. I copied the values from config.h and the CFLAGS from the CMake build system into Makefile and config.inc. 6 | 7 | - Mark 8 | -------------------------------------------------------------------------------- /ports/debian/changelog: -------------------------------------------------------------------------------- 1 | libblocksruntime (0.1-1) unstable; urgency=low 2 | 3 | * Initial release from compiler-rt r104787 sources (Closes: #584190) 4 | 5 | -- Mark Heily Wed, 02 Jun 2010 22:17:11 -0400 6 | -------------------------------------------------------------------------------- /ports/debian/compat: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /ports/debian/control: -------------------------------------------------------------------------------- 1 | Source: libblocksruntime 2 | Priority: extra 3 | Maintainer: Mark Heily 4 | Build-Depends: debhelper (>= 7), clang 5 | Standards-Version: 3.8.4 6 | Section: libs 7 | Homepage: http://compiler-rt.llvm.org/ 8 | 9 | Package: libblocksruntime-dev 10 | Section: libdevel 11 | Architecture: any 12 | Depends: libblocksruntime0 (= ${binary:Version}), ${misc:Depends} 13 | Description: Blocks Runtime (development files) 14 | This package contains development headers for building software that 15 | uses blocks. 16 | . 17 | Blocks are a proposed extension to the C, Objective C, and C++ languages 18 | developed by Apple to support the Grand Central Dispatch concurrency engine. 19 | 20 | Package: libblocksruntime0 21 | Section: libs 22 | Architecture: any 23 | Depends: ${shlibs:Depends}, ${misc:Depends} 24 | Description: Blocks Runtime library 25 | Blocks are a proposed extension to the C, Objective C, and C++ languages 26 | developed by Apple to support the Grand Central Dispatch concurrency engine. 27 | Blocks are anonymous inline functions that automatically capture a read-only 28 | copy of local variables, and have read-write access to local variables that 29 | are declared with the "__block" storage class. 30 | . 31 | This package contains a library that is needed by programs that use Blocks. 32 | -------------------------------------------------------------------------------- /ports/debian/copyright: -------------------------------------------------------------------------------- 1 | This work was packaged for Debian by: 2 | 3 | Mark Heily on Wed, 02 Jun 2010 22:17:11 -0400 4 | 5 | It was downloaded from 6 | 7 | Upstream Author(s): 8 | 9 | LLVM Development Team 10 | Apple, Inc. 11 | 12 | Copyright: 13 | 14 | Copyright (c) 2003-2009 University of Illinois at Urbana-Champaign. 15 | Copyright 2008-2010 Apple, Inc. 16 | 17 | License: 18 | 19 | ============================================================================== 20 | LLVM Release License 21 | ============================================================================== 22 | University of Illinois/NCSA 23 | Open Source License 24 | 25 | Copyright (c) 2003-2009 University of Illinois at Urbana-Champaign. 26 | All rights reserved. 27 | 28 | Developed by: 29 | 30 | LLVM Team 31 | 32 | University of Illinois at Urbana-Champaign 33 | 34 | http://llvm.org 35 | 36 | Permission is hereby granted, free of charge, to any person obtaining a copy of 37 | this software and associated documentation files (the "Software"), to deal with 38 | the Software without restriction, including without limitation the rights to 39 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 40 | of the Software, and to permit persons to whom the Software is furnished to do 41 | so, subject to the following conditions: 42 | 43 | * Redistributions of source code must retain the above copyright notice, 44 | this list of conditions and the following disclaimers. 45 | 46 | * Redistributions in binary form must reproduce the above copyright notice, 47 | this list of conditions and the following disclaimers in the 48 | documentation and/or other materials provided with the distribution. 49 | 50 | * Neither the names of the LLVM Team, University of Illinois at 51 | Urbana-Champaign, nor the names of its contributors may be used to 52 | endorse or promote products derived from this Software without specific 53 | prior written permission. 54 | 55 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 56 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 57 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 58 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 59 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 60 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE 61 | SOFTWARE. 62 | 63 | ============================================================================== 64 | Copyrights and Licenses for Third Party Software Distributed with LLVM: 65 | ============================================================================== 66 | The LLVM software contains code written by third parties. Such software will 67 | have its own individual LICENSE.TXT file in the directory in which it appears. 68 | This file will describe the copyrights, license, and restrictions which apply 69 | to that code. 70 | 71 | The disclaimer of warranty in the University of Illinois Open Source License 72 | applies to all code in the LLVM Distribution, and nothing in any of the 73 | other licenses gives permission to use the names of the LLVM Team or the 74 | University of Illinois to endorse or promote products derived from this 75 | Software. 76 | 77 | --------------------------------------------------------------------------- 78 | Apple "Blocks" license 79 | --------------------------------------------------------------------------- 80 | 81 | Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge, 82 | to any person obtaining a copy of this software and associated documentation 83 | files (the "Software"), to deal in the Software without restriction, 84 | including without limitation the rights to use, copy, modify, merge, publish, 85 | distribute, sublicense, and/or sell copies of the Software, and to permit 86 | persons to whom the Software is furnished to do so, subject to the following 87 | conditions: 88 | 89 | The above copyright notice and this permission notice shall be included in 90 | all copies or substantial portions of the Software. 91 | 92 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 93 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 94 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 95 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 96 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 97 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 98 | SOFTWARE. 99 | 100 | The Debian packaging is: 101 | 102 | Copyright (C) 2010 Mark Heily 103 | 104 | and is licensed under the BSD license, 105 | see `/usr/share/common-licenses/BSD'. 106 | 107 | -------------------------------------------------------------------------------- /ports/debian/libblocksruntime-dev.dirs: -------------------------------------------------------------------------------- 1 | usr/lib 2 | usr/include 3 | -------------------------------------------------------------------------------- /ports/debian/libblocksruntime-dev.install: -------------------------------------------------------------------------------- 1 | usr/include/* 2 | usr/lib/lib*.so 3 | -------------------------------------------------------------------------------- /ports/debian/libblocksruntime0.dirs: -------------------------------------------------------------------------------- 1 | usr/lib 2 | -------------------------------------------------------------------------------- /ports/debian/libblocksruntime0.install: -------------------------------------------------------------------------------- 1 | usr/lib/lib*.so.* 2 | -------------------------------------------------------------------------------- /ports/debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | # Uncomment this to turn on verbose mode. 4 | #export DH_VERBOSE=1 5 | 6 | build: build-stamp 7 | build-stamp: 8 | dh_testdir 9 | CC=clang CFLAGS="" LDFLAGS="" \ 10 | ./configure --prefix=/usr --mandir=\$${prefix}/share/man 11 | $(MAKE) 12 | touch $@ 13 | 14 | clean: 15 | dh_testdir 16 | dh_testroot 17 | rm -f build-stamp 18 | 19 | # Add here commands to clean up after the build process. 20 | [ ! -f config.mk ] || $(MAKE) distclean 21 | 22 | dh_clean 23 | 24 | install: build 25 | dh_testdir 26 | dh_testroot 27 | dh_prep 28 | dh_installdirs 29 | 30 | # Add here commands to install the package into debian/tmp 31 | $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install 32 | 33 | # Build architecture-independent files here. 34 | binary-indep: install 35 | # We have nothing to do by default. 36 | 37 | # Build architecture-dependent files here. 38 | binary-arch: install 39 | dh_testdir 40 | dh_testroot 41 | dh_movefiles 42 | dh_installchangelogs ChangeLog 43 | dh_installdocs 44 | dh_installexamples 45 | dh_install 46 | dh_installman 47 | dh_link 48 | dh_strip 49 | dh_compress 50 | dh_fixperms 51 | dh_makeshlibs -V 52 | dh_installdeb 53 | dh_shlibdeps 54 | dh_gencontrol 55 | dh_md5sums 56 | dh_builddeb 57 | 58 | binary: binary-indep binary-arch 59 | .PHONY: build clean binary-indep binary-arch binary install 60 | -------------------------------------------------------------------------------- /ports/debian/source/format: -------------------------------------------------------------------------------- 1 | 1.0 2 | -------------------------------------------------------------------------------- /rpm.spec.in: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2009-2010 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | Name: @@PROGRAM@@ 18 | Summary: @@PKG_SUMMARY@@ 19 | Version: @@VERSION@@ 20 | Release: 1 21 | License: @@LICENSE@@ 22 | Vendor: @@AUTHOR@@ 23 | Group: System Environment/Libraries 24 | Source0: %{name}-%version.tar.gz 25 | 26 | %description 27 | @@PKG_DESCRIPTION@@ 28 | 29 | %package devel 30 | Summary: Header files, libraries and development documentation for %{name} 31 | Group: Development/Libraries 32 | Requires: %{name} = %{version}-%{release} 33 | 34 | %description devel 35 | This package contains the header files, static libraries and development 36 | documentation for %{name}. If you like to develop programs using %{name}, 37 | you will need to install %{name}-devel. 38 | 39 | %prep 40 | %setup -q -n @@PROGRAM@@-@@VERSION@@ 41 | 42 | %build 43 | ./configure --prefix=/usr 44 | make 45 | 46 | %install 47 | make DESTDIR=%{buildroot} install 48 | 49 | %clean 50 | [ %{buildroot} != "/" ] && rm -rf %{buildroot} 51 | 52 | %post -p /sbin/ldconfig 53 | %postun -p /sbin/ldconfig 54 | 55 | %files 56 | %defattr(-,root,root) 57 | 58 | /usr/lib/*.so.* 59 | 60 | %files devel 61 | %defattr(-,root,root) 62 | 63 | /usr/lib/*.so 64 | /usr/include/* 65 | 66 | %changelog 67 | * @@RPM_DATE@@ Mark Heily - @@VERSION@@-1 68 | - automatically generated spec file 69 | -------------------------------------------------------------------------------- /runtime.c: -------------------------------------------------------------------------------- 1 | /* 2 | * runtime.c 3 | * 4 | * Copyright 2008-2010 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 | * 23 | */ 24 | 25 | #include "config2.h" 26 | #include "Block_private.h" 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "config.h" 33 | 34 | #ifdef HAVE_AVAILABILITY_MACROS_H 35 | #include 36 | #endif /* HAVE_AVAILABILITY_MACROS_H */ 37 | 38 | #ifdef HAVE_TARGET_CONDITIONALS_H 39 | #include 40 | #endif /* HAVE_TARGET_CONDITIONALS_H */ 41 | 42 | #if defined(HAVE_OSATOMIC_COMPARE_AND_SWAP_INT) && defined(HAVE_OSATOMIC_COMPARE_AND_SWAP_LONG) 43 | 44 | #ifdef HAVE_LIBKERN_OSATOMIC_H 45 | #include 46 | #endif /* HAVE_LIBKERN_OSATOMIC_H */ 47 | 48 | #elif defined(__WIN32__) || defined(_WIN32) 49 | #define _CRT_SECURE_NO_WARNINGS 1 50 | #include 51 | 52 | static __inline bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst) { 53 | /* fixme barrier is overkill -- see objc-os.h */ 54 | long original = InterlockedCompareExchange(dst, newl, oldl); 55 | return (original == oldl); 56 | } 57 | 58 | static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst) { 59 | /* fixme barrier is overkill -- see objc-os.h */ 60 | int original = InterlockedCompareExchange(dst, newi, oldi); 61 | return (original == oldi); 62 | } 63 | 64 | /* 65 | * Check to see if the GCC atomic built-ins are available. If we're on 66 | * a 64-bit system, make sure we have an 8-byte atomic function 67 | * available. 68 | * 69 | */ 70 | 71 | #elif defined(HAVE_SYNC_BOOL_COMPARE_AND_SWAP_INT) && defined(HAVE_SYNC_BOOL_COMPARE_AND_SWAP_LONG) 72 | 73 | static __inline bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst) { 74 | return __sync_bool_compare_and_swap(dst, oldl, newl); 75 | } 76 | 77 | static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst) { 78 | return __sync_bool_compare_and_swap(dst, oldi, newi); 79 | } 80 | 81 | #else 82 | #error unknown atomic compare-and-swap primitive 83 | #endif /* HAVE_OSATOMIC_COMPARE_AND_SWAP_INT && HAVE_OSATOMIC_COMPARE_AND_SWAP_LONG */ 84 | 85 | 86 | /* 87 | * Globals: 88 | */ 89 | 90 | static const int _Block_copy_flag = BLOCK_NEEDS_FREE; 91 | static const int _Byref_flag_initial_value = BLOCK_NEEDS_FREE | 2; 92 | 93 | static const int WANTS_ONE = (1 << 16); 94 | 95 | /* 96 | * Internal Utilities: 97 | */ 98 | 99 | static int latching_incr_int(int *where) { 100 | while (1) { 101 | int old_value = *(volatile int *)where; 102 | if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) { 103 | return BLOCK_REFCOUNT_MASK; 104 | } 105 | if (OSAtomicCompareAndSwapInt(old_value, old_value+1, (volatile int *)where)) { 106 | return old_value+1; 107 | } 108 | } 109 | } 110 | 111 | static int latching_decr_int(int *where) { 112 | while (1) { 113 | int old_value = *(volatile int *)where; 114 | if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) { 115 | return BLOCK_REFCOUNT_MASK; 116 | } 117 | if ((old_value & BLOCK_REFCOUNT_MASK) == 0) { 118 | return 0; 119 | } 120 | if (OSAtomicCompareAndSwapInt(old_value, old_value-1, (volatile int *)where)) { 121 | return old_value-1; 122 | } 123 | } 124 | } 125 | 126 | 127 | /* 128 | * GC support stub routines: 129 | */ 130 | #if 0 131 | #pragma mark GC Support Routines 132 | #endif /* if 0 */ 133 | 134 | 135 | static void *_Block_alloc_default(const unsigned long size, const bool initialCountIsOne, const bool isObject) { 136 | return malloc(size); 137 | } 138 | 139 | static void _Block_assign_default(void *value, void **destptr) { 140 | *destptr = value; 141 | } 142 | 143 | static void _Block_retain_object_default(const void *ptr) { 144 | if (!ptr) return; 145 | } 146 | 147 | static void _Block_release_object_default(const void *ptr) { 148 | if (!ptr) return; 149 | } 150 | 151 | static void _Block_assign_weak_default(const void *ptr, void *dest) { 152 | *(void **)dest = (void *)ptr; 153 | } 154 | 155 | static void _Block_memmove_default(void *dst, void *src, unsigned long size) { 156 | memmove(dst, src, (size_t)size); 157 | } 158 | 159 | 160 | /* 161 | * GC support callout functions - initially set to stub routines: 162 | */ 163 | 164 | static void *(*_Block_allocator)(const unsigned long, const bool isOne, const bool isObject) = _Block_alloc_default; 165 | static void (*_Block_deallocator)(const void *) = (void (*)(const void *))free; 166 | static void (*_Block_assign)(void *value, void **destptr) = _Block_assign_default; 167 | static void (*_Block_retain_object)(const void *ptr) = _Block_retain_object_default; 168 | static void (*_Block_release_object)(const void *ptr) = _Block_release_object_default; 169 | static void (*_Block_assign_weak)(const void *dest, void *ptr) = _Block_assign_weak_default; 170 | static void (*_Block_memmove)(void *dest, void *src, unsigned long size) = _Block_memmove_default; 171 | 172 | 173 | 174 | /* 175 | * Internal Support routines for copying: 176 | */ 177 | 178 | #if 0 179 | #pragma mark Copy/Release support 180 | #endif /* if 0 */ 181 | 182 | /* Copy, or bump refcount, of a block. If really copying, call the copy helper if present. */ 183 | static void *_Block_copy_internal(const void *arg, const int flags) { 184 | struct Block_layout *aBlock; 185 | 186 | //printf("_Block_copy_internal(%p, %x)\n", arg, flags); 187 | if (!arg) return NULL; 188 | 189 | 190 | // The following would be better done as a switch statement 191 | aBlock = (struct Block_layout *)arg; 192 | if (aBlock->flags & BLOCK_NEEDS_FREE) { 193 | // latches on high 194 | latching_incr_int(&aBlock->flags); 195 | return aBlock; 196 | } 197 | else if (aBlock->flags & BLOCK_IS_GLOBAL) { 198 | return aBlock; 199 | } 200 | 201 | // Its a stack block. Make a copy. 202 | struct Block_layout *result = malloc(aBlock->descriptor->size); 203 | if (!result) return (void *)0; 204 | memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first 205 | // reset refcount 206 | result->flags &= ~(BLOCK_REFCOUNT_MASK); // XXX not needed 207 | result->flags |= BLOCK_NEEDS_FREE | 1; 208 | if (result->flags & BLOCK_HAS_COPY_DISPOSE) { 209 | //printf("calling block copy helper %p(%p, %p)...\n", aBlock->descriptor->copy, result, aBlock); 210 | (*aBlock->descriptor->copy)(result, aBlock); // do fixup 211 | } 212 | return result; 213 | } 214 | 215 | 216 | /* 217 | * Runtime entry points for maintaining the sharing knowledge of byref data blocks. 218 | * 219 | * A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data 220 | * Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr. 221 | * We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment it. 222 | * Otherwise we need to copy it and update the stack forwarding pointer 223 | * XXX We need to account for weak/nonretained read-write barriers. 224 | */ 225 | 226 | static void _Block_byref_assign_copy(void *dest, const void *arg, const int flags) { 227 | struct Block_byref **destp = (struct Block_byref **)dest; 228 | struct Block_byref *src = (struct Block_byref *)arg; 229 | 230 | //printf("_Block_byref_assign_copy called, byref destp %p, src %p, flags %x\n", destp, src, flags); 231 | //printf("src dump: %s\n", _Block_byref_dump(src)); 232 | if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) { 233 | //printf("making copy\n"); 234 | // src points to stack 235 | struct Block_byref *copy = (struct Block_byref *)_Block_allocator(src->size, false, false); 236 | copy->flags = src->flags | _Byref_flag_initial_value; // non-GC one for caller, one for stack 237 | copy->forwarding = copy; // patch heap copy to point to itself (skip write-barrier) 238 | src->forwarding = copy; // patch stack to point to heap copy 239 | copy->size = src->size; 240 | if (src->flags & BLOCK_HAS_COPY_DISPOSE) { 241 | // Trust copy helper to copy everything of interest 242 | // If more than one field shows up in a byref block this is wrong XXX 243 | copy->byref_keep = src->byref_keep; 244 | copy->byref_destroy = src->byref_destroy; 245 | (*src->byref_keep)(copy, src); 246 | } 247 | else { 248 | // just bits. Blast 'em using _Block_memmove in case they're __strong 249 | _Block_memmove( 250 | (void *)©->byref_keep, 251 | (void *)&src->byref_keep, 252 | src->size - sizeof(struct Block_byref_header)); 253 | } 254 | } 255 | // already copied to heap 256 | else if ((src->forwarding->flags & BLOCK_NEEDS_FREE) == BLOCK_NEEDS_FREE) { 257 | latching_incr_int(&src->forwarding->flags); 258 | } 259 | // assign byref data block pointer into new Block 260 | _Block_assign(src->forwarding, (void **)destp); 261 | } 262 | 263 | // Old compiler SPI 264 | static void _Block_byref_release(const void *arg) { 265 | struct Block_byref *shared_struct = (struct Block_byref *)arg; 266 | int refcount; 267 | 268 | // dereference the forwarding pointer since the compiler isn't doing this anymore (ever?) 269 | shared_struct = shared_struct->forwarding; 270 | 271 | //printf("_Block_byref_release %p called, flags are %x\n", shared_struct, shared_struct->flags); 272 | // To support C++ destructors under GC we arrange for there to be a finalizer for this 273 | // by using an isa that directs the code to a finalizer that calls the byref_destroy method. 274 | if ((shared_struct->flags & BLOCK_NEEDS_FREE) == 0) { 275 | return; // stack or GC or global 276 | } 277 | refcount = shared_struct->flags & BLOCK_REFCOUNT_MASK; 278 | if (refcount <= 0) { 279 | printf("_Block_byref_release: Block byref data structure at %p underflowed\n", arg); 280 | } 281 | else if ((latching_decr_int(&shared_struct->flags) & BLOCK_REFCOUNT_MASK) == 0) { 282 | //printf("disposing of heap based byref block\n"); 283 | if (shared_struct->flags & BLOCK_HAS_COPY_DISPOSE) { 284 | //printf("calling out to helper\n"); 285 | (*shared_struct->byref_destroy)(shared_struct); 286 | } 287 | _Block_deallocator((struct Block_layout *)shared_struct); 288 | } 289 | } 290 | 291 | 292 | /* 293 | * 294 | * API supporting SPI 295 | * _Block_copy, _Block_release, and (old) _Block_destroy 296 | * 297 | */ 298 | 299 | #if 0 300 | #pragma mark SPI/API 301 | #endif /* if 0 */ 302 | 303 | void * __attribute__((weak)) _Block_copy(const void *arg) { 304 | return _Block_copy_internal(arg, WANTS_ONE); 305 | } 306 | 307 | 308 | // API entry point to release a copied Block 309 | void __attribute__((weak)) _Block_release(void *arg) { 310 | struct Block_layout *aBlock = (struct Block_layout *)arg; 311 | int32_t newCount; 312 | if (!aBlock) return; 313 | newCount = latching_decr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK; 314 | if (newCount > 0) return; 315 | // Hit zero 316 | if (aBlock->flags & BLOCK_NEEDS_FREE) { 317 | if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE)(*aBlock->descriptor->dispose)(aBlock); 318 | _Block_deallocator(aBlock); 319 | } 320 | else if (aBlock->flags & BLOCK_IS_GLOBAL) { 321 | ; 322 | } 323 | else { 324 | printf("Block_release called upon a stack Block: %p, ignored\n", (void *)aBlock); 325 | } 326 | } 327 | 328 | 329 | 330 | // Old Compiler SPI point to release a copied Block used by the compiler in dispose helpers 331 | static void _Block_destroy(const void *arg) { 332 | struct Block_layout *aBlock; 333 | if (!arg) return; 334 | aBlock = (struct Block_layout *)arg; 335 | _Block_release(aBlock); 336 | } 337 | 338 | 339 | 340 | /* 341 | * 342 | * SPI used by other layers 343 | * 344 | */ 345 | 346 | // SPI 347 | unsigned long int __attribute__((weak)) Block_size(void *arg) { 348 | return ((struct Block_layout *)arg)->descriptor->size; 349 | } 350 | 351 | 352 | #if 0 353 | #pragma mark Compiler SPI entry points 354 | #endif /* if 0 */ 355 | 356 | 357 | /******************************************************* 358 | 359 | Entry points used by the compiler - the real API! 360 | 361 | 362 | A Block can reference four different kinds of things that require help when the Block is copied to the heap. 363 | 1) C++ stack based objects 364 | 2) References to Objective-C objects 365 | 3) Other Blocks 366 | 4) __block variables 367 | 368 | In these cases helper functions are synthesized by the compiler for use in Block_copy and Block_release, called the copy and dispose helpers. The copy helper emits a call to the C++ const copy constructor for C++ stack based objects and for the rest calls into the runtime support function _Block_object_assign. The dispose helper has a call to the C++ destructor for case 1 and a call into _Block_object_dispose for the rest. 369 | 370 | The flags parameter of _Block_object_assign and _Block_object_dispose is set to 371 | * BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object, 372 | * BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and 373 | * BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable. 374 | If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16). 375 | 376 | So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24. 377 | 378 | When a __block variable is either a C++ object, an Objective-C object, or another Block then the compiler also generates copy/dispose helper functions. Similarly to the Block copy helper, the "__block" copy helper (formerly and still a.k.a. "byref" copy helper) will do a C++ copy constructor (not a const one though!) and the dispose helper will do the destructor. And similarly the helpers will call into the same two support functions with the same values for objects and Blocks with the additional BLOCK_BYREF_CALLER (128) bit of information supplied. 379 | 380 | So the __block copy/dispose helpers will generate flag values of 3 or 7 for objects and Blocks respectively, with BLOCK_FIELD_IS_WEAK (16) or'ed as appropriate and always 128 or'd in, for the following set of possibilities: 381 | __block id 128+3 382 | __weak block id 128+3+16 383 | __block (^Block) 128+7 384 | __weak __block (^Block) 128+7+16 385 | 386 | The implementation of the two routines would be improved by switch statements enumerating the eight cases. 387 | 388 | ********************************************************/ 389 | 390 | /* 391 | * When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point 392 | * to do the assignment. 393 | */ 394 | void __attribute__((weak)) _Block_object_assign(void *destAddr, const void *object, const int flags) { 395 | //printf("_Block_object_assign(*%p, %p, %x)\n", destAddr, object, flags); 396 | if ((flags & BLOCK_BYREF_CALLER) == BLOCK_BYREF_CALLER) { 397 | if ((flags & BLOCK_FIELD_IS_WEAK) == BLOCK_FIELD_IS_WEAK) { 398 | _Block_assign_weak(object, destAddr); 399 | } 400 | else { 401 | // do *not* retain or *copy* __block variables whatever they are 402 | _Block_assign((void *)object, destAddr); 403 | } 404 | } 405 | else if ((flags & BLOCK_FIELD_IS_BYREF) == BLOCK_FIELD_IS_BYREF) { 406 | // copying a __block reference from the stack Block to the heap 407 | // flags will indicate if it holds a __weak reference and needs a special isa 408 | _Block_byref_assign_copy(destAddr, object, flags); 409 | } 410 | // (this test must be before next one) 411 | else if ((flags & BLOCK_FIELD_IS_BLOCK) == BLOCK_FIELD_IS_BLOCK) { 412 | // copying a Block declared variable from the stack Block to the heap 413 | _Block_assign(_Block_copy_internal(object, flags), destAddr); 414 | } 415 | // (this test must be after previous one) 416 | else if ((flags & BLOCK_FIELD_IS_OBJECT) == BLOCK_FIELD_IS_OBJECT) { 417 | //printf("retaining object at %p\n", object); 418 | _Block_retain_object(object); 419 | //printf("done retaining object at %p\n", object); 420 | _Block_assign((void *)object, destAddr); 421 | } 422 | } 423 | 424 | // When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point 425 | // to help dispose of the contents 426 | // Used initially only for __attribute__((NSObject)) marked pointers. 427 | void __attribute__((weak)) _Block_object_dispose(const void *object, const int flags) { 428 | //printf("_Block_object_dispose(%p, %x)\n", object, flags); 429 | if (flags & BLOCK_FIELD_IS_BYREF) { 430 | // get rid of the __block data structure held in a Block 431 | _Block_byref_release(object); 432 | } 433 | else if ((flags & (BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_BLOCK) { 434 | // get rid of a referenced Block held by this Block 435 | // (ignore __block Block variables, compiler doesn't need to call us) 436 | _Block_destroy(object); 437 | } 438 | else if ((flags & (BLOCK_FIELD_IS_WEAK|BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_OBJECT) { 439 | // get rid of a referenced object held by this Block 440 | // (ignore __block object variables, compiler doesn't need to call us) 441 | _Block_release_object(object); 442 | } 443 | } 444 | 445 | 446 | /* 447 | * Debugging support: 448 | */ 449 | #if 0 450 | #pragma mark Debugging 451 | #endif /* if 0 */ 452 | 453 | 454 | const char * __attribute__((weak)) _Block_dump(const void *block) { 455 | struct Block_layout *closure = (struct Block_layout *)block; 456 | static char buffer[512]; 457 | char *cp = buffer; 458 | if (closure == NULL) { 459 | sprintf(cp, "NULL passed to _Block_dump\n"); 460 | return buffer; 461 | } 462 | if (! (closure->flags & BLOCK_HAS_DESCRIPTOR)) { 463 | printf("Block compiled by obsolete compiler, please recompile source for this Block\n"); 464 | exit(1); 465 | } 466 | cp += sprintf(cp, "^%p (new layout) =\n", (void *)closure); 467 | cp += sprintf(cp, "flags:"); 468 | if (closure->flags & BLOCK_HAS_DESCRIPTOR) { 469 | cp += sprintf(cp, " HASDESCRIPTOR"); 470 | } 471 | if (closure->flags & BLOCK_NEEDS_FREE) { 472 | cp += sprintf(cp, " FREEME"); 473 | } 474 | if (closure->flags & BLOCK_IS_GC) { 475 | cp += sprintf(cp, " ISGC"); 476 | } 477 | if (closure->flags & BLOCK_HAS_COPY_DISPOSE) { 478 | cp += sprintf(cp, " HASHELP"); 479 | } 480 | if (closure->flags & BLOCK_HAS_CTOR) { 481 | cp += sprintf(cp, " HASCTOR"); 482 | } 483 | cp += sprintf(cp, "\nrefcount: %u\n", closure->flags & BLOCK_REFCOUNT_MASK); 484 | cp += sprintf(cp, "invoke: %p\n", (void *)(uintptr_t)closure->invoke); 485 | { 486 | struct Block_descriptor *dp = closure->descriptor; 487 | cp += sprintf(cp, "descriptor: %p\n", (void *)dp); 488 | cp += sprintf(cp, "descriptor->reserved: %lu\n", dp->reserved); 489 | cp += sprintf(cp, "descriptor->size: %lu\n", dp->size); 490 | 491 | if (closure->flags & BLOCK_HAS_COPY_DISPOSE) { 492 | cp += sprintf(cp, "descriptor->copy helper: %p\n", (void *)(uintptr_t)dp->copy); 493 | cp += sprintf(cp, "descriptor->dispose helper: %p\n", (void *)(uintptr_t)dp->dispose); 494 | } 495 | } 496 | return buffer; 497 | } 498 | 499 | 500 | const char * __attribute__((weak)) _Block_byref_dump(struct Block_byref *src) { 501 | static char buffer[256]; 502 | char *cp = buffer; 503 | cp += sprintf(cp, "byref data block %p contents:\n", (void *)src); 504 | cp += sprintf(cp, " forwarding: %p\n", (void *)src->forwarding); 505 | cp += sprintf(cp, " flags: 0x%x\n", src->flags); 506 | cp += sprintf(cp, " size: %d\n", src->size); 507 | if (src->flags & BLOCK_HAS_COPY_DISPOSE) { 508 | cp += sprintf(cp, " copy helper: %p\n", (void *)(uintptr_t)src->byref_keep); 509 | cp += sprintf(cp, " dispose helper: %p\n", (void *)(uintptr_t)src->byref_destroy); 510 | } 511 | return buffer; 512 | } 513 | 514 | -------------------------------------------------------------------------------- /test/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | typedef int (^IntBlock)(); 22 | 23 | IntBlock incrementor(int start) 24 | { 25 | __block int i = start; 26 | 27 | return Block_copy( ^{ 28 | return ++i; 29 | } 30 | ); 31 | } 32 | 33 | int 34 | main(int argc, char **argv) 35 | { 36 | IntBlock counter = incrementor(0); 37 | int rv; 38 | 39 | printf("increment test... "); 40 | rv = counter(); 41 | rv = counter(); 42 | rv = counter(); 43 | Block_release(counter); 44 | if (rv == 3) { 45 | puts("ok"); 46 | exit(0); 47 | } else { 48 | puts("ERROR"); 49 | exit(1); 50 | } 51 | } 52 | --------------------------------------------------------------------------------