libamxo  4.3.4
Object Definition Language (ODL) parsing
Import Resolver API
Collaboration diagram for Import Resolver API:

Functions

int amxo_resolver_import_open (amxo_parser_t *parser, const char *so_name, const char *alias, int flags)
 Opens a shared object file (.so file) More...
 
void amxo_resolver_import_close_all (void)
 Unloads all loaded shared objects. More...
 

Detailed Description

Function Documentation

◆ amxo_resolver_import_close_all()

void amxo_resolver_import_close_all ( void  )

Unloads all loaded shared objects.

Removes all loaded shared objects from memory, regardless whether they are referenced. This function is typically called just before exiting the application,

Warning
Call this method before exiting the application. Do not call this method when resolved functions are still being used.

Definition at line 451 of file amxo_import_resolver.c.

451  {
452  amxc_htable_clean(&import_libs, amxo_import_lib_free);
453  amxc_htable_init(&import_libs, 10);
454 }
static amxc_htable_t import_libs
static void amxo_import_lib_free(UNUSED const char *key, amxc_htable_it_t *it)

◆ amxo_resolver_import_open()

int amxo_resolver_import_open ( amxo_parser_t parser,
const char *  so_name,
const char *  alias,
int  flags 
)

Opens a shared object file (.so file)

The import function resolver can load shared objects. During parsing of the odl file all "import" mentions will call this method to open and load the shared object.

Using this function it is possible to load shared objects prior to start parsing an odl file.

After parsing the referenced shared objects are kept in memory. When they are not needed anymore they can be unloaded by using amxo_resolver_import_close_all

Note
The alias must be unique, if another shared object is loaded with same alias the loading will fail.
Warning
After parsing of an odl file, all shared objects not referenced are unloaded. A shared object is considered referenced when at least one function is resolved from that shared object.
Parameters
parserthe odl parser instance
so_namefile name of the shared object (can include relative or absolute path).
aliasalias for the shared object, makes it easier to use in odl files
flagsdlopen flags
Returns

Returns 0 when the function is removed from the function table.

Definition at line 354 of file amxo_import_resolver.c.

357  {
358  int retval = -1;
359  void* handle = NULL;
360  amxo_import_lib_t* import_lib = NULL;
361  char* full_path = NULL;
362  bool silent = amxc_var_constcast(bool, GET_OPTION(parser, "silent"));
363  const amxc_llist_t* impdirs =
364  amxc_var_constcast(amxc_llist_t, GET_OPTION(parser, "import-dirs"));
365  amxc_htable_t* import_data = amxo_parser_claim_resolver_data(parser, "import");
366  amxc_string_t res_so_name;
367  amxc_string_t res_alias;
368  amxc_string_init(&res_so_name, 0);
369  amxc_string_init(&res_alias, 0);
370 
371  dbg = amxc_var_constcast(bool, GET_OPTION(parser, "import-dbg"));
372 
373  when_null(parser, exit);
374  parser->status = amxd_status_invalid_arg;
375  when_str_empty(so_name, exit);
376  when_true(alias != NULL && alias[0] == 0, exit);
377 
378  parser->status = amxd_status_ok;
379  when_true_status(amxo_parser_no_import(parser), exit, retval = 0);
380 
381  if(amxc_string_set_resolved(&res_alias, alias, &parser->config) > 0) {
382  alias = amxc_string_get(&res_alias, 0);
383  }
384  if(amxc_string_set_resolved(&res_so_name, so_name, &parser->config) > 0) {
385  so_name = amxc_string_get(&res_so_name, 0);
386  }
387 
388  when_true_status(amxo_resolver_import_alias_exists(import_data, alias),
389  exit,
390  retval = 0);
391 
392  if(!amxo_parser_find(parser, impdirs, so_name, &full_path)) {
393  if(dbg && !silent) {
394  fprintf(stderr, "[IMPORT-DBG] - file not found %s\n", so_name);
395  }
396  parser->status = amxd_status_file_not_found;
397  amxo_parser_msg(parser, "Import file not found !!! \"%s\"", so_name);
398  goto exit;
399  }
400 
401  handle = amxo_resolver_import_lib(parser, so_name, full_path, (flags & ~RTLD_NODELETE));
402  when_null(handle, exit);
403 
404  import_lib = (amxo_import_lib_t*) calloc(1, sizeof(amxo_import_lib_t));
405  when_true_status(import_lib == NULL, exit, parser->status = amxd_status_out_of_mem);
406 
407  import_lib->handle = handle;
408  if((flags & RTLD_NODELETE) == RTLD_NODELETE) {
409  import_lib->references++;
410  }
411  amxc_htable_insert(import_data, alias, &import_lib->hit);
412  retval = 0;
413 
414 exit:
415  amxc_string_clean(&res_alias);
416  amxc_string_clean(&res_so_name);
417  free(full_path);
418  if((retval != 0) && (handle != NULL)) {
419  dlclose(handle);
420  }
421  return retval;
422 }
static bool dbg
static bool amxo_parser_no_import(amxo_parser_t *parser)
static void * amxo_resolver_import_lib(amxo_parser_t *parser, const char *so_name, const char *full_path, int flags)
#define GET_OPTION(parser, name)
static bool amxo_resolver_import_alias_exists(amxc_htable_t *import_data, const char *alias)
PRIVATE void PRIVATE int PRIVATE bool amxo_parser_find(amxo_parser_t *parser, const amxc_llist_t *dirs, const char *file_path, char **full_path)
PRIVATE void amxo_parser_msg(amxo_parser_t *parser, const char *format,...) __attribute__((format(printf
amxc_htable_t * amxo_parser_claim_resolver_data(amxo_parser_t *parser, const char *resolver_name)
Fetches resolver specific data for a parser instance.
amxc_htable_it_t hit
amxc_var_t config
Definition: amxo_types.h:250
amxd_status_t status
Definition: amxo_types.h:258