18cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar/*
2b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * Block.h
38cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *
4b1c07156bab839c0502789f09654ec5da8d33c39Blaine Garst * Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge,
5b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * to any person obtaining a copy of this software and associated documentation
6b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * files (the "Software"), to deal in the Software without restriction,
7b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * including without limitation the rights to use, copy, modify, merge, publish,
8b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * distribute, sublicense, and/or sell copies of the Software, and to permit
9b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * persons to whom the Software is furnished to do so, subject to the following
10b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * conditions:
11b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar *
12b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * The above copyright notice and this permission notice shall be included in
13b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * all copies or substantial portions of the Software.
14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar *
15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * SOFTWARE.
228cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *
238cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar */
248cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
25b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen#ifndef _BLOCK_H_
26b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen#define _BLOCK_H_
278cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
288cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#if !defined(BLOCK_EXPORT)
298cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#   if defined(__cplusplus)
308cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#       define BLOCK_EXPORT extern "C"
318cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#   else
328cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#       define BLOCK_EXPORT extern
338cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#   endif
348cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
358cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
36b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen#if defined(__cplusplus)
378cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbarextern "C" {
388cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
398cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
4009870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan/* Create a heap based copy of a Block or simply add a reference to an existing one.
4109870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan * This must be paired with Block_release to recover memory, even when running
4209870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan * under Objective-C Garbage Collection.
4309870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan */
448cbe163cba77c772621f89ddb33793ac170b1faDaniel DunbarBLOCK_EXPORT void *_Block_copy(const void *aBlock);
458cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
4609870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan/* Lose the reference, and if heap based and last reference, recover the memory. */
478cbe163cba77c772621f89ddb33793ac170b1faDaniel DunbarBLOCK_EXPORT void _Block_release(const void *aBlock);
488cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
49b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen#if defined(__cplusplus)
508cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar}
518cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
528cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
5309870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan/* Type correct macros. */
548cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
558cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__)))
568cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#define Block_release(...) _Block_release((const void *)(__VA_ARGS__))
578cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
588cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
598cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
60