Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 1 | /* |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 2 | * Block.h |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 3 | * |
Blaine Garst | b1c0715 | 2010-04-21 04:34:46 +0000 | [diff] [blame] | 4 | * Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge, |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 22 | * |
| 23 | */ |
| 24 | |
Shantonu Sen | b4c3b6f | 2009-09-22 00:49:12 +0000 | [diff] [blame] | 25 | #ifndef _BLOCK_H_ |
| 26 | #define _BLOCK_H_ |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 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 | |
Shantonu Sen | b4c3b6f | 2009-09-22 00:49:12 +0000 | [diff] [blame] | 36 | #if defined(__cplusplus) |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 37 | extern "C" { |
| 38 | #endif |
| 39 | |
Edward O'Callaghan | 0987064 | 2009-09-12 15:47:39 +0000 | [diff] [blame] | 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 | */ |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 44 | BLOCK_EXPORT void *_Block_copy(const void *aBlock); |
| 45 | |
Edward O'Callaghan | 0987064 | 2009-09-12 15:47:39 +0000 | [diff] [blame] | 46 | /* Lose the reference, and if heap based and last reference, recover the memory. */ |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 47 | BLOCK_EXPORT void _Block_release(const void *aBlock); |
| 48 | |
Shantonu Sen | b4c3b6f | 2009-09-22 00:49:12 +0000 | [diff] [blame] | 49 | #if defined(__cplusplus) |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 50 | } |
| 51 | #endif |
| 52 | |
Edward O'Callaghan | 0987064 | 2009-09-12 15:47:39 +0000 | [diff] [blame] | 53 | /* Type correct macros. */ |
Daniel Dunbar | 8cbe163 | 2009-06-26 16:21:04 +0000 | [diff] [blame] | 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 |