mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +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:
parent
33bbdf474e
commit
215775da59
1 changed files with 16 additions and 0 deletions
|
@ -32,6 +32,8 @@
|
|||
|
||||
#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));
|
||||
|
||||
|
@ -82,4 +84,18 @@ rb_free(void *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 */
|
||||
|
|
Loading…
Reference in a new issue