Block.h revision 09870645031d5a05c7c3b9a66a53348a0d82ae52
18cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar/*
2b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * Block.h
38cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *
4b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar * Copyright 2008-2009 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
258cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#ifndef _Block_H_
268cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#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
3609870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan#include <config.h>
3709870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan
3809870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan#if defined( HAVE_AVAILABILITY_MACROS_H ) && defined( HAVE_TARGET_CONDITIONALS_H )
398cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#include <AvailabilityMacros.h>
408cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#include <TargetConditionals.h>
4109870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan#endif /* HAVE_AVAILABILITY_MACROS_H and HAVE_TARGET_CONDITIONALS_H. */
428cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
438cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#if __cplusplus
448cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbarextern "C" {
458cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
468cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
4709870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan/* Create a heap based copy of a Block or simply add a reference to an existing one.
4809870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan * This must be paired with Block_release to recover memory, even when running
4909870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan * under Objective-C Garbage Collection.
5009870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan */
518cbe163cba77c772621f89ddb33793ac170b1faDaniel DunbarBLOCK_EXPORT void *_Block_copy(const void *aBlock);
528cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
5309870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan/* Lose the reference, and if heap based and last reference, recover the memory. */
548cbe163cba77c772621f89ddb33793ac170b1faDaniel DunbarBLOCK_EXPORT void _Block_release(const void *aBlock);
558cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
568cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#if __cplusplus
578cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar}
588cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
598cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
6009870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan/* Type correct macros. */
618cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
628cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__)))
638cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#define Block_release(...) _Block_release((const void *)(__VA_ARGS__))
648cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
658cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
668cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar#endif
67