Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #93931

Re: Generic read/write function - casting vs union

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Generic read/write function - casting vs union
Date 2016-09-23 09:59 +0200
Organization A noiseless patient Spider
Message-ID <ns2nd8$dat$1@dont-email.me> (permalink)
References <ns08ge$o5v$1@dont-email.me> <ns0hps$mc6$1@dont-email.me> <ns0jt5$tfh$1@dont-email.me> <ns0l81$2mb$1@dont-email.me> <ns0moe$7td$1@dont-email.me>

Show all headers | View raw


On 22/09/16 15:36, Noob wrote:
> On 22/09/2016 15:10, David Brown wrote:
> 
>> On 22/09/16 14:46, Noob wrote:
>>
>>> As you allude to, there is some context between PROLOG and EPILOG.
>>>
>>> PROLOG does several "setup" operations, which must be "torn down"
>>> in the reverse order in EPILOG, with proper error-handling, meaning
>>> we cannot tear down a resource that has not been set up.
>>>
>>> PROLOG:
>>>   alloc #1
>>>   if (failure) goto L1;
>>>   alloc #2
>>>   if (failure) goto L2;
>>>   alloc #3
>>>   if (failure) goto L3;
>>>
>>> EPILOG:
>>>   tear_down(#3);
>>> L3:
>>>   tear_down(#2);
>>> L2:
>>>   tear_down(#1);
>>> L1:
>>>   return err;
>>>
>>> Also I'm not a fan of separating PROLOG and EPILOG, because they
>>> are obviously closely related, and it doesn't make sense to call
>>> one without the other. But it's interesting to have other points
>>> of view.
>>
>> How portable does your code need to be?  If you can use gcc extensions,
> 
> This code will be submitted to the Linux kernel. So C++ is out ;-)

So C++ is out, but gcc extensions are allowed.  There is an on-going
project to make the kernel compilation work with clang, so you'll annoy
people if the extension is gcc only, but clang supports most of gcc's
extensions so you are probably okay here.

But you may need to check with the relevant maintainers before being the
first to use a particular gcc extension in the kernel.

> They do use many gcc extensions (e.g. updating void pointers)
> but git grep '((cleanup))' returns nothing for Linux 4.7
> 
> https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
> 
>> cleanup (cleanup_function)
>>
>> The cleanup attribute runs a function when the variable goes out of
>> scope. This attribute can only be applied to auto function scope
>> variables; it may not be applied to parameters or variables with
>> static storage duration. The function must take one parameter, a
>> pointer to a type compatible with the variable. The return value of
>> the function (if any) is ignored.
>>
>> If -fexceptions is enabled, then cleanup_function is run during the
>> stack unwinding that happens during the processing of the exception.
>> Note that the cleanup attribute does not allow the exception to be
>> caught, only to perform an action. It is undefined what happens if
>> cleanup_function does not return normally.
> 
> "Exceptions" means C++ right? (not C)

Sort of.  gcc supports exceptions to some extent in C as well - mainly
with a view to allowing exceptions to pass through C compiled functions
on their way between two C++ functions.  But for a C only project (like
the kernel), "-fexceptions" is not enabled, so you don't have to worry
about this one.

> 
> I'm not familiar with this attribute. Thanks for pointing it out.
> Does that mean one has to define a function for every variable
> that is to be allocated? And test in every function before
> actually cleaning up?

I have not used it myself - I am just throwing it out here as a
suggestion that you can investigate.  So it may not be suitable, or it
may make your code really messy - if so, then at least you'll have
learned something new :-)

The cleanup function gets a pointer to the variable to be "cleaned" as
an argument - this may mean that you only need the one function for all
the cleanups.

And since you are in gcc extension mode, you might also use a local
nested function for your cleanup function to keep everything neatly in
the one place.

Have fun with the idea, anyway.

> 
> 
>> you could consider using "cleanup" functions as a sort of C++
>> destructor.  Then your code becomes:
>>
>> PROLOG:
>> 	alloc #1 with cleanup function 1
>> 	if (failure) return err;
>> 	alloc #2 with cleanup function 2
>> 	if (failure) return err;
>> 	alloc #3 with cleanup function 3
>> 	if (failure) return err;
>>
>> ...
>>
>> EPILOG:
>> 	return success
>>
>> Your cleanup functions are called in reverse order, and only if your
>> code has got as far as declaring them.
> 
> Regards.
> 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 11:33 +0200
  Re: Generic read/write function - casting vs union Richard Heathfield <rjh@cpax.org.uk> - 2016-09-22 11:07 +0100
    Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 12:36 +0200
      Re: Generic read/write function - casting vs union Richard Heathfield <rjh@cpax.org.uk> - 2016-09-22 14:07 +0100
    Re: Generic read/write function - casting vs union Keith Thompson <kst-u@mib.org> - 2016-09-22 11:48 -0700
    Re: Generic read/write function - casting vs union raltbos@xs4all.nl (Richard Bos) - 2016-09-23 19:42 +0000
  Re: Generic read/write function - casting vs union BartC <bc@freeuk.com> - 2016-09-22 13:11 +0100
    Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 14:46 +0200
      Re: Generic read/write function - casting vs union David Brown <david.brown@hesbynett.no> - 2016-09-22 15:10 +0200
        Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-22 15:36 +0200
          Re: Generic read/write function - casting vs union Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-22 19:28 -0700
          Re: Generic read/write function - casting vs union David Brown <david.brown@hesbynett.no> - 2016-09-23 09:59 +0200
      Re: Generic read/write function - casting vs union Barry Schwarz <schwarzb@dqel.com> - 2016-09-22 20:11 -0700
      Re: Generic read/write function - casting vs union Richard Damon <Richard@Damon-Family.org> - 2016-09-23 11:13 -0400
  Re: Generic read/write function - casting vs union Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-22 08:35 -0700
    Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-25 23:17 +0200
    Re: Generic read/write function - casting vs union Noob <root@127.0.0.1> - 2016-09-26 14:54 +0200
      Re: Generic read/write function - casting vs union boon <root@localhost.localdomain> - 2016-09-26 19:47 +0200
      Re: Generic read/write function - casting vs union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-26 19:23 +0100
      Re: Generic read/write function - casting vs union Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-27 13:10 -0700
      Re: Generic read/write function - casting vs union David Thompson <dave.thompson2@verizon.net> - 2016-10-16 18:08 -0400
  Re: Generic read/write function - casting vs union Anton Shepelev <anton.txt@gmail.com> - 2016-10-17 23:40 +0300
  Re: Generic read/write function - casting vs union albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-11 15:20 +0100
    Re: Generic read/write function - casting vs union supercat@casperkitty.com - 2016-12-11 09:38 -0800

csiph-web