core-lightning/common/autodata.c
Rusty Russell b594c53771 common/autodata: autodata replacement which uses __attribute__((constructor))
This is a GCC extension, but avoids much other messiness.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-09-21 18:04:43 +02:00

41 lines
746 B
C

#include "config.h"
#include <assert.h>
#include <ccan/strmap/strmap.h>
#include <common/autodata.h>
struct typereg {
size_t num;
const void **ptrs;
};
static STRMAP(struct typereg *) typemap;
void autodata_register_(const char *typename, const void *ptr)
{
struct typereg *t;
assert(ptr);
t = strmap_get(&typemap, typename);
if (!t) {
t = malloc(sizeof(struct typereg));
t->num = 0;
t->ptrs = NULL;
strmap_add(&typemap, typename, t);
}
t->ptrs = realloc(t->ptrs, (t->num + 1) * sizeof(*t->ptrs));
t->ptrs[t->num] = ptr;
t->num++;
}
void *autodata_get_(const char *typename, size_t *nump)
{
struct typereg *t = strmap_get(&typemap, typename);
if (!t) {
*nump = 0;
return NULL;
}
*nump = t->num;
return t->ptrs;
}