mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
b594c53771
This is a GCC extension, but avoids much other messiness. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
41 lines
746 B
C
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;
|
|
}
|