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.

23 lines
1.2 KiB
C

#pragma once
#define DEF_SCOPED_TYPE(NAME, TYPE, UNOCCUPIED, CLEANUP_FN) \
static inline TYPE _tlsl_scoped_##NAME##_unoccupied(void) { \
return (UNOCCUPIED); \
} \
static inline void _tlsl_scoped_##NAME##_cleanup(TYPE *_val) { \
CLEANUP_FN(_val); \
*_val = (UNOCCUPIED); \
} \
static inline void _tlsl_scoped_##NAME##_cleanup_raw(void *_val) { \
_tlsl_scoped_##NAME##_cleanup((TYPE *)_val); \
} \
static inline TYPE _tlsl_scoped_##NAME##_release(TYPE *_val) { \
TYPE _released = *_val; \
*_val = (UNOCCUPIED); \
return _released; \
}
#define SCOPED(NAME, TYPE, VAL) \
TYPE VAL __attribute__((cleanup(_tlsl_scoped_##NAME##_cleanup_raw))) \
= _tlsl_scoped_##NAME##_unoccupied()