Wiki Article
Blocks (C language extension)
Nguồn dữ liệu từ Wikipedia, hiển thị bởi DefZone.Net
Blocks are a non-standard extension added by Apple Inc. to the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Using this feature requires support from the compiler as well as a runtime library.
Like function definitions, blocks can take arguments, and declare their own variables internally. Unlike ordinary C function definitions, their value can capture state from their surrounding context. A block definition produces an opaque value which contains both a reference to the code within the block and a snapshot of the current state of local stack variables at the time of its definition. The block may be later invoked in the same manner as a function pointer. The block may be assigned to variables, passed to functions, and otherwise treated like a normal function pointer, although the application programmer (or the API) must mark the block with a special operator (Block_copy) if it's to be used outside the scope in which it was defined. Given a block value, the code within the block can be executed at any later time by calling it, using the same syntax that would be used for calling a function.
Blocks were originally introduced in 2009 with the explicit goal of making it easier to write programs for the Grand Central Dispatch (libdispatch) threading architecture.[1][2] It does not, however, rely on libdispatch; instead it depends on its own (much simpler) runtime library to work. A blocks runtime library is preinstalled for Mac OS X 10.6+ and iOS 4.0+.[3]
Scope of support
[edit]There are only two families of compilers that implement blocks: Apple's own branch of the GNU Compiler Collection (Apple GCC 4.2.1 and LLVM-GCC) and the upstream Clang LLVM compiler front end. Apple GCC is abandoned since circa 2011 (XCode 4.2).[3] The most up-to-date description of this language extension is found in clang documentation."Clang Language Extensions: Blocks". LLVM Project. Retrieved 2013-01-20.
As mentioned above, Apple operating systems released after 2009 have a built-in blocks runtime. The LLVM project provides a "target-independent" runtime that should work for every operating system supported by LLVM.[4]
Applications
[edit]As mentioned before the main user of blocks is libdispatch, which is used in a variety of Apple products such as the Swift standard library.
Blocks syntax is part of the OpenCL C standard in versions 2.0 and 3.0. It is used to enqueue kernels from within kernels.[5] The Khronos Group also defines C++-based heterogenous programming languages including SYCL and C++ for OpenCL. Both use C++'s standard lambda expressions instead.[6]
Examples
[edit]A simple example capturing mutable state in the surrounding scope is an integer range iterator:[7]
/* blocks-test.c */
#include <stdio.h>
#include <Block.h>
/* Type of block taking nothing returning an int */
typedef int (^IntBlock)();
IntBlock MakeCounter(int start, int increment) {
__block int i = start;
return Block_copy( ^(void) {
int ret = i;
i += increment;
return ret;
});
}
int main(void) {
IntBlock mycounter = MakeCounter(5, 2);
printf("First call: %d\n", mycounter());
printf("Second call: %d\n", mycounter());
printf("Third call: %d\n", mycounter());
/* because it was copied, it must also be released */
Block_release(mycounter);
return 0;
}
Compile and execute
[edit]$ clang -fblocks blocks-test.c # Mac OS X
$ ./a.out
First call: 5
Second call: 7
Third call: 9
The blocks runtime is not part of the C library(s) linked by default on some systems. If this is the case, it is required to explicitly link to this library:
$ clang -fblocks blocks-test.c -lBlocksRuntime # Linux
The runtime is a part of clang's runtime, but is sometimes not installed with the clang package. A standalone runtime extracted from compiler-rt is available.[8]
Relation to GCC nested functions
[edit]Blocks bear a superficial resemblance to GCC's extension of C to support lexically scoped nested functions. However, GCC's nested functions, unlike blocks, must not be called after the containing scope has exited, as that would result in undefined behavior.[9]
GCC-style nested functions currently use dynamic creation of executable thunks on most architectures when taking the address of the nested function. On most architectures (including X86), these thunks are created on the stack, which requires marking the stack executable. Executable stacks are generally considered to be a potential security hole. Blocks do not require the use of executable thunks, so they do not share this weakness. On the other hand, blocks introduces a completely new type for the pointer, while pointers to nested functions in GCC are regular function pointers and can be used directly with existing code.
See also
[edit]- Closure (computer science)
- Lexical scope
- Lambda (programming)
- Spaghetti stack
- XNU
- C++11 (includes "lambda expressions")
References
[edit]- ^ "Grand Central Dispatch" (PDF) (technology brief). Apple. 2009-09-03. Archived from the original (PDF) on 2009-09-20. Retrieved June 9, 2009.
- ^ Siracusa, John (September 1, 2009). "Mac OS X 10.6 Snow Leopard: the Ars Technica review: Blocks". Ars Technica.
- ^ a b "Blocks Programming Topics". Apple Developer. Apple Inc. Retrieved 2011-03-08.
- ^ ""compiler-rt" Runtime Library". compiler-rt.llvm.org.
- ^
- Munshi, Aaftab, ed. (2013-07-13). "The OpenCL C Specification. Version 2.0. Document Revision 11" (PDF). Khronos OpenCL Working Group. p. 173. Archived from the original (PDF) on 2013-11-05. Retrieved 2013-07-23.
- "The OpenCL™ C Specification". registry.khronos.org.
- ^ "SYCL – C++ Single-source Heterogeneous Programming for OpenCL". Khronos Group. 2014-01-21. Archived from the original on January 18, 2021. Retrieved October 24, 2016.
- ^ Bengtsson, Joachim. "Programming with C Blocks on Apple Devices". Archived from the original on 2017-11-15. Retrieved 2009-09-17.
- ^ "mackyle/blocksruntime: standalone blocks runtime". Retrieved 15 January 2020.
- ^ "Nested Functions: Using the GNU Compiler Collection (GCC)".
External links
[edit]- "Plausible Blocks (PLBlocks)". Google Code. 2009. Retrieved 2021-01-26. runtime and toolchain for using blocks in iPhone 2.2+ and Mac OS X 10.5 (of historical relevance only)