Back: A Module Loading Subsystem
Forward: Managing Module Loader Errors
 
FastBack: A Complex GNU Autotools Project
Up: A Module Loading Subsystem
FastForward: A Loadable Module
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

20.1.1 Initialising the Module Loader

Before using this code (or any other libltdl based module loader for that matter), a certain amount of initialisation is required:

  • libltdl itself requires initialisation.

    1. libltdl should be told to use the same memory allocation routines used by the rest of Sic.

    2. Any preloaded modules (see section 18.4 dlpreopen Loading) need to be initialised with LTDL_SET_PRELOADED_SYMBOLS().

    3. ltdl_init() must be called.

  • The module search path needs to be set. Here I allow the installer to specify a default search path to correspond with the installed Sic modules at compile time, but search the directories in the runtime environment variable `SIC_MODULES_PATH' first.

  • The internal error handling needs to be initialised.

Here is the start of the module loader, `sic/module.c', including the initialisation code for libltdl:

 
#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include "common.h"
#include "builtin.h"
#include "eval.h"
#include "ltdl.h"
#include "module.h"
#include "sic.h"

#ifndef SIC_MODULE_PATH_ENV
#  define SIC_MODULE_PATH_ENV   "SIC_MODULE_PATH"
#endif

int
module_init (void)
{
  static int initialised = 0;
  int errors = 0;

  /* Only perform the initialisation once. */
  if (!initialised)
    {
      /* ltdl should use the same mallocation as us. */
      lt_dlmalloc = (lt_ptr_t (*) (size_t)) xmalloc;
      lt_dlfree = (void (*) (lt_ptr_t)) free;

      /* Make sure preloaded modules are initialised. */
      LTDL_SET_PRELOADED_SYMBOLS();

      last_error = NULL;

      /* Call ltdl initialisation function. */
      errors = lt_dlinit();


      /* Set up the module search directories. */
      if (errors == 0)
        {
          const char *path = getenv (SIC_MODULE_PATH_ENV);

          if (path != NULL)
            errors = lt_dladdsearchdir(path);
        }

      if (errors == 0)
        errors = lt_dladdsearchdir(MODULE_PATH);

      if (errors != 0)
        last_error = lt_dlerror ();

      ++initialised;

      return errors ? SIC_ERROR : SIC_OKAY;
    }

  last_error = multi_init_error;
  return SIC_ERROR;
}


This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html