Conversation
|
Since it's OSX-specific, should it be in stdc ? |
| } | ||
| }); | ||
|
|
||
| dyld_enumerate_tlv_storage(&handler); |
There was a problem hiding this comment.
This handler doesn't escape, does it ?
In which case the delegate literal at line 156 should be made a scope delegate to avoid needless memory allocation.
What is blocks semantic in that regard ? Is it mostly / only used for callback or can it be returned as a true delegate as well ?
There was a problem hiding this comment.
This handler doesn't escape, does it ?
I really hope it doesn't because block returns by value and handler is on the stack, which will be gone after getTLSRange completes. I can look at the source code to confirm, but I would be very surprised.
In which case the delegate literal at line 156 should be made a scope delegate to avoid needless memory allocation.
Is that possible to do on the call site?
What is blocks semantic in that regard ? Is it mostly / only used for callback or can it be returned as a true delegate as well ?
What do you mean with "true delegate"? A closure that captures local variables which can be used after a function returns? Yes.
There was a problem hiding this comment.
Is that possible to do on the call site?
Yes. Either you can use:
scope dg = (dyled_tlv_states state, dyld_tlv_info* info) { /*... */ }
auto handler = block(dg);You could change the definition of block to take a parameter of type scope R delegate(Params) dg which will mean delegate literals passed directly will never allocate. But see the next point.
What do you mean with "true delegate"? A closure that captures local variables which can be used after a function returns? Yes.
Quite a poor phrasing on my side, the question was actually "can the block escape the lexical scope in which it was declared", to which the answers seems to be yes (and so you can't use scope delegate for block's prototype).
There was a problem hiding this comment.
scope dg = ...
Aha, I've never though of doing that for anything like but classes. I'll update that.
|
Out of curiosity, what's the motivation behind the porting ? |
|
I don't know, suggestions are welcome. I had to put it somewhere. I quick search on google suggest that it works on Linux as well with the correct runtime libraries installed. I can do a test and confirm. |
|
I just did a test in a Debian container, following these instructions [1], and it works perfectly fine. |
|
I'll add some code to make it possible to call blocks from the D side as well. |
|
Suggestions for the naming convention would be appreciated as well. I think the official name is "Blocks" but I called it "Clang Blocks" for now. To avoid confusion with a regular block. Perhaps "Block" with a capital letter is enough to avoid any confusion. |
|
@MartinNowak osx_tls.c is now ported to D. |
d7db859 to
9551eba
Compare
I'll skip this because the |
|
Ok, all tests are passing. |
| #ifndef __BLOCKS__ | ||
| #error "Need a C compiler with Apple Blocks support – not building on OS X with Clang?" | ||
| #endif | ||
| #endif |
There was a problem hiding this comment.
Is there an equivalent test needed in the D version? That is, what happens if you are building on something other than OSX, or linking with non-clang libc?
I totally admit, I'm not sure what should be done, I'm just pointing out a difference between the C code and the D code.
There was a problem hiding this comment.
The content of this file is moved into sections_osx_x86_64.d, which is already properly guarded for OS X 64bit.
The code which makes calling Blocks possible, clang_block.d, is cross-platform. Using that code only make sense if you have a C function taking Block you want to call. That C code needs to be compiled with Clang, and linking that on non-Apple platforms requires the Blocks runtime, which is noted in the documentation. We can't do anything, as far as I know, on the D side to enforce this.
If you try to compile the C code with something else than Clang you'll get a syntax error. Linking without the Blocks runtime will cause undefined symbol errors.
There was a problem hiding this comment.
So far the tests for clang_block.d is only enabled on OS X because it has everything required out of the box. If we install Clang and the Blocks runtime on the other machines used by the autotester I can enable it for those other platforms as well.
I would definitely not put it in Either we can have a |
| * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) | ||
| * Authors: Jacob Carlborg | ||
| * Source: $(DRUNTIMESRC core/_clang_block.d) | ||
| */ |
It works on other platforms than OS X (that's why I put it in core.stdc) but I guessing it's not going to be used on any non-Apple platform. I did not want to put it in the |
I think we should separate what is used for implementing the runtime and what druntime expose. |
There's a specific reason why I chose to expose the Blocks related code. That's because it's very useful to be able to interface with functions taking Blocks on OS X.
I don't see a reason why not. |
Clang Blocks, or just Blocks [1], is an extension to the C family of languages which adds support for anonymous functions, much like delegates in D. Being able to interface with Blocks is important when interfacing with Apple specific API's since some of the API's are only available in a form requiring using Blocks. Interfacing with Blocks is done by directly using the underlying ABI. A Block basically consist of a struct containing a function pointer to the body of the Block, any captured values and some metadata. The ABI that is used is the Apple ABI [2]. It's possible to use Blocks on non-Apple platforms as well. This requires to compile the C code using Clang and install and link with the Blocks runtime [3]. [1] Language specification for Blocks: http://clang.llvm.org/docs/BlockLanguageSpec.html [2] http://clang.llvm.org/docs/Block-ABI-Apple.html [3] http://compiler-rt.llvm.org
cea56eb to
550edd0
Compare
Okay, let's wait for some other opinion on this topic then. |
|
Blocks aren't standard C, so they don't belong into core.stdc, use core.sys.osx.something. |
|
|
Not sure what you mean with "hacks". But yes, it uses/follows http://clang.llvm.org/docs/Block-ABI-Apple.html |
Exactly. If we install Clang and the Blocks Runtime on the autotester machines I could enable the tests on more platforms. |
No description provided.