#include "mlp_c.h" #include "dlfcn.h" mlp_c_library::mlp_c_library(mlp_context *ctx, mlp_runtime *rt, const char *name, const void *opts) : mlp_library(ctx, rt, name, opts), cil(NULL) { ctx->clear_error() ; char *buf = NULL ; if (strcmp(this->name.c_str(), "") != 0){ buf = (char *)malloc((strlen(name) + 32) * sizeof(char)) ; sprintf(buf, "lib%s.so", name) ; } CInvContext *cic = (CInvContext *)ctx->get_runtime_data(rt) ; cil = cinv_library_create(cic, buf) ; if (cil == NULL){ ctx->set_error(MLP_LIBRARY_ERROR, "Can't load shared object %s: %s\n", (buf != NULL ? buf : "(MAIN PROGRAM)"), cinv_context_geterrormsg(cic)) ; free(buf) ; return ; } mlp_debug(2, "C library %s loaded", buf) ; free(buf) ; } mlp_c_library::~mlp_c_library(){ } mlp_function *mlp_c_library::get_function(mlp_context *ctx, mlp_type rtype, const char *name, mlp_prototype proto){ return new mlp_c_function(ctx, rt, this, rtype, name, proto) ; } void *mlp_c_library::_load_entrypoint(mlp_context *ctx, const char *name){ ctx->clear_error() ; CInvContext *cic = (CInvContext *)ctx->get_runtime_data(rt) ; void *fptr = cinv_library_load_entrypoint(cic, cil, name) ; if (fptr == NULL){ ctx->set_error(MLP_FUNCTION_ERROR, "Can't locate entrypoint '%s' in library '%s': %s\n", name, this->name.c_str(), cinv_context_geterrormsg(cic)) ; return NULL ; } return fptr ; } void mlp_c_library::destroy(mlp_context *ctx){ ctx->clear_error() ; if (cil != NULL){ CInvContext *cic = (CInvContext *)ctx->get_runtime_data(rt) ; cinv_status_t st = cinv_library_delete(cic, cil) ; if (st == CINV_ERROR){ ctx->set_error(MLP_LIBRARY_ERROR, "Can't unload shared object %s: %s\n", (strcmp(name.c_str(), "") != 0 ? name.c_str() : "(MAIN PROGRAM)"), cinv_context_geterrormsg(cic)) ; } } mlp_library::destroy(ctx) ; }