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.
This commit is contained in:
Marcel Moolenaar 2016-09-25 22:57:59 +00:00
parent 5b06614ce3
commit 5a1302ab2e
5 changed files with 61 additions and 30 deletions

View File

@ -28,8 +28,6 @@
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/linker_set.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
@ -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);

View File

@ -29,21 +29,24 @@
#ifndef _MKIMG_FORMAT_H_
#define _MKIMG_FORMAT_H_
#include <sys/linker_set.h>
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_ */

View File

@ -27,7 +27,6 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/linker_set.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -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 = " ";
}

View File

@ -28,8 +28,6 @@
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/linker_set.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
@ -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);

View File

@ -29,8 +29,6 @@
#ifndef _MKIMG_SCHEME_H_
#define _MKIMG_SCHEME_H_
#include <sys/linker_set.h>
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);