I made a simple memory manger and I'm trying to compile it using shared library in the driver.
The shared library files itself compiles fine. However, when I compiled the driver by calling the functions in the shared library with my memory manager, it shows me the screenshot below:
Here are my code for the shared.c
/* conveniences for casting and declarations */
typedef block_info* (*MM_CREATE)(size_t, MMPolicy);
typedef void* (*MM_ALLOCATE)(block_info *, size_t, char *);
typedef int (*MM_DEALLOCATE)(block_info *, void *);
typedef void (*MM_DESTROY)(block_info *);
/* Function pointers retrieved from the shared library */
typedef struct LibraryFunctions
{
MM_CREATE create;
MM_DESTROY destroy;
MM_ALLOCATE allocate;
MM_DEALLOCATE deallocate;
}LibraryFunctions;
/* Loads a shared library and returns a pointer to it in libhandle */
/* Returns SUCCESS, if it successful, otherwise, FAILURE */
int load_library(const char *libname, void **libhandle)
{
*libhandle = dlopen(*libhandle, RTLD_LAZY);
if(!(*libhandle))
{
return FAILURE;
}
else
{
return SUCCESS;
}
return *libname;
}
int get_functions(void *libhandle,
LibraryFunctions *functions,
const char **fn_names)
{
functions->create = (MM_CREATE)(intptr_t)dlsym(libhandle, *fn_names);
if(!functions->create)
{
return FAILURE;
}
functions->destroy = (MM_DESTROY)(intptr_t)dlsym(libhandle, *fn_names);
if(!functions->destroy)
{
return FAILURE;
}
functions->allocate = (MM_ALLOCATE)(intptr_t)dlsym(libhandle, *fn_names);
if(!functions->allocate)
{
return FAILURE;
}
functions->deallocate = (MM_DEALLOCATE)(intptr_t)dlsym(libhandle, *fn_names);
if(!functions->deallocate)
{
return FAILURE;
}
return SUCCESS;
} Here is part of the driver code to call the shared library:
void setup(void)
{
const char *fn_names[] = {"mm_create", "mm_destroy", "mm_allocate", "mm_deallocate"};
LibraryFunctions funs;
int error;
error = load_library("./libmemmgr.so", &gLib);
if (error == FAILURE)
{
printf("load_library failed! %s\n", dlerror());
exit(-1);
}
error = get_functions(gLib, &funs, fn_names);
if (error == FAILURE)
{
printf("get_functions failed! %s\n", dlerror());
exit(-1);
}
mmlib_create = funs.create;
mmlib_destroy = funs.destroy;
mmlib_allocate = funs.allocate;
mmlib_deallocate = funs.deallocate;
}
void teardown(void)
{
dlclose(gLib);
}
I'm not sure what is causing the errors.
edit: So I managed to run the exe file but I failed on "get_functions". Are there any problems with my get_functions?