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.

27 lines
1.4 KiB
C

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#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()