You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.6 KiB
C

#pragma once
#define CHECK_TYPE(TYPE, VAL) ({ \
typedef void (*_check_type_dummy_fn_t)(TYPE); \
_check_type_dummy_fn_t _check_type_dummy_fn = (_check_type_dummy_fn_t)NULL; \
if (0) \
_check_type_dummy_fn(VAL); \
1; \
})
#define DEF_SCOPED_TYPE(NAME, TYPE, UNOCCUPIED, CLEANUP_FN) \
static inline TYPE _scoped_##NAME##_unoccupied(void) { \
return (UNOCCUPIED); \
} \
static inline void _scoped_##NAME##_cleanup(TYPE *_val) { \
CLEANUP_FN(*_val); \
*_val = (UNOCCUPIED); \
} \
static inline void _scoped_##NAME##_cleanup_raw(void *_val) { \
_scoped_##NAME##_cleanup((TYPE *)_val); \
} \
static inline TYPE _scoped_##NAME##_release(TYPE *_val) { \
TYPE _released = *_val; \
*_val = (UNOCCUPIED); \
return _released; \
}
#define SCOPED(NAME, TYPE, VAL) \
TYPE VAL __attribute__((cleanup(_scoped_##NAME##_cleanup_raw))) = _scoped_##NAME##_unoccupied()