将 RAII 引入 C (当您没有 cleanup()时)的一个解决方案是用执行清理的代码包装函数调用。这也可以打包在一个整洁的宏中(如最后所示)。
/* Publicly known method */
void SomeFunction() {
/* Create raii object, which holds records of object pointers and a
destruction method for that object (or null if not needed). */
Raii raii;
RaiiCreate(&raii);
/* Call function implementation */
SomeFunctionImpl(&raii);
/* This method calls the destruction code for each object. */
RaiiDestroyAll(&raii);
}
/* Hidden method that carries out implementation. */
void SomeFunctionImpl(Raii *raii) {
MyStruct *object;
MyStruct *eventually_destroyed_object;
int *pretend_value;
/* Create a MyStruct object, passing the destruction method for
MyStruct objects. */
object = RaiiAdd(raii, MyStructCreate(), MyStructDestroy);
/* Create a MyStruct object (adding it to raii), which will later
be removed before returning. */
eventually_destroyed_object = RaiiAdd(raii,
MyStructCreate(), MyStructDestroy);
/* Create an int, passing a null destruction method. */
pretend_value = RaiiAdd(raii, malloc(sizeof(int)), 0);
/* ... implementation ... */
/* Destroy object (calling destruction method). */
RaiiDestroy(raii, eventually_destroyed_object);
/* or ... */
RaiiForgetAbout(raii, eventually_destroyed_object);
}
#include <stdio.h>
#include <stdlib.h>
static char* watched2;
__attribute__((constructor))
static void init_static_vars()
{
printf("variable (%p) is initialazed, initial value (%p)\n", &watched2, watched2);
watched2=malloc(1024);
}
__attribute__((destructor))
static void destroy_static_vars()
{
printf("variable (%p), value( %p) goes out of scope\n", &watched2, watched2);
free(watched2);
}
int main(void)
{
printf("exit from main, variable (%p) value(%p) is static\n", &watched2, watched2);
return 0;
}
这是一个测试:
>./example
variable (0x600aa0) is initialazed, initial value ((nil))
exit from main, variable (0x600aa0) value(0x16df010) is static
variable (0x600aa0), value( 0x16df010) goes out of scope
my implementation of raii for c in pure c and minimal asm
@ https://github.com/smartmaster/sml_clang_raii
**RAII for C language in pure C and ASM**
**featurs : **
-easy and graceful to use
- no need seperate free cleanup functions
- able to cleanup any resources or call any function on scope exits
**User guide : **
-add source files in src folder to your project
-include sml_raii_clang.h in.c file
-annote resource and its cleanup functions
/*
示例代码
*/
void sml_raii_clang_test()
{
//start a scope, the scope name can be any string
SML_RAII_BLOCK_START(0);
SML_RAII_VOLATILE(WCHAR*) resA000 = calloc(128, sizeof(WCHAR)); //allocate memory resource
SML_RAII_START(0, resA000); //indicate starting a cleanup code fragment, here 'resA000' can be any string you want
if (resA000) //cleanup code fragment
{
free(resA000);
resA000 = NULL;
}
SML_RAII_END(0, resA000); //indicate end of a cleanup code fragment
//another resource
//////////////////////////////////////////////////////////////////////////
SML_RAII_VOLATILE(WCHAR*) res8000 = calloc(128, sizeof(WCHAR));
SML_RAII_START(0, D000);
if (res8000)
{
free(res8000);
res8000 = NULL;
}
SML_RAII_END(0, D000);
//scope ended, will call all annoated cleanups
SML_RAII_BLOCK_END(0);
SML_RAII_LABEL(0, resA000); //if code is optimized, we have to put labels after SML_RAII_BLOCK_END
SML_RAII_LABEL(0, D000);
}