Page MenuHomeFreeBSD

fts: Add blocks support.
ClosedPublic

Authored by des on Thu, Apr 17, 5:55 PM.
Tags
None
Referenced Files
F115550847: D49877.id153879.diff
Fri, Apr 25, 5:22 AM
F115549443: D49877.id153878.diff
Fri, Apr 25, 4:54 AM
F115543740: D49877.id154094.diff
Fri, Apr 25, 2:55 AM
F115538987: D49877.id153874.diff
Fri, Apr 25, 1:16 AM
F115536163: D49877.id153940.diff
Fri, Apr 25, 12:24 AM
F115534508: D49877.id153958.diff
Thu, Apr 24, 11:49 PM
F115532748: D49877.id153782.diff
Thu, Apr 24, 11:12 PM
F115530607: D49877.id.diff
Thu, Apr 24, 10:30 PM

Details

Summary

This adds an fts_open_b() variant of fts_open() which takes a block
instead of a function pointer.

This was inspired by, and is intended to be compatible with, Apple's
implementation; however, although our FTS and theirs share a common
ancestor, they have diverged significantly. That and the fact that
we still target compilers which don't support blocks means Apple's
implementation was not directly reusable.

MFC after: never
Relnotes: yes
Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 63595
Build 60479: arc lint + arc unit

Event Timeline

des requested review of this revision.Thu, Apr 17, 5:55 PM
theraven added inline comments.
lib/libc/gen/fts.c
266

This will crash if the blocks runtime is not linked, which is not very friendly to callers. It would be nice if it either:

  • Gracefully exited with ENOSYS or similar if a blocks runtime is not present (and, ideally, documented this in the man page), or
  • Skipped this call if the block's isa pointer is &_NSConcreteGlobalBlock (i.e. the block has no captures).

The cast should not be required here. The Block_copy macro should cast the result to typeof the argument.

353

As above, this will crash if the blocks runtime isn't implemented, though (with the current code) we'll crash on the copy here first.

review feedback + memory issues

lib/libc/gen/fts.c
266

I don't see how compar->isa == &_NSConcreteGlobalBlock saves me from having to copy the block. The block descriptor itself could still go out of scope if the caller returns before calling fts_close().

des marked 2 inline comments as done.Fri, Apr 18, 5:16 PM
lib/libc/gen/fts.c
266

If the block is a global block then the block structure is a global and is never deallocated. The block *descriptor* is always a global (and is never copied by _Block_copy.

lib/libc/gen/fts.c
266

something like this?

@@ -59,6 +59,7 @@
 /* only present if linked with blocks runtime */
 void *_Block_copy(const void *) __weak_symbol;
 void _Block_release(const void *) __weak_symbol;
+extern void *_NSConcreteGlobalBlock[] __weak_symbol;
 
 static FTSENT  *fts_alloc(FTS *, char *, size_t);
 static FTSENT  *fts_build(FTS *, int);
@@ -274,6 +275,7 @@
 #ifdef __BLOCKS__
        compar = Block_copy(compar);
 #else
+       if (compar->isa != &_NSConcreteGlobalBlock)
                compar = _Block_copy(compar);
 #endif /* __BLOCKS__ */
        if (compar == NULL) {
@@ -287,6 +289,7 @@
 #ifdef __BLOCKS__
                Block_release(compar);
 #else
+               if (compar->isa != &_NSConcreteGlobalBlock)
                        _Block_release(compar);
 #endif /* __BLOCKS__ */
        }
This revision is now accepted and ready to land.Sat, Apr 19, 5:48 PM

don't copy global blocks

This revision now requires review to proceed.Sat, Apr 19, 9:14 PM
des marked an inline comment as done.Sat, Apr 19, 9:14 PM

Seems reasonable to me.
Might be useful in the tests to have the blocks runtime added more centrally, but that looks kinda tricky, so what's here is fine.
I can't review the block stuff in detail since I've not used it, but it seems right or very close to right.

This revision is now accepted and ready to land.Tue, Apr 22, 3:16 PM
kevans added inline comments.
lib/libc/tests/gen/Makefile
10

This needs to be scoped to COMPILER_TYPE == clang

jrtc27 added inline comments.
lib/libc/tests/gen/Makefile
10

Maybe worth a COMPILER_FEATURES entry now we're going from n=1 in lib/libc/tests/stdlib/Makefile to n=2? (Especially with my CheriBSD hat on as we currently don't implement -fblocks for CHERI so have to disable using it even for Clang)

This revision now requires review to proceed.Tue, Apr 22, 3:43 PM
des marked 2 inline comments as done.Tue, Apr 22, 3:46 PM
des added inline comments.
lib/libc/tests/gen/Makefile
10

I'm not opposed, but that's out of scope for this review.

des marked an inline comment as done.Tue, Apr 22, 3:46 PM
lib/libc/tests/gen/Makefile
10

If people think it's not worth it then that's fine, but I don't agree about it being out of scope. The point at which one starts copying code or patterns is a justifiable point at which that should be extracted out, just as how you extracted __fts_open out rather than duplicate the implementation (and had you duplicated it then it would be completely reasonable to object to that). The only difference here is the code in question is a single if so there's less perceived benefit to extracting it out, it's only really us downstream that would benefit (and even then, it's not a huge issue).

Please amend the commit message to point out that this may be worth a COMPILER_FEATURES entry in case it inspires someone to try and add more blocks stuff elsewhere, but it is hard to see the value with this little of proliferation at the moment

This revision is now accepted and ready to land.Tue, Apr 22, 5:09 PM
This revision was automatically updated to reflect the committed changes.