Skip to content

Commit 7571aee

Browse files
committed
Plugin extension and inout parameter for handles working properly.
1 parent b18468f commit 7571aee

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

source/loader/source/loader_impl.c

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct loader_handle_impl_type
9999
loader_handle module; /* Pointer to the implementation handle, provided by the loader, it is its internal representation */
100100
context ctx; /* Contains the objects, classes and functions loaded in the handle */
101101
int populated; /* If it is populated (0), the handle context is also stored in loader context (global scope), otherwise it is private */
102+
vector populated_handles; /* Vector containing all the references to which this handle has been populated into, it is necessary for detach the symbols when destroying (used in load_from_* when passing an input parameter) */
102103
};
103104

104105
struct loader_impl_handle_register_cb_iterator_type
@@ -517,25 +518,39 @@ loader_handle_impl loader_impl_load_handle(loader_impl impl, loader_impl_interfa
517518
{
518519
loader_handle_impl handle_impl = malloc(sizeof(struct loader_handle_impl_type));
519520

520-
if (handle_impl != NULL)
521+
if (handle_impl == NULL)
521522
{
522-
handle_impl->impl = impl;
523-
handle_impl->iface = iface;
524-
strncpy(handle_impl->path, path, LOADER_PATH_SIZE);
525-
handle_impl->module = module;
526-
handle_impl->ctx = context_create(handle_impl->path);
523+
goto alloc_loader_handle_impl_error;
524+
}
527525

528-
if (handle_impl->ctx == NULL)
529-
{
530-
handle_impl->magic = (uintptr_t)loader_handle_impl_magic_free;
531-
free(handle_impl);
532-
return NULL;
533-
}
526+
handle_impl->impl = impl;
527+
handle_impl->iface = iface;
528+
strncpy(handle_impl->path, path, LOADER_PATH_SIZE);
529+
handle_impl->module = module;
530+
handle_impl->ctx = context_create(handle_impl->path);
531+
532+
if (handle_impl->ctx == NULL)
533+
{
534+
goto alloc_context_error;
535+
}
536+
537+
handle_impl->populated_handles = vector_create_type(loader_handle_impl);
534538

535-
handle_impl->magic = (uintptr_t)loader_handle_impl_magic_alloc;
536-
return handle_impl;
539+
if (handle_impl->populated_handles == NULL)
540+
{
541+
goto alloc_populated_handles_error;
537542
}
538543

544+
handle_impl->magic = (uintptr_t)loader_handle_impl_magic_alloc;
545+
546+
return handle_impl;
547+
548+
alloc_populated_handles_error:
549+
context_destroy(handle_impl->ctx);
550+
alloc_context_error:
551+
handle_impl->magic = (uintptr_t)loader_handle_impl_magic_free;
552+
free(handle_impl);
553+
alloc_loader_handle_impl_error:
539554
return NULL;
540555
}
541556

@@ -544,6 +559,7 @@ void loader_impl_destroy_handle(loader_handle_impl handle_impl)
544559
if (handle_impl != NULL)
545560
{
546561
static const char func_fini_name[] = LOADER_IMPL_FUNCTION_FINI;
562+
size_t iterator;
547563

548564
if (handle_impl->impl->init == 0)
549565
{
@@ -565,7 +581,20 @@ void loader_impl_destroy_handle(loader_handle_impl handle_impl)
565581
context_remove(handle_impl->impl->ctx, handle_impl->ctx);
566582
}
567583

584+
for (iterator = 0; iterator < vector_size(handle_impl->populated_handles); ++iterator)
585+
{
586+
loader_handle_impl populated_handle_impl = vector_at_type(handle_impl->populated_handles, iterator, loader_handle_impl);
587+
588+
if (populated_handle_impl->populated == 0)
589+
{
590+
context_remove(populated_handle_impl->impl->ctx, populated_handle_impl->ctx);
591+
}
592+
593+
context_remove(populated_handle_impl->ctx, handle_impl->ctx);
594+
}
595+
568596
context_destroy(handle_impl->ctx);
597+
vector_destroy(handle_impl->populated_handles);
569598
handle_impl->magic = (uintptr_t)loader_handle_impl_magic_free;
570599

571600
free(handle_impl);
@@ -717,8 +746,10 @@ int loader_impl_handle_register(plugin_manager manager, loader_impl impl, const
717746
log_write("metacall", LOG_LEVEL_ERROR, "Duplicated symbol found named '%s' already defined in the handle scope by handle: %s", duplicated_key, path);
718747
return 1;
719748
}
720-
else if (context_append(impl->ctx, handle_impl->ctx) == 0)
749+
else if (context_append(target_handle->ctx, handle_impl->ctx) == 0)
721750
{
751+
vector_push_back_var(handle_impl->populated_handles, target_handle);
752+
722753
return loader_impl_handle_init(impl, path, handle_impl, NULL, 1);
723754
}
724755
}

0 commit comments

Comments
 (0)