From 5a1302ab2e3250c158817bcb7880cb7a1ea856b4 Mon Sep 17 00:00:00 2001 From: Marcel Moolenaar Date: Sun, 25 Sep 2016 22:57:59 +0000 Subject: [PATCH] Replace the use of linker sets with constructors for both the formats and schemes. Formats and schemes are registered at runtime now, rather than collected at link time. --- usr.bin/mkimg/format.c | 24 +++++++++++++++++++----- usr.bin/mkimg/format.h | 13 ++++++++----- usr.bin/mkimg/mkimg.c | 21 ++++++++++----------- usr.bin/mkimg/scheme.c | 23 ++++++++++++++++++----- usr.bin/mkimg/scheme.h | 10 ++++++---- 5 files changed, 61 insertions(+), 30 deletions(-) diff --git a/usr.bin/mkimg/format.c b/usr.bin/mkimg/format.c index a21b08ad0e7e..010da7c11235 100644 --- a/usr.bin/mkimg/format.c +++ b/usr.bin/mkimg/format.c @@ -28,8 +28,6 @@ __FBSDID("$FreeBSD$"); #include -#include -#include #include #include #include @@ -42,8 +40,24 @@ __FBSDID("$FreeBSD$"); #include "format.h" #include "mkimg.h" +static struct mkimg_format *first; static struct mkimg_format *format; +struct mkimg_format * +format_iterate(struct mkimg_format *f) +{ + + return ((f == NULL) ? first : f->next); +} + +void +format_register(struct mkimg_format *f) +{ + + f->next = first; + first = f; +} + int format_resize(lba_t end) { @@ -56,10 +70,10 @@ format_resize(lba_t end) int format_select(const char *spec) { - struct mkimg_format *f, **iter; + struct mkimg_format *f; - SET_FOREACH(iter, formats) { - f = *iter; + f = NULL; + while ((f = format_iterate(f)) != NULL) { if (strcasecmp(spec, f->name) == 0) { format = f; return (0); diff --git a/usr.bin/mkimg/format.h b/usr.bin/mkimg/format.h index aa00e6e2e27b..e50fe3dc26cb 100644 --- a/usr.bin/mkimg/format.h +++ b/usr.bin/mkimg/format.h @@ -29,21 +29,24 @@ #ifndef _MKIMG_FORMAT_H_ #define _MKIMG_FORMAT_H_ -#include - struct mkimg_format { + struct mkimg_format *next; const char *name; const char *description; int (*resize)(lba_t); int (*write)(int); }; -SET_DECLARE(formats, struct mkimg_format); -#define FORMAT_DEFINE(nm) DATA_SET(formats, nm) +#define FORMAT_DEFINE(nm) \ +static void format_register_##nm(void) __attribute__((constructor)); \ +static void format_register_##nm(void) { format_register(&nm); } -int format_resize(lba_t); +struct mkimg_format *format_iterate(struct mkimg_format *); +void format_register(struct mkimg_format *); int format_select(const char *); struct mkimg_format *format_selected(void); + +int format_resize(lba_t); int format_write(int); #endif /* _MKIMG_FORMAT_H_ */ diff --git a/usr.bin/mkimg/mkimg.c b/usr.bin/mkimg/mkimg.c index 5d41e3bbd48b..3dfc10a3617d 100644 --- a/usr.bin/mkimg/mkimg.c +++ b/usr.bin/mkimg/mkimg.c @@ -27,7 +27,6 @@ #include __FBSDID("$FreeBSD$"); -#include #include #include #include @@ -77,20 +76,20 @@ u_int blksz = 0; static void print_formats(int usage) { - struct mkimg_format *f, **f_iter; + struct mkimg_format *f; const char *sep; if (usage) { fprintf(stderr, " formats:\n"); - SET_FOREACH(f_iter, formats) { - f = *f_iter; + f = NULL; + while ((f = format_iterate(f)) != NULL) { fprintf(stderr, "\t%s\t- %s\n", f->name, f->description); } } else { sep = ""; - SET_FOREACH(f_iter, formats) { - f = *f_iter; + f = NULL; + while ((f = format_iterate(f)) != NULL) { printf("%s%s", sep, f->name); sep = " "; } @@ -101,20 +100,20 @@ print_formats(int usage) static void print_schemes(int usage) { - struct mkimg_scheme *s, **s_iter; + struct mkimg_scheme *s; const char *sep; if (usage) { fprintf(stderr, " schemes:\n"); - SET_FOREACH(s_iter, schemes) { - s = *s_iter; + s = NULL; + while ((s = scheme_iterate(s)) != NULL) { fprintf(stderr, "\t%s\t- %s\n", s->name, s->description); } } else { sep = ""; - SET_FOREACH(s_iter, schemes) { - s = *s_iter; + s = NULL; + while ((s = scheme_iterate(s)) != NULL) { printf("%s%s", sep, s->name); sep = " "; } diff --git a/usr.bin/mkimg/scheme.c b/usr.bin/mkimg/scheme.c index 6cd332f2a946..34047e751bc5 100644 --- a/usr.bin/mkimg/scheme.c +++ b/usr.bin/mkimg/scheme.c @@ -28,8 +28,6 @@ __FBSDID("$FreeBSD$"); #include -#include -#include #include #include #include @@ -65,6 +63,7 @@ static struct { { NULL, ALIAS_NONE } /* Keep last! */ }; +static struct mkimg_scheme *first; static struct mkimg_scheme *scheme; static void *bootcode; @@ -82,13 +81,27 @@ scheme_parse_alias(const char *name) return (ALIAS_NONE); } +struct mkimg_scheme * +scheme_iterate(struct mkimg_scheme *s) +{ + + return ((s == NULL) ? first : s->next); +} + +void +scheme_register(struct mkimg_scheme *s) +{ + s->next = first; + first = s; +} + int scheme_select(const char *spec) { - struct mkimg_scheme *s, **iter; + struct mkimg_scheme *s; - SET_FOREACH(iter, schemes) { - s = *iter; + s = NULL; + while ((s = scheme_iterate(s)) != NULL) { if (strcasecmp(spec, s->name) == 0) { scheme = s; return (0); diff --git a/usr.bin/mkimg/scheme.h b/usr.bin/mkimg/scheme.h index 552d0313f32d..5826036622a1 100644 --- a/usr.bin/mkimg/scheme.h +++ b/usr.bin/mkimg/scheme.h @@ -29,8 +29,6 @@ #ifndef _MKIMG_SCHEME_H_ #define _MKIMG_SCHEME_H_ -#include - enum alias { ALIAS_NONE, /* Keep first! */ /* start */ @@ -62,6 +60,7 @@ struct mkimg_alias { }; struct mkimg_scheme { + struct mkimg_scheme *next; const char *name; const char *description; struct mkimg_alias *aliases; @@ -77,9 +76,12 @@ struct mkimg_scheme { u_int maxsecsz; }; -SET_DECLARE(schemes, struct mkimg_scheme); -#define SCHEME_DEFINE(nm) DATA_SET(schemes, nm) +#define SCHEME_DEFINE(nm) \ +static void scheme_register_##nm(void) __attribute__((constructor)); \ +static void scheme_register_##nm(void) { scheme_register(&nm); } +struct mkimg_scheme *scheme_iterate(struct mkimg_scheme *); +void scheme_register(struct mkimg_scheme *); int scheme_select(const char *); struct mkimg_scheme *scheme_selected(void);