4 ms·
__attribute__((cleanup)) please. It's used by systemd now and is supported by gcc and clang. It makes a real difference to code, avoiding masses of cleanup al
by rwmj 11y ago
__attribute__((cleanup)) please. It's used by systemd now and is supported by gcc and clang. It makes a real difference to code, avoiding masses of cleanup along error paths.
- JoshTriplett 11y agoAgreed. __attribute__((cleanup)) provides a way to do block-scoped cleanup of each variable and eliminate the "tear down in reverse order in each error path" or "tear down in reverse order with goto labels" pattern.
- haberman 11y agoI have never seen this before but was just reading about it. It appears to run unconditionally when the scope is exited though: how do you prevent it from tearing down even in the success case?
- rwmj 11y agoTearing down in the success case is a feature, not a bug! If you don't want cleanup along the success path (eg if you're returning an allocated string), don't use cleanup on the returned variable. However that does point to the one problem with attribute((cleanup)). If you consider code like this (using the systemd macros): int f (void) { _cleanup_free char *s1; _cleanup_free char *s2; s1 = strdup ("foo"); if (!s1) return -1; /* crash */ s2 = strdup ("bar"); if (!s2) return -1; // ... return 0; } If strdup fails, it'll crash at the place I marked because it will try to free the uninitialized pointer s2. To get around that you have to initialize everything to NULL (since free(NULL) is legal). The problem then becomes that you end up "over-initializing" and "over-freeing". It would be nice to have a version of attribute((cleanup)) that would eliminate the call to the cleanup function if the pointer is NULL (or uninitialized?). But then you're relying on the compiler to do some kind of dataflow analysis, which is difficult in a standard (excludes simple compiler implementations), and may not even be possible because of the halting problem. This is basically the reason why you wouldn't want to use cleanups in kernel code, because performance is paramount there and unnecessary cleanup calls wouldn't be welcome.
- JoshTriplett 11y agoYou could potentially write the "free" function like this: static inline void free(void *ptr) { if (!ptr) return; _free(ptr); } Then the compiler can easily inline this, omit the NULL check if it knows the pointer can't be NULL, or omit the whole thing if it knows the pointer is NULL.
- the_why_of_y 11y agoGCC 5 already does that for you (and more), since malloc/free are C standard library functions: #include <stdlib.h> int main() { free(NULL); char *foo = malloc(42); free(foo); } gcc -O1 compiles it to: 00000000004004f6 <main>: 4004f6: b8 00 00 00 00 mov $0x0,%eax 4004fb: c3 retq 4004fc: 0f 1f 40 00 nopl 0x0(%rax)
- PeCaN 11y agofree(NULL) is already perfectly legal (specced to be a no-op) as noted by the parent. The problem is that `char * s;` is probably _not_ NULL, so you'd try to free some random address on the stack. Of course it is easily fixed with `_cleanup_free char * s = NULL;`, but it's a somewhat difficult error to spot sometimes. EDIT: I super misread that, my bad.
- adrusi 11y agoThe point is that this informs the compiler of that behavior of free so that it can optimize it away. Basically it gives you some of the advantages you'd get if free were a compiler intrinsic (which it might be, idk).
- haberman 11y ago> Tearing down in the success case is a feature, not a bug! I guess it is if you are thinking of this as C++ RAII destructors, which is indeed one very useful case. It's not useful in some other cases. Like imagine that you are writing the C equivalent of a constructor. If you initialize half your members and then run out of memory, you want to uninitialize only the members you managed to initialize already. attribute((cleanup)) doesn't help with this case.
- loeg 11y ago@haberman, you don't use it for constructing an object you want to return.
- masklinn 11y agoWhen you start needing/wanting stuff like that, wouldn't it make sense to just use a C++ compiler and a simple RAII type or two?
- rwmj 11y agoReally it doesn't. If you add C++ to the mix, you lose the clear relationship with code as written to what runs (is this operator overloaded?), and you have an open invitation for people to attempt to use more and more C++ features, many of which are not well thought through. Instead of moving to C++, I'd rather allow mixed objects in a more sensible language (eg. linking with objects written in OCaml).
- xioxox 11y agoI'm not convinced by this argument. It's perfectly possible to stick to a well defined subset of C++ if you're scared by certain features. Just write a coding standards document and enforce it. Operator overloading is very important for generating the fastest code possible, too. It would be hard to get the benefits of a library like Eigen which can automatically use SSE to optimize maths expressions in C.
- loeg 11y agoNo. You don't want the vast majority of C++ semantics; especially classes, templates, and different casting rules. Additionally C++ compilers are just slower than C compilers.
- grok2 11y agoI mentioned it elsewhere, but some equivalent D's Scope Guard statement would be even better: https://dlang.org/spec/statement.html#ScopeGuardStatement https://dlang.org/spec/statement.html#ScopeGuardStatement
- ndesaulniers 11y agoRAII is the one thing I miss from C++.