0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-02 03:32:52 +01:00

librb: Add smart-pointer macro for RAII resource management.

A good practice, especially for large community projects, preventing
leaks and error handling as clean possible without exceptions.
This commit is contained in:
Jason Volk 2016-06-22 18:15:58 -07:00
parent 33bbdf474e
commit 215775da59

View file

@ -32,6 +32,8 @@
#include <stdlib.h> #include <stdlib.h>
#define RB_UNIQUE_PTR(deleter) __attribute__((cleanup(deleter)))
#define RB_AUTO_PTR RB_UNIQUE_PTR(rb_raii_free)
void rb_outofmemory(void) __attribute__((noreturn)); void rb_outofmemory(void) __attribute__((noreturn));
@ -82,4 +84,18 @@ rb_free(void *ptr)
free(ptr); free(ptr);
} }
static inline void
rb_raii_free(const void *const ptr)
{
if(!ptr)
return;
const void *const _ptr = *(const void **)ptr;
if(!_ptr)
return;
rb_free((void *)_ptr);
}
#endif /* _I_MEMORY_H */ #endif /* _I_MEMORY_H */