LWN.net Logo

Object-oriented design patterns in the kernel, part 1

Object-oriented design patterns in the kernel, part 1

Posted Jun 2, 2011 19:04 UTC (Thu) by Trelane (guest, #56877)
In reply to: Object-oriented design patterns in the kernel, part 1 by Cyberax
Parent article: Object-oriented design patterns in the kernel, part 1

Thanks for the pointers! (heh) It will make my code suck much less. :)


(Log in to post comments)

Object-oriented design patterns in the kernel, part 1

Posted Jun 2, 2011 22:42 UTC (Thu) by cmccabe (guest, #60281) [Link]

Golang has the "defer" statement, which sets up some statements which will be executed when the current scope exits. So you might do something like this
> func foo() {
>     defer fmt.Println("running cleanup");
>    ...
> }
Then the printout will happen when you return from the function, whenever that is.

C, of course, has the single exit point idiom, which is used a lot in the kernel. It looks like this:

> int foo() {
>     setup_bar();
>     ...
>     if (ret)
>         goto cleanup;
>     ...
> cleanup:
>     shutdown_bar();
>     return ret;
> }
Java, Python, and Ruby have try...finally { }

I have to be honest; I don't think dumping that pile of macros and templates into your code will make it "suck less." I think you just need to use the mechanisms C++ gives you, namely try and catch, and RAII. It may be an extra line or two, but the programmers trying to read your code after you've moved on will thank you.

Especially when you're debugging something, flow control macros are not your friends. Alexandrescu may be able to figure it out in gdb, but he probably doesn't work with you.

Object-oriented design patterns in the kernel, part 1

Posted Jun 2, 2011 22:53 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link]

To be fair, ON_SCOPE_EXIT is quite nice. Its functionality is very simple and restricted and it's indispensable when one needs to do something simple, usually with non-C++ resources (like calling fclose on FILE*).

It's also easy to debug. Though I tend to spend much less time in a debugger when working with C++ code, all this strict typing pays off.

C++0x has lambdas so it's possible to write analog of 'defer' from Golang (indeed, Boost already has it).

Object-oriented design patterns in the kernel, part 1

Posted Jun 3, 2011 1:16 UTC (Fri) by Trelane (guest, #56877) [Link]

Thanks to both of your for the info; I am learning a lot. Yet again LWN proves to be the only place where the comments have value. :)

Object-oriented design patterns in the kernel, part 1

Posted Jun 3, 2011 9:46 UTC (Fri) by cortana (subscriber, #24596) [Link]

FYI, you can still use shared_ptr with FILE* (and probably any other C API that follows the same pattern.

shared_ptr<FILE> f (fopen ("whatever", "r"), fclose);

Now, fclose will be called whenever the last copy of that shared_ptr is destructed. No more single exit points, goto tricks, or leaning towers of if statements!

Object-oriented design patterns in the kernel, part 1

Posted Jun 3, 2011 12:30 UTC (Fri) by cmccabe (guest, #60281) [Link]

That's pretty good, and it relies on things that are actually in the standard library. Thanks.

Object-oriented design patterns in the kernel, part 1

Posted Jun 3, 2011 14:19 UTC (Fri) by cortana (subscriber, #24596) [Link]

This and std::vector are the best things in C++! :)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds