Load and unload resources

This call will mmap all the index files and in future some of the work
done in ctx creation can be put here.
This commit is contained in:
Lucas De Marchi 2011-12-08 14:59:51 -02:00
parent 5109f2b422
commit 33bb69b943
3 changed files with 49 additions and 3 deletions

View File

@ -73,6 +73,7 @@ struct kmod_ctx {
char *dirname;
struct kmod_config *config;
struct kmod_hash *modules_by_name;
struct index_mm *indexes[_KMOD_INDEX_LAST];
};
void kmod_log(const struct kmod_ctx *ctx,
@ -257,11 +258,15 @@ KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
if (--ctx->refcount > 0)
return ctx;
INFO(ctx, "context %p released\n", ctx);
kmod_unload_resources(ctx);
kmod_hash_free(ctx->modules_by_name);
free(ctx->dirname);
if (ctx->config)
kmod_config_free(ctx->config);
free(ctx);
return NULL;
}
@ -541,3 +546,42 @@ fail:
*output = NULL;
return -ENOMEM;
}
KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
{
size_t i;
if (ctx == NULL)
return -ENOENT;
for (i = 0; i < ARRAY_SIZE(index_files); i++) {
if (ctx->indexes[i] == NULL) {
const char *fn = index_files[i];
ctx->indexes[i] = index_mm_open(ctx, fn, true);
if (ctx->indexes[i] == NULL)
goto fail;
}
}
return 0;
fail:
kmod_unload_resources(ctx);
return -ENOMEM;
}
KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
{
size_t i;
if (ctx == NULL)
return;
for (i = 0; i < ARRAY_SIZE(index_files); i++) {
if (ctx->indexes[i] != NULL) {
index_mm_close(ctx->indexes[i]);
ctx->indexes[i] = NULL;
}
}
}

View File

@ -48,6 +48,8 @@ int kmod_get_log_priority(const struct kmod_ctx *ctx);
void kmod_set_log_priority(struct kmod_ctx *ctx, int priority);
void *kmod_get_userdata(const struct kmod_ctx *ctx);
void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata);
int kmod_load_resources(struct kmod_ctx *ctx);
void kmod_unload_resources(struct kmod_ctx *ctx);
/*
* kmod_list

View File

@ -13,6 +13,9 @@ global:
kmod_list_prev;
kmod_loaded_get_list;
kmod_load_resources;
kmod_unload_resources;
kmod_module_new_from_name;
kmod_module_new_from_path;
kmod_module_new_from_lookup;
@ -21,7 +24,6 @@ global:
kmod_module_unref_list;
kmod_module_remove_module;
kmod_module_insert_module;
kmod_module_get_dependencies;
kmod_module_get_module;
@ -37,9 +39,7 @@ global:
kmod_module_section_free_list;
kmod_module_section_get_name;
kmod_module_section_get_address;
kmod_module_get_holders;
kmod_module_get_size;
local:
*;