Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[tables] Redefine methods for accessing linker tables
Intel's C compiler (icc) chokes on the zero-length arrays that we
currently use as part of the mechanism for accessing linker table
entries.  Abstract away the zero-length arrays, to make a port to icc
easier.

Introduce macros such as for_each_table_entry() to simplify the common
case of iterating over all entries in a linker table.

Represent table names as #defined string constants rather than
unquoted literals; this avoids visual confusion between table names
and C variable or type names, and also allows us to force a
compilation error in the event of incorrect table names.
  • Loading branch information
Michael Brown committed Mar 13, 2009
1 parent 3ed468e commit 1266d79
Show file tree
Hide file tree
Showing 48 changed files with 239 additions and 264 deletions.
11 changes: 2 additions & 9 deletions src/core/console.c
Expand Up @@ -5,11 +5,6 @@

/** @file */

static struct console_driver console_drivers[0]
__table_start ( struct console_driver, console );
static struct console_driver console_drivers_end[0]
__table_end ( struct console_driver, console );

/**
* Write a single character to each console device.
*
Expand All @@ -28,8 +23,7 @@ void putchar ( int character ) {
if ( character == '\n' )
putchar ( '\r' );

for ( console = console_drivers; console < console_drivers_end ;
console++ ) {
for_each_table_entry ( console, CONSOLES ) {
if ( ( ! console->disabled ) && console->putchar )
console->putchar ( character );
}
Expand All @@ -51,8 +45,7 @@ void putchar ( int character ) {
static struct console_driver * has_input ( void ) {
struct console_driver *console;

for ( console = console_drivers; console < console_drivers_end ;
console++ ) {
for_each_table_entry ( console, CONSOLES ) {
if ( ( ! console->disabled ) && console->iskey ) {
if ( console->iskey () )
return console;
Expand Down
7 changes: 1 addition & 6 deletions src/core/device.c
Expand Up @@ -29,11 +29,6 @@
*
*/

static struct root_device root_devices[0]
__table_start ( struct root_device, root_devices );
static struct root_device root_devices_end[0]
__table_end ( struct root_device, root_devices );

/** Registered root devices */
static LIST_HEAD ( devices );

