1 // Licensed under BSD-MIT: See LICENSE.
7 #if HAVE_SECTION_START_STOP
8 void *autodata_get_section(void *start, void *stop, size_t *nump)
10 *nump = (void **)(stop) - (void **)(start);
14 void autodata_free(void *table)
18 #include <ccan/ptr_valid/ptr_valid.h>
20 void *autodata_make_table(const void *example, const char *name, size_t *nump)
22 const char *start, *end, *tag;
23 struct ptr_valid_batch batch;
24 const void *const magic = (void *)AUTODATA_MAGIC;
28 if (!ptr_valid_batch_start(&batch))
31 /* Get range to search. */
32 for (start = (char *)((intptr_t)example & ~(getpagesize() - 1));
33 ptr_valid_batch(&batch, start-getpagesize(), 1, sizeof(void *),
35 start -= getpagesize());
37 for (end = (char *)((intptr_t)example & ~(getpagesize() - 1));
38 ptr_valid_batch(&batch, end, 1, sizeof(void *), false);
39 end += getpagesize());
42 first_magic = *(char *)&magic;
43 for (tag = memchr(start, first_magic, end - start);
45 tag = memchr(tag+1, first_magic, end - (tag + 1))) {
48 /* We can read 4 void *'s here? */
49 if (tag + sizeof(adata) > end)
52 memcpy(adata, tag, sizeof(adata));
55 if (adata[0] != (void *)AUTODATA_MAGIC || adata[1] != tag)
59 if (!ptr_valid_batch_string(&batch, adata[3])
60 || strcmp(name, adata[3]) != 0)
63 if (!ptr_valid_batch_read(&batch, (char *)adata[2]))
66 table = realloc(table, sizeof(void *) * (*nump + 1));
69 table[*nump] = adata[2];
72 ptr_valid_batch_end(&batch);
76 void autodata_free(void *table)