A simple library to provide RAII in standard-compliant C99, using raii_with(resource, initializer, destructor) { ... }-syntax:
A simple library to provide RAII (or something very much like it) in standard-compliant C99, using raii_with(resource, initializer, destructor) { ... }
-syntax:
#include "raii/raii.h"
int main() {
raii_with(int *myint, safe_malloc(sizeof(int)), free){
*myint = 10;
printf("myint is located at %p and contains: %d\n", myint, *myint);
printf("%s\n", __func__);
}
int res = safe_return_example();
printf("res: %d\n", res);
printf("Test\n");
}
Resource Acquisition Is Initialization (or, if you prefer, Resource Relinquishment is Destruction) is a pattern that C++ and some other languages use to ensure that a resource is automatically released once you are done working with it.
The raii library for C99 tries to emulate this, by letting you write raii_with
statements
that are similar to the with
-statements of e.g. Python.
The advantages over managing resources manually are:
goto
s to labels at the end of the function, is prone to mistakes when new content is added to the function or the internal order of resources is swapped.Instead, raii:
raii_with
only exist in the block you provide. Referencing after the block will result in a compiler error, so that's a whole lot of bugs you don't need to worry about anymore!safe_return
to return early from within a raii_with
-block.continue
or break
to jump to the end of the current raii_with
-block.raii uses a custom control structure macro to ensure you can pass any code block to the raii_with
-statement.
For more information, you are encouraged to read the inline documentation in the raii/raii.h
file.
For more information about writing Custom Control Structure Macro's, see my related library exceptional that introduces block-style exceptions to C99, and the there-linked further resources as well.
return
from within raii_with
, but only safe_return
, because otherwise the destructors will not be run.var_decl
; the destructor will still be run on the original structure, because raii
keeps its own reference to the resource.I believe both of these things are very logical and easy to keep in mind. (resulting in raii being a near-leak-free abstraction). If you know of a way to further reduce these without introducing new ones, please share it!
I'd like to thank: