1 // Licensed under BSD-MIT: See LICENSE.
2 #ifndef CCAN_AUTODATA_H
3 #define CCAN_AUTODATA_H
5 #include <ccan/compiler/compiler.h>
8 #if HAVE_SECTION_START_STOP
11 * AUTODATA_TYPE - declare the type for a given autodata name.
12 * @name: the name for this set of autodata
13 * @type: the type this autodata points to
15 * This macro is usually placed in a header: it must preceed any
16 * autodata functions in the file.
19 * // My set of char pointers.
20 * AUTODATA_TYPE(names, char);
22 #define AUTODATA_TYPE(name, type) \
23 typedef type autodata_##name##_; \
24 extern type *__start_autodata_##name[], *__stop_autodata_##name[]
27 * AUTODATA - add a pointer to this autodata set
28 * @name: the name of the set of autodata
29 * @ptr: the compile-time-known pointer
31 * This embeds @ptr into the binary, with the tag corresponding to
32 * @name (which must look like a valid identifier, no punctuation!).
33 * The type of @ptr must match that given by AUTODATA_TYPE. It is
34 * usually a file-level declaration.
37 * // Put two char pointers into the names AUTODATA set.
38 * AUTODATA(names, "Arabella");
39 * AUTODATA(names, "Alex");
41 #define AUTODATA(name, ptr) \
42 static const autodata_##name##_ *NEEDED \
43 __attribute__((section("autodata_" #name))) \
44 AUTODATA_VAR_(name, __LINE__) = (ptr);
47 * autodata_get - get an autodata set
48 * @name: the name of the set of autodata
49 * @nump: the number of items in the set.
51 * This extract the embedded pointers matching @name. It may fail
52 * if malloc() fails, or if there is no AUTODATA at all.
54 * The return will be a pointer to an array of @type pointers (from
58 * static void print_embedded_names(void)
60 * unsigned int i, num;
61 * char **n = autodata_get(names, &num);
63 * for (i = 0; i < num; i++)
64 * printf("%s\n", n[i]);
67 #define autodata_get(name, nump) \
68 ((autodata_##name##_ **) \
69 autodata_get_section(__start_autodata_##name, \
70 __stop_autodata_##name, (nump)))
71 #endif /* HAVE_SECTION_START_STOP */
74 * autodata_free - free the table returned by autodata_get()
77 void autodata_free(void *p);
79 /* Internal functions. */
80 #define AUTODATA_VAR__(name, line) autodata_##name##_##line
81 #define AUTODATA_VAR_(name, line) AUTODATA_VAR__(name, line)
83 #if HAVE_SECTION_START_STOP
84 void *autodata_get_section(void *start, void *stop, size_t *nump);
86 #define AUTODATA_TYPE(name, type) \
87 typedef type autodata_##name##_; \
88 static const void *autodata_##name##_ex = &autodata_##name##_ex
90 #define AUTODATA_MAGIC ((long)0xFEEDA10DA7AF00D5ULL)
91 #define AUTODATA(name, ptr) \
92 static const autodata_##name##_ *NEEDED \
93 AUTODATA_VAR_(name, __LINE__)[4] = \
94 { (void *)AUTODATA_MAGIC, \
95 (void *)&AUTODATA_VAR_(name, __LINE__), \
99 #define autodata_get(name, nump) \
100 ((autodata_##name##_ **) \
101 autodata_make_table(&autodata_##name##_ex, #name, (nump)))
103 void *autodata_make_table(const void *example, const char *name, size_t *nump);
106 #endif /* CCAN_AUTODATA_H */