Author Topic: Weird ideas #3: resource control  (Read 13225 times)

kyle

  • Newbie
  • *
  • Posts: 48
    • View Profile
Weird ideas #3: resource control
« on: June 19, 2014, 03:04:48 AM »
I was not sure what to call this.  One of the things I like about C# is the "using" construct because you can clean up afterward. 

Go has a similar concept, but very different syntactically with deferred functions:

f := open(...)
defer close(f)

I probably have the syntax wrong. 

The idea is that at the end of the block or function, the resource (a file here) is handled.  In this case closed.

I have started trying to build something like this with macros in C but have not gotten very far.

The idea I had was something like this:
Code: [Select]
using(buf = malloc(42)) {
   ... do savage and unnatural things to buf ...
} finally {
   free(buf);
}

The part inside the "using" would allocate or lock or create something and the code inside the finally part would clean it up.  I don't like the syntax because it seems imbalanced (parentheses for allocation and curly braces for clean up), but I like the idea a lot.  If general enough you could use it to lock mutexes, open files etc. etc.

Thoughts?

Best,
Kyle



bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Weird ideas #3: resource control
« Reply #1 on: June 19, 2014, 08:18:32 AM »

Yes, I (and a lot of others) think this is the most powerful feature of C++, destructors.
I've seen constructions in C also. The source code of Systemd uses this, you use a
GCC specific attribute, to register a function that gets called when the block exists.
This is used to free stuff like your example. I think in the systemd case, they should
have used C++ instead, because you should use a language in a way the designers
intended. I would *love* adding some C++ features like functions on objects or
interfaces, and destructors, but not in the C++ way. That would be C2++ and is
currently planned for 2034  ;D

kyle

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Weird ideas #3: resource control
« Reply #2 on: June 20, 2014, 11:40:24 PM »
Strange, when I read this thread, I don't see my reply, but when I try to reply to it I do.  ???


kyle

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Weird ideas #3: resource control
« Reply #3 on: June 20, 2014, 11:54:22 PM »
Hmm, now it is gone.  Well...

One of the syntaxes I was playing with was something like this:

Code: [Select]
use_resource(FILE *f=fopen(filename); f; fclose(f)) {
 ... do things to f ...
}

or this:

Code: [Select]
use_resource(int rc = lock_mutex(&m); rc == 0; unlock_mutex(&m)) {
 ... do things with the mutex locked...
}

"use_resource" is not a good name.  The idea is that the construct looks much like a for loop and works a bit like one too.  The first part is the initialization set.  Then the middle part must be true for the block to run.  The end part is what to do when the block terminates.

I have been trying to make this work as a macro hack in C.  I have some ideas, but I am not done.  Something about nested for loops might do it.

I did not know that systemd was using GCC-specific additions like that.  There are a lot of GNU-isms in the Linux kernel, but not usually so much in user space. 

If systemd did not cause enough heat on its own, if it was written in C++, I can't even imagine the flame wars!

Best,
Kyle