dso: dynamically allocate destructor table

This saves static memory on mono targets where it's scarce.
This commit is contained in:
Lephenixnoir 2023-01-01 19:23:08 +01:00
parent 1da9d10226
commit 465655674b
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495

View file

@ -1,4 +1,4 @@
#include <stddef.h>
#include <stdlib.h>
/* 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;