10b57cec5SDimitry Andric /* 20b57cec5SDimitry Andric * Block.h 30b57cec5SDimitry Andric * 40b57cec5SDimitry Andric * Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge, 50b57cec5SDimitry Andric * to any person obtaining a copy of this software and associated documentation 60b57cec5SDimitry Andric * files (the "Software"), to deal in the Software without restriction, 70b57cec5SDimitry Andric * including without limitation the rights to use, copy, modify, merge, publish, 80b57cec5SDimitry Andric * distribute, sublicense, and/or sell copies of the Software, and to permit 90b57cec5SDimitry Andric * persons to whom the Software is furnished to do so, subject to the following 100b57cec5SDimitry Andric * conditions: 110b57cec5SDimitry Andric * 120b57cec5SDimitry Andric * The above copyright notice and this permission notice shall be included in 130b57cec5SDimitry Andric * all copies or substantial portions of the Software. 140b57cec5SDimitry Andric * 150b57cec5SDimitry Andric * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 160b57cec5SDimitry Andric * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 170b57cec5SDimitry Andric * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 180b57cec5SDimitry Andric * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 190b57cec5SDimitry Andric * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 200b57cec5SDimitry Andric * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 210b57cec5SDimitry Andric * SOFTWARE. 220b57cec5SDimitry Andric * 230b57cec5SDimitry Andric */ 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric #ifndef _BLOCK_H_ 260b57cec5SDimitry Andric #define _BLOCK_H_ 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #if !defined(BLOCK_EXPORT) 290b57cec5SDimitry Andric # if defined(__cplusplus) 300b57cec5SDimitry Andric # define BLOCK_EXPORT extern "C" 310b57cec5SDimitry Andric # else 320b57cec5SDimitry Andric # define BLOCK_EXPORT extern 330b57cec5SDimitry Andric # endif 340b57cec5SDimitry Andric #endif 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #if defined(__cplusplus) 370b57cec5SDimitry Andric extern "C" { 380b57cec5SDimitry Andric #endif 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric /* Create a heap based copy of a Block or simply add a reference to an existing one. 410b57cec5SDimitry Andric * This must be paired with Block_release to recover memory, even when running 420b57cec5SDimitry Andric * under Objective-C Garbage Collection. 430b57cec5SDimitry Andric */ 440b57cec5SDimitry Andric BLOCK_EXPORT void *_Block_copy(const void *aBlock); 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric /* Lose the reference, and if heap based and last reference, recover the memory. */ 470b57cec5SDimitry Andric BLOCK_EXPORT void _Block_release(const void *aBlock); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric #if defined(__cplusplus) 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric #endif 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric /* Type correct macros. */ 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric #define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__))) 560b57cec5SDimitry Andric #define Block_release(...) _Block_release((const void *)(__VA_ARGS__)) 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric #endif 60