static-destruct: introduce STATIC_ARRAY_DESTRUCTOR_REGISTER()
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
/* A framework for registering static variables that shall be freed on shutdown of a process. It's a bit like gcc's
|
||||
* destructor attribute, but allows us to precisely schedule when we want to free the variables. This is supposed to
|
||||
@@ -25,9 +26,24 @@
|
||||
* packed next to each other so that we can enumerate it. */ \
|
||||
_variable_no_sanitize_address_
|
||||
|
||||
typedef struct StaticDestructor {
|
||||
typedef enum StaticDestructorType {
|
||||
STATIC_DESTRUCTOR_SIMPLE,
|
||||
STATIC_DESTRUCTOR_ARRAY,
|
||||
_STATIC_DESTRUCTOR_TYPE_MAX,
|
||||
_STATIC_DESTRUCTOR_INVALID = -EINVAL,
|
||||
} StaticDestructorType;
|
||||
|
||||
typedef struct SimpleCleanup {
|
||||
void *data;
|
||||
free_func_t destroy;
|
||||
} SimpleCleanup;
|
||||
|
||||
typedef struct StaticDestructor {
|
||||
StaticDestructorType type;
|
||||
union {
|
||||
SimpleCleanup simple;
|
||||
ArrayCleanup array;
|
||||
};
|
||||
} StaticDestructor;
|
||||
|
||||
#define STATIC_DESTRUCTOR_REGISTER(variable, func) \
|
||||
@@ -41,10 +57,25 @@ typedef struct StaticDestructor {
|
||||
} \
|
||||
_common_static_destruct_attrs_ \
|
||||
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
|
||||
.data = &(variable), \
|
||||
.destroy = UNIQ_T(static_destructor_wrapper, uq), \
|
||||
.type = STATIC_DESTRUCTOR_SIMPLE, \
|
||||
.simple.data = &(variable), \
|
||||
.simple.destroy = UNIQ_T(static_destructor_wrapper, uq), \
|
||||
}
|
||||
|
||||
#define STATIC_ARRAY_DESTRUCTOR_REGISTER(a, n, func) \
|
||||
_STATIC_ARRAY_DESTRUCTOR_REGISTER(UNIQ, a, n, func)
|
||||
|
||||
#define _STATIC_ARRAY_DESTRUCTOR_REGISTER(uq, a, n, func) \
|
||||
/* Type-safety check */ \
|
||||
_unused_ static void (* UNIQ_T(static_destructor_wrapper, uq))(typeof(a[0]) *x, size_t y) = (func); \
|
||||
_common_static_destruct_attrs_ \
|
||||
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
|
||||
.type = STATIC_DESTRUCTOR_ARRAY, \
|
||||
.array.parray = (void**) &(a), \
|
||||
.array.pn = &(n), \
|
||||
.array.pfunc = (free_array_func_t) (func), \
|
||||
};
|
||||
|
||||
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work
|
||||
* even if no destructors are defined and the section is missing. */
|
||||
extern const StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[];
|
||||
@@ -59,5 +90,16 @@ static inline void static_destruct(void) {
|
||||
for (const StaticDestructor *d = ALIGN_PTR(__start_SYSTEMD_STATIC_DESTRUCT);
|
||||
d < __stop_SYSTEMD_STATIC_DESTRUCT;
|
||||
d = ALIGN_PTR(d + 1))
|
||||
d->destroy(d->data);
|
||||
switch (d->type) {
|
||||
case STATIC_DESTRUCTOR_SIMPLE:
|
||||
d->simple.destroy(d->simple.data);
|
||||
break;
|
||||
|
||||
case STATIC_DESTRUCTOR_ARRAY:
|
||||
array_cleanup(&d->array);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user