xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/BlocksRuntime/Block.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric /*
2*0b57cec5SDimitry Andric  * Block.h
3*0b57cec5SDimitry Andric  *
4*0b57cec5SDimitry Andric  * Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge,
5*0b57cec5SDimitry Andric  * to any person obtaining a copy of this software and associated documentation
6*0b57cec5SDimitry Andric  * files (the "Software"), to deal in the Software without restriction,
7*0b57cec5SDimitry Andric  * including without limitation the rights to use, copy, modify, merge, publish,
8*0b57cec5SDimitry Andric  * distribute, sublicense, and/or sell copies of the Software, and to permit
9*0b57cec5SDimitry Andric  * persons to whom the Software is furnished to do so, subject to the following
10*0b57cec5SDimitry Andric  * conditions:
11*0b57cec5SDimitry Andric  *
12*0b57cec5SDimitry Andric  * The above copyright notice and this permission notice shall be included in
13*0b57cec5SDimitry Andric  * all copies or substantial portions of the Software.
14*0b57cec5SDimitry Andric  *
15*0b57cec5SDimitry Andric  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*0b57cec5SDimitry Andric  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*0b57cec5SDimitry Andric  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18*0b57cec5SDimitry Andric  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*0b57cec5SDimitry Andric  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*0b57cec5SDimitry Andric  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*0b57cec5SDimitry Andric  * SOFTWARE.
22*0b57cec5SDimitry Andric  *
23*0b57cec5SDimitry Andric  */
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric #ifndef _BLOCK_H_
26*0b57cec5SDimitry Andric #define _BLOCK_H_
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric #if !defined(BLOCK_EXPORT)
29*0b57cec5SDimitry Andric #   if defined(__cplusplus)
30*0b57cec5SDimitry Andric #       define BLOCK_EXPORT extern "C"
31*0b57cec5SDimitry Andric #   else
32*0b57cec5SDimitry Andric #       define BLOCK_EXPORT extern
33*0b57cec5SDimitry Andric #   endif
34*0b57cec5SDimitry Andric #endif
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric #if defined(__cplusplus)
37*0b57cec5SDimitry Andric extern "C" {
38*0b57cec5SDimitry Andric #endif
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric /* Create a heap based copy of a Block or simply add a reference to an existing one.
41*0b57cec5SDimitry Andric  * This must be paired with Block_release to recover memory, even when running
42*0b57cec5SDimitry Andric  * under Objective-C Garbage Collection.
43*0b57cec5SDimitry Andric  */
44*0b57cec5SDimitry Andric BLOCK_EXPORT void *_Block_copy(const void *aBlock);
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric /* Lose the reference, and if heap based and last reference, recover the memory. */
47*0b57cec5SDimitry Andric BLOCK_EXPORT void _Block_release(const void *aBlock);
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric #if defined(__cplusplus)
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric #endif
52*0b57cec5SDimitry Andric 
53*0b57cec5SDimitry Andric /* Type correct macros. */
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric #define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__)))
56*0b57cec5SDimitry Andric #define Block_release(...) _Block_release((const void *)(__VA_ARGS__))
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric #endif
60