Expand Down Expand Up @@ -77,7 +72,7 @@ static void probe_devices ( void ) {
struct root_device *rootdev;
int rc;

for ( rootdev = root_devices; rootdev < root_devices_end; rootdev++ ) {
for_each_table_entry ( rootdev, ROOT_DEVICES ) {
list_add ( &rootdev->dev.siblings, &devices );
INIT_LIST_HEAD ( &rootdev->dev.children );
if ( ( rc = rootdev_probe ( rootdev ) ) != 0 )
Expand Down
7 changes: 1 addition & 6 deletions src/core/exec.c
Expand Up @@ -34,11 +34,6 @@
*
*/

static struct command commands[0]
__table_start ( struct command, commands );
static struct command commands_end[0]
__table_end ( struct command, commands );

/* Avoid dragging in getopt.o unless a command really uses it */
int optind;
int nextchar;
Expand Down Expand Up @@ -78,7 +73,7 @@ int execv ( const char *command, char * const argv[] ) {
reset_getopt();

/* Hand off to command implementation */
for ( cmd = commands ; cmd < commands_end ; cmd++ ) {
for_each_table_entry ( cmd, COMMANDS ) {
if ( strcmp ( command, cmd->name ) == 0 )
return cmd->exec ( argc, ( char ** ) argv );
}
Expand Down
7 changes: 2 additions & 5 deletions src/core/gdbstub.c
Expand Up @@ -54,10 +54,6 @@ struct gdbstub {
int len; /* length of payload */
};

/* Transports */
static struct gdb_transport gdb_transport_start[0] __table_start ( struct gdb_transport, gdb_transports );
static struct gdb_transport gdb_transport_end[0] __table_end ( struct gdb_transport, gdb_transports );

/* Packet parser states */
static void gdbstub_state_new ( struct gdbstub *stub, char ch );
static void gdbstub_state_data ( struct gdbstub *stub, char ch );
Expand Down Expand Up @@ -387,7 +383,8 @@ void gdbstub_handler ( int signo, gdbreg_t *regs ) {

struct gdb_transport *find_gdb_transport ( const char *name ) {
struct gdb_transport *trans;
for ( trans = gdb_transport_start; trans < gdb_transport_end; trans++ ) {

for_each_table_entry ( trans, GDB_TRANSPORTS ) {
if ( strcmp ( trans->name, name ) == 0 ) {
return trans;
}
Expand Down
8 changes: 1 addition & 7 deletions src/core/image.c
Expand Up @@ -37,12 +37,6 @@
/** List of registered images */
struct list_head images = LIST_HEAD_INIT ( images );

/** List of image types */
static struct image_type image_types[0]
__table_start ( struct image_type, image_types );
static struct image_type image_types_end[0]
__table_end ( struct image_type, image_types );

/**
* Free executable/loadable image
*
Expand Down Expand Up @@ -219,7 +213,7 @@ int image_autoload ( struct image *image ) {
return image_load ( image );

/* Otherwise probe for a suitable type */
for ( type = image_types ; type < image_types_end ; type++ ) {
for_each_table_entry ( type, IMAGE_TYPES ) {
DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
rc = image_load_type ( image, type );
if ( image->type == NULL )
Expand Down
23 changes: 4 additions & 19 deletions src/core/init.c
Expand Up @@ -25,18 +25,6 @@
*
*/

/** Registered initialisation functions */
static struct init_fn init_fns[0]
__table_start ( struct init_fn, init_fns );
static struct init_fn init_fns_end[0]
__table_end ( struct init_fn, init_fns );

/** Registered startup/shutdown functions */
static struct startup_fn startup_fns[0]
__table_start ( struct startup_fn, startup_fns );
static struct startup_fn startup_fns_end[0]
__table_end ( struct startup_fn, startup_fns );

/** "startup() has been called" flag */
static int started = 0;

Expand All @@ -54,9 +42,8 @@ void initialise ( void ) {
struct init_fn *init_fn;

/* Call registered initialisation functions */
for ( init_fn = init_fns ; init_fn < init_fns_end ; init_fn++ ) {
for_each_table_entry ( init_fn, INIT_FNS )
init_fn->initialise ();
}
}

/**
Expand All @@ -73,8 +60,7 @@ void startup ( void ) {
return;

/* Call registered startup functions */
for ( startup_fn = startup_fns ; startup_fn < startup_fns_end ;
startup_fn++ ) {
for_each_table_entry ( startup_fn, STARTUP_FNS ) {
if ( startup_fn->startup )
startup_fn->startup();
}
Expand All @@ -90,7 +76,7 @@ void startup ( void ) {
* This function reverses the actions of startup(), and leaves gPXE in
* a state ready to be removed from memory. You may call startup()
* again after calling shutdown().
*
* Call this function only once, before either exiting main() or
* starting up a non-returnable image.
*/
Expand All @@ -101,8 +87,7 @@ void shutdown ( int flags ) {
return;

/* Call registered shutdown functions (in reverse order) */
for ( startup_fn = startup_fns_end - 1 ; startup_fn >= startup_fns ;
startup_fn-- ) {
for_each_table_entry_reverse ( startup_fn, STARTUP_FNS ) {
if ( startup_fn->shutdown )
startup_fn->shutdown ( flags );
}
Expand Down
5 changes: 1 addition & 4 deletions src/core/main.c
Expand Up @@ -27,9 +27,6 @@ Literature dealing with the network protocols:
#define BOLD "\033[1m"
#define CYAN "\033[36m"

static struct feature features[0] __table_start ( struct feature, features );
static struct feature features_end[0] __table_end ( struct feature, features );

/**
* Main entry point
*
Expand Down Expand Up @@ -61,7 +58,7 @@ __asmcall int main ( void ) {
NORMAL " -- Open Source Boot Firmware -- "
CYAN "http://etherboot.org" NORMAL "\n"
"Features:" );
for ( feature = features ; feature < features_end ; feature++ )
for_each_table_entry ( feature, FEATURES )
printf ( " %s", feature->name );
printf ( "\n" );

Expand Down
16 changes: 2 additions & 14 deletions src/core/open.c
Expand Up @@ -30,18 +30,6 @@
*
*/

/** Registered URI openers */
static struct uri_opener uri_openers[0]
__table_start ( struct uri_opener, uri_openers );
static struct uri_opener uri_openers_end[0]
__table_end ( struct uri_opener, uri_openers );

/** Registered socket openers */
static struct socket_opener socket_openers[0]
__table_start ( struct socket_opener, socket_openers );
static struct socket_opener socket_openers_end[0]
__table_end ( struct socket_opener, socket_openers );

/**
* Open URI
*
Expand All @@ -63,7 +51,7 @@ int xfer_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
return -ENOMEM;

/* Find opener which supports this URI scheme */
for ( opener = uri_openers ; opener < uri_openers_end ; opener++ ) {
for_each_table_entry ( opener, URI_OPENERS ) {
if ( strcmp ( resolved_uri->scheme, opener->scheme ) == 0 ) {
rc = opener->open ( xfer, resolved_uri );
goto done;
Expand Down Expand Up @@ -121,7 +109,7 @@ int xfer_open_socket ( struct xfer_interface *xfer, int semantics,
socket_semantics_name ( semantics ),
socket_family_name ( peer->sa_family ) );

for ( opener = socket_openers; opener < socket_openers_end; opener++ ){
for_each_table_entry ( opener, SOCKET_OPENERS ) {
if ( ( opener->semantics == semantics ) &&
( opener->family == peer->sa_family ) ) {
return opener->open ( xfer, peer, local );
Expand Down
9 changes: 1 addition & 8 deletions src/core/process.c
Expand Up @@ -31,12 +31,6 @@
/** Process run queue */
static LIST_HEAD ( run_queue );

/** Registered permanent processes */
static struct process processes[0]
__table_start ( struct process, processes );
static struct process processes_end[0]
__table_end ( struct process, processes );

/**
* Add process to process list
*
Expand Down Expand Up @@ -93,9 +87,8 @@ void step ( void ) {
static void init_processes ( void ) {
struct process *process;

for ( process = processes ; process < processes_end ; process++ ) {
for_each_table_entry ( process, PERMANENT_PROCESSES )
process_add ( process );
}
}

/** Process initialiser */
Expand Down
10 changes: 2 additions & 8 deletions src/core/resolv.c
Expand Up @@ -150,12 +150,6 @@ struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
***************************************************************************
*/

/** Registered name resolvers */
static struct resolver resolvers[0]
__table_start ( struct resolver, resolvers );
static struct resolver resolvers_end[0]
__table_end ( struct resolver, resolvers );

/** A name resolution multiplexer */
struct resolv_mux {
/** Reference counter */
Expand Down Expand Up @@ -223,7 +217,7 @@ static void resolv_mux_done ( struct resolv_interface *resolv,

/* Attempt next child resolver, if possible */
mux->resolver++;
if ( mux->resolver >= resolvers_end ) {
if ( mux->resolver >= table_end ( struct resolver, RESOLVERS ) ) {
DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
goto finished;
}
Expand Down Expand Up @@ -262,7 +256,7 @@ int resolv ( struct resolv_interface *resolv, const char *name,
return -ENOMEM;
resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
mux->resolver = resolvers;
mux->resolver = table_start ( struct resolver, RESOLVERS );
memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
memcpy ( mux->name, name, name_len );

Expand Down
25 changes: 3 additions & 22 deletions src/core/settings.c
Expand Up @@ -37,24 +37,6 @@
*
*/

/** Registered settings */
static struct setting settings[0]
__table_start ( struct setting, settings );
static struct setting settings_end[0]
__table_end ( struct setting, settings );

/** Registered setting types */
static struct setting_type setting_types[0]
__table_start ( struct setting_type, setting_types );
static struct setting_type setting_types_end[0]
__table_end ( struct setting_type, setting_types );

/** Registered settings applicators */
static struct settings_applicator settings_applicators[0]
__table_start ( struct settings_applicator, settings_applicators );
static struct settings_applicator settings_applicators_end[0]
__table_end ( struct settings_applicator, settings_applicators );

/******************************************************************************
*
* Registered settings blocks
Expand Down Expand Up @@ -229,8 +211,7 @@ static int apply_settings ( void ) {
int rc;

/* Call all settings applicators */
for ( applicator = settings_applicators ;
applicator < settings_applicators_end ; applicator++ ) {
for_each_table_entry ( applicator, SETTINGS_APPLICATORS ) {
if ( ( rc = applicator->apply() ) != 0 ) {
DBG ( "Could not apply settings using applicator "
"%p: %s\n", applicator, strerror ( rc ) );
Expand Down Expand Up @@ -670,7 +651,7 @@ int storef_setting ( struct settings *settings, struct setting *setting,
static struct setting * find_setting ( const char *name ) {
struct setting *setting;

for ( setting = settings ; setting < settings_end ; setting++ ) {
for_each_table_entry ( setting, SETTINGS ) {
if ( strcmp ( name, setting->name ) == 0 )
return setting;
}
Expand All @@ -686,7 +667,7 @@ static struct setting * find_setting ( const char *name ) {
static struct setting_type * find_setting_type ( const char *name ) {
struct setting_type *type;

for ( type = setting_types ; type < setting_types_end ; type++ ) {
for_each_table_entry ( type, SETTING_TYPES ) {
if ( strcmp ( name, type->name ) == 0 )
return type;
}
Expand Down
7 changes: 1 addition & 6 deletions src/drivers/bus/eisa.c
Expand Up @@ -7,11 +7,6 @@
#include <unistd.h>
#include <gpxe/eisa.h>

static struct eisa_driver eisa_drivers[0]
__table_start ( struct eisa_driver, eisa_drivers );
static struct eisa_driver eisa_drivers_end[0]
__table_end ( struct eisa_driver, eisa_drivers );

static void eisabus_remove ( struct root_device *rootdev );

/**
Expand Down Expand Up @@ -57,7 +52,7 @@ static int eisa_probe ( struct eisa_device *eisa ) {
eisa->slot, eisa->vendor_id, eisa->prod_id,
isa_id_string ( eisa->vendor_id, eisa->prod_id ), eisa->ioaddr );

for ( driver = eisa_drivers; driver < eisa_drivers_end; driver++ ) {
for_each_table_entry ( driver, EISA_DRIVERS ) {
for ( i = 0 ; i < driver->id_count ; i++ ) {
id = &driver->ids[i];
if ( id->vendor_id != eisa->vendor_id )
Expand Down
7 changes: 1 addition & 6 deletions src/drivers/bus/isa.c
Expand Up @@ -48,11 +48,6 @@ static isa_probe_addr_t isa_extra_probe_addrs[] = {
isa_extra_probe_addrs[ (ioidx) + ISA_EXTRA_PROBE_ADDR_COUNT ] : \
(driver)->probe_addrs[(ioidx)] )

static struct isa_driver isa_drivers[0]
__table_start ( struct isa_driver, isa_drivers );
static struct isa_driver isa_drivers_end[0]
__table_end ( struct isa_driver, isa_drivers );

static void isabus_remove ( struct root_device *rootdev );

/**
Expand Down Expand Up @@ -100,7 +95,7 @@ static int isabus_probe ( struct root_device *rootdev ) {
int ioidx;
int rc;

for ( driver = isa_drivers ; driver < isa_drivers_end ; driver++ ) {
for_each_table_entry ( driver, ISA_DRIVERS ) {
for ( ioidx = ISA_IOIDX_MIN ( driver ) ;
ioidx <= ISA_IOIDX_MAX ( driver ) ; ioidx++ ) {
/* Allocate struct isa_device */
Expand Down

0 comments on commit 1266d79

Please sign in to comment.