Skip to content

Commit

Permalink
#37 move bake-specific code to os_api
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Aug 25, 2019
1 parent 6269c82 commit e09f59b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 13 deletions.
39 changes: 39 additions & 0 deletions include/flecs/util/os_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ extern "C" {
typedef uintptr_t ecs_os_thread_t;
typedef uintptr_t ecs_os_cond_t;
typedef uintptr_t ecs_os_mutex_t;
typedef uintptr_t ecs_os_dl_t;

/* Generic function pointer type */
typedef void (*ecs_os_proc_t)(void);

/* Memory management */
typedef
Expand Down Expand Up @@ -113,6 +117,24 @@ typedef
void (*ecs_os_api_abort_t)(
void);

/* Dynamic libraries */
typedef
ecs_os_dl_t (*ecs_os_api_dlopen_t)(
const char *libname);

typedef
ecs_os_proc_t (*ecs_os_api_dlproc_t)(
ecs_os_dl_t lib,
const char *procname);

typedef
void (*ecs_os_api_dlclose_t)(
ecs_os_dl_t lib);

typedef
char* (*ecs_os_api_module_to_dl_t)(
const char *module_id);

typedef struct ecs_os_api_t {
/* Memory management */
ecs_os_api_malloc_t malloc;
Expand Down Expand Up @@ -149,6 +171,15 @@ typedef struct ecs_os_api_t {

/* Application termination */
ecs_os_api_abort_t abort;

/* Dynamic library loading */
ecs_os_api_dlopen_t dlopen;
ecs_os_api_dlproc_t dlproc;
ecs_os_api_dlclose_t dlclose;

/* Overridable function that translates from a logical module id to the
* a shared library filename */
ecs_os_api_module_to_dl_t module_to_dl;
} ecs_os_api_t;

FLECS_EXPORT
Expand Down Expand Up @@ -216,6 +247,14 @@ bool ecs_os_dbg_enabled(void);
/* Application termination */
#define ecs_os_abort() ecs_os_api.abort()

/* Dynamic libraries */
#define ecs_os_dlopen(libname) ecs_os_api.dlopen(libname)
#define ecs_os_dlproc(lib, procname) ecs_os_api.dlproc(lib, procname)
#define ecs_os_dlclose(lib) ecs_os_api.dlclose(lib)

/* Module id translation */
#define ecs_os_module_to_dl(lib) ecs_os_api.module_to_dl(lib)

/* Sleep with floating point time */
FLECS_EXPORT
void ecs_sleepf(
Expand Down
52 changes: 50 additions & 2 deletions src/os_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void ecs_os_set_api(
}
}

/* When flecs is built with bake, use threading functions from bake.util */
/* When flecs is built with bake, use functions from bake.util */
#ifdef __BAKE__

static
Expand Down Expand Up @@ -103,6 +103,47 @@ void bake_log(
ecs_os_log("%s", msg);
}
}

static
ecs_os_dl_t bake_dlopen(
const char *dlname)
{
return (ecs_os_dl_t)ut_dl_open(dlname);
}

static
void bake_dlclose(
ecs_os_dl_t dl)
{
ut_dl_close((ut_dl)dl);
}

static
ecs_os_proc_t bake_dlproc(
ecs_os_dl_t dl,
const char *procname)
{
ecs_os_proc_t result = (ecs_os_proc_t)ut_dl_proc((ut_dl)dl, procname);
if (!result) {
ut_raise();
}
return result;
}

static
char* bake_module_to_dl(
const char *module_id)
{
const char *result = ut_locate(module_id, NULL, UT_LOCATE_LIB);
if (result) {
return ut_strdup(result);
} else {
return NULL;
}
}


/* __BAKE__ */
#endif

static
Expand Down Expand Up @@ -219,9 +260,16 @@ void ecs_os_set_api_defaults(void)
_ecs_os_api->cond_broadcast = bake_cond_broadcast;
_ecs_os_api->cond_wait = bake_cond_wait;

ut_log_handlerRegister(bake_log, NULL);
_ecs_os_api->dlopen = bake_dlopen;
_ecs_os_api->dlproc = bake_dlproc;
_ecs_os_api->dlclose = bake_dlclose;

_ecs_os_api->module_to_dl = bake_module_to_dl;

ut_log_handlerRegister(bake_log, NULL);
/* __BAKE__ */
#endif

_ecs_os_api->sleep = ecs_os_time_sleep;
_ecs_os_api->get_time = ecs_os_gettime;

Expand Down
49 changes: 38 additions & 11 deletions src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -1395,10 +1395,20 @@ ecs_entity_t ecs_import_from_library(
const char *module_name,
int flags)
{
#ifdef __BAKE__
char *import_func = (char*)module_name; /* safe */
char *module = (char*)module_name;

if (!ecs_os_api.module_to_dl ||
!ecs_os_api.dlopen ||
!ecs_os_api.dlproc ||
!ecs_os_api.dlclose)
{
ecs_os_err(
"library loading not supported, set module_to_dl, dlopen, dlclose "
"and dlproc os API callbacks first");
return ECS_INVALID_ENTITY;
}

/* If no module name is specified, try default naming convention for loading
* the main module from the library */
if (!import_func) {
Expand Down Expand Up @@ -1427,16 +1437,36 @@ ecs_entity_t ecs_import_from_library(
strcat(bptr, "Import");
}

/* Find civetweb module & entry point */
ecs_module_init_action_t action = (ecs_module_init_action_t)ut_load_proc(
library_name, NULL, import_func);
char *library_filename = ecs_os_module_to_dl(library_name);
if (!library_filename) {
ecs_os_err("failed to find library file for '%s'", library_name);
return ECS_INVALID_ENTITY;
} else {
ecs_os_dbg("found file '%s' for library '%s'",
library_filename, library_name);
}

ecs_os_dl_t dl = ecs_os_dlopen(library_filename);
if (!dl) {
ecs_os_err("failed to load library '%s' ('%s')",
library_name, library_filename);
free(library_filename);
return ECS_INVALID_ENTITY;
} else {
ecs_os_dbg("library '%s' ('%s') loaded",
library_name, library_filename);
}

ecs_module_init_action_t action = (ecs_module_init_action_t)
ecs_os_dlproc(dl, import_func);
if (!action) {
ecs_os_err("failed to load import function %s from library %s",
import_func, library_name);
ut_raise();
free(library_filename);
ecs_os_dlclose(dl);
return ECS_INVALID_ENTITY;
} else {
ecs_os_dbg("found import function %s in library %s for module %s",
ecs_os_dbg("found import function '%s' in library '%s' for module '%s'",
import_func, library_name, module);
}

Expand All @@ -1451,12 +1481,9 @@ ecs_entity_t ecs_import_from_library(
free(module);
}

free(library_filename);

return result;
#else
ecs_os_err(
"sorry, loading libraries is only possible if flecs is built with bake :(");
return ECS_INVALID_ENTITY;
#endif
}

uint16_t ecs_get_thread_index(
Expand Down

1 comment on commit e09f59b

@ArnCarveris
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing following functions:

ecs_os_init()

flecs/src/world.c

Lines 583 to 588 in e09f59b

#ifdef __BAKE__
ut_init(NULL);
if (ut_load_init(NULL, NULL, NULL, NULL)) {
ecs_os_err("warning: failed to initialize package loader");
}
#endif

ecs_os_deinit()

flecs/src/world.c

Lines 842 to 844 in e09f59b

#ifdef __BAKE__
ut_deinit();
#endif

Please sign in to comment.