mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-11 07:13:20 +00:00
Add support for the clang compiler; update RAII_VAR to use BlocksRuntime
RAII_VAR, which is used extensively in Asterisk to manage reference counted resources, uses a GCC extension to automatically invoke a cleanup function when a variable loses scope. While this functionality is incredibly useful and has prevented a large number of memory leaks, it also prevents Asterisk from being compiled with clang. This patch updates the RAII_VAR macro such that it can be compiled with clang. It makes use of the BlocksRuntime, which allows for a closure to be created that performs the actual cleanup. Note that this does not attempt to address the numerous warnings that the clang compiler catches in Asterisk. Much thanks for this patch goes to: * The folks on StackOverflow who asked this question and Leushenko for providing the answer that formed the basis of this code: http://stackoverflow.com/questions/24959440/rewrite-gcc-cleanup-macro-with-nested-function-for-clang * Diederik de Groot, who has been extremely patient in working on getting this patch into Asterisk. Review: https://reviewboard.asterisk.org/r/4370/ ASTERISK-24133 ASTERISK-23666 ASTERISK-20399 ASTERISK-20850 #close Reported by: Diederik de Groot patches: RAII_CLANG.patch uploaded by Diederik de Groot (License 6600) ........ Merged revisions 432807 from http://svn.asterisk.org/svn/asterisk/branches/11 git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@432808 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -1005,12 +1005,33 @@ char *ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size);
|
||||
* }
|
||||
* \endcode
|
||||
*/
|
||||
#define RAII_VAR(vartype, varname, initval, dtor) \
|
||||
/* Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774 */ \
|
||||
auto void _dtor_ ## varname (vartype * v); \
|
||||
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
|
||||
|
||||
#if defined(__clang__)
|
||||
|
||||
#if defined(__has_feature) && __has_feature(blocks)
|
||||
typedef void (^_raii_cleanup_block_t)(void);
|
||||
static inline void _raii_cleanup_block(_raii_cleanup_block_t *b) { (*b)(); }
|
||||
|
||||
#define RAII_VAR(vartype, varname, initval, dtor) \
|
||||
_raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = NULL; \
|
||||
vartype varname = initval; \
|
||||
_raii_cleanup_ ## varname = ^{ dtor(varname); }
|
||||
|
||||
#else
|
||||
#error "CLANG must support the 'blocks' feature to compile Asterisk."
|
||||
#endif /* #if defined(__has_feature) && __has_feature(blocks) */
|
||||
|
||||
#elif defined(__GNUC__)
|
||||
|
||||
#define RAII_VAR(vartype, varname, initval, dtor) \
|
||||
auto void _dtor_ ## varname (vartype * v); \
|
||||
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
|
||||
vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
|
||||
|
||||
#else
|
||||
#error "Cannot compile Asterisk: unknown and unsupported compiler."
|
||||
#endif /* #if __GNUC__ */
|
||||
|
||||
/*!
|
||||
* \brief Asterisk wrapper around crypt(3).
|
||||
*
|
||||
|
Reference in New Issue
Block a user