From 465655674b9c5687eaaa2e6c9463429f69eff0fd Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 1 Jan 2023 19:23:08 +0100 Subject: [PATCH] dso: dynamically allocate destructor table This saves static memory on mono targets where it's scarce. --- src/dso.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/dso.c b/src/dso.c index 0dc7215..8b572ba 100644 --- a/src/dso.c +++ b/src/dso.c @@ -1,4 +1,4 @@ -#include +#include /* We don't support shared object loading, so provide a single handle */ __attribute__((visibility("hidden"))) @@ -13,12 +13,18 @@ struct dtor { void *d; }; -static struct dtor _dtors[ATEXIT_MAX]; +static struct dtor *_dtors; static int _dtor_count = 0; +__attribute__((constructor)) +static void alloc_dtors(void) +{ + _dtors = malloc(ATEXIT_MAX * sizeof *_dtors); +} + int __cxa_atexit(void (*f)(void *), void *p, void *d) { - if(_dtor_count >= ATEXIT_MAX) + if(!_dtors || _dtor_count >= ATEXIT_MAX) return 1; _dtors[_dtor_count++] = (struct dtor){ f, p, d }; return 0;