@@ -79,14 +79,15 @@ typedef struct loader_impl_handle_register_cb_iterator_type *loader_impl_handle_
7979
8080struct loader_impl_type
8181{
82- plugin p ; /* Plugin instance to which loader belongs to */
83- int init ; /* Flag for checking if the loader is initialized */
84- set handle_impl_map ; /* Indexes handles by path */
85- loader_impl_data data ; /* Derived metadata provided by the loader, usually contains the data of the VM, Interpreter or JIT */
86- context ctx ; /* Contains the objects, classes and functions loaded in the global scope of each loader */
87- set type_info_map ; /* Stores a set indexed by type name of all of the types existing in the loader (global scope (TODO: may need refactor per handle)) */
88- void * options ; /* Additional initialization options passed in the initialize phase */
89- set exec_path_map ; /* Set of execution paths passed by the end user */
82+ plugin p ; /* Plugin instance to which loader belongs to */
83+ int init ; /* Flag for checking if the loader is initialized */
84+ set handle_impl_path_map ; /* Indexes handles by path */
85+ set handle_impl_map ; /* Indexes handles from loaders to handle impl (loader_handle -> loader_handle_impl) */
86+ loader_impl_data data ; /* Derived metadata provided by the loader, usually contains the data of the VM, Interpreter or JIT */
87+ context ctx ; /* Contains the objects, classes and functions loaded in the global scope of each loader */
88+ set type_info_map ; /* Stores a set indexed by type name of all of the types existing in the loader (global scope (TODO: may need refactor per handle)) */
89+ void * options ; /* Additional initialization options passed in the initialize phase */
90+ set exec_path_map ; /* Set of execution paths passed by the end user */
9091};
9192
9293struct loader_handle_impl_type
@@ -166,7 +167,14 @@ loader_impl loader_impl_allocate(const loader_tag tag)
166167
167168 memset (impl , 0 , sizeof (struct loader_impl_type ));
168169
169- impl -> handle_impl_map = set_create (& hash_callback_str , & comparable_callback_str );
170+ impl -> handle_impl_path_map = set_create (& hash_callback_str , & comparable_callback_str );
171+
172+ if (impl -> handle_impl_path_map == NULL )
173+ {
174+ goto alloc_handle_impl_path_map_error ;
175+ }
176+
177+ impl -> handle_impl_map = set_create (& hash_callback_ptr , & comparable_callback_ptr );
170178
171179 if (impl -> handle_impl_map == NULL )
172180 {
@@ -203,6 +211,8 @@ loader_impl loader_impl_allocate(const loader_tag tag)
203211alloc_type_info_map_error :
204212 set_destroy (impl -> handle_impl_map );
205213alloc_handle_impl_map_error :
214+ set_destroy (impl -> handle_impl_path_map );
215+ alloc_handle_impl_path_map_error :
206216 free (impl );
207217alloc_error :
208218 return NULL ;
@@ -795,17 +805,22 @@ int loader_impl_load_from_file(plugin_manager manager, plugin p, loader_impl imp
795805 {
796806 handle_impl -> populated = 1 ;
797807
798- if (set_insert (impl -> handle_impl_map , handle_impl -> path , handle_impl ) == 0 )
808+ if (set_insert (impl -> handle_impl_path_map , handle_impl -> path , handle_impl ) == 0 )
799809 {
800- if (iface -> discover (impl , handle_impl -> module , handle_impl -> ctx ) == 0 )
810+ if (set_insert (impl -> handle_impl_map , handle_impl -> module , handle_impl ) == 0 )
801811 {
802- if (loader_impl_handle_register ( manager , impl , path , handle_impl , handle_ptr ) == 0 )
812+ if (iface -> discover ( impl , handle_impl -> module , handle_impl -> ctx ) == 0 )
803813 {
804- return 0 ;
814+ if (loader_impl_handle_register (manager , impl , path , handle_impl , handle_ptr ) == 0 )
815+ {
816+ return 0 ;
817+ }
805818 }
819+
820+ set_remove (impl -> handle_impl_map , handle_impl -> module );
806821 }
807822
808- set_remove (impl -> handle_impl_map , handle_impl -> path );
823+ set_remove (impl -> handle_impl_path_map , handle_impl -> path );
809824 }
810825
811826 log_write ("metacall" , LOG_LEVEL_ERROR , "Error when loading handle: %s" , path );
@@ -886,17 +901,22 @@ int loader_impl_load_from_memory(plugin_manager manager, plugin p, loader_impl i
886901 {
887902 handle_impl -> populated = 1 ;
888903
889- if (set_insert (impl -> handle_impl_map , handle_impl -> path , handle_impl ) == 0 )
904+ if (set_insert (impl -> handle_impl_path_map , handle_impl -> path , handle_impl ) == 0 )
890905 {
891- if (iface -> discover (impl , handle_impl -> module , handle_impl -> ctx ) == 0 )
906+ if (set_insert (impl -> handle_impl_map , handle_impl -> module , handle_impl ) == 0 )
892907 {
893- if (loader_impl_handle_register ( manager , impl , name , handle_impl , handle_ptr ) == 0 )
908+ if (iface -> discover ( impl , handle_impl -> module , handle_impl -> ctx ) == 0 )
894909 {
895- return 0 ;
910+ if (loader_impl_handle_register (manager , impl , name , handle_impl , handle_ptr ) == 0 )
911+ {
912+ return 0 ;
913+ }
896914 }
915+
916+ set_remove (impl -> handle_impl_map , handle_impl -> module );
897917 }
898918
899- set_remove (impl -> handle_impl_map , handle_impl -> path );
919+ set_remove (impl -> handle_impl_path_map , handle_impl -> path );
900920 }
901921
902922 log_write ("metacall" , LOG_LEVEL_ERROR , "Error when loading handle: %s" , name );
@@ -946,17 +966,22 @@ int loader_impl_load_from_package(plugin_manager manager, plugin p, loader_impl
946966 {
947967 handle_impl -> populated = 1 ;
948968
949- if (set_insert (impl -> handle_impl_map , handle_impl -> path , handle_impl ) == 0 )
969+ if (set_insert (impl -> handle_impl_path_map , handle_impl -> path , handle_impl ) == 0 )
950970 {
951- if (iface -> discover (impl , handle_impl -> module , handle_impl -> ctx ) == 0 )
971+ if (set_insert (impl -> handle_impl_map , handle_impl -> module , handle_impl ) == 0 )
952972 {
953- if (loader_impl_handle_register ( manager , impl , subpath , handle_impl , handle_ptr ) == 0 )
973+ if (iface -> discover ( impl , handle_impl -> module , handle_impl -> ctx ) == 0 )
954974 {
955- return 0 ;
975+ if (loader_impl_handle_register (manager , impl , subpath , handle_impl , handle_ptr ) == 0 )
976+ {
977+ return 0 ;
978+ }
956979 }
980+
981+ set_remove (impl -> handle_impl_map , handle_impl -> module );
957982 }
958983
959- set_remove (impl -> handle_impl_map , handle_impl -> path );
984+ set_remove (impl -> handle_impl_path_map , handle_impl -> path );
960985 }
961986
962987 log_write ("metacall" , LOG_LEVEL_ERROR , "Error when loading handle: %s" , subpath );
@@ -974,7 +999,7 @@ void *loader_impl_get_handle(loader_impl impl, const char *name)
974999{
9751000 if (impl != NULL && name != NULL )
9761001 {
977- return (void * )set_get (impl -> handle_impl_map , (set_key )name );
1002+ return (void * )set_get (impl -> handle_impl_path_map , (set_key )name );
9781003 }
9791004
9801005 return NULL ;
@@ -1019,14 +1044,14 @@ context loader_impl_handle_context(void *handle)
10191044 return handle_impl -> ctx ;
10201045}
10211046
1022- void * loader_impl_handle_container_of (void * impl )
1047+ void * loader_impl_handle_container_of (loader_impl impl , void * handle )
10231048{
1024- loader_handle handle = impl ;
1025-
1026- #define container_of ( ptr , type , member ) \
1027- (type *)((char *)(ptr) - (char *)&((type *)0)->member)
1049+ if ( handle != NULL )
1050+ {
1051+ return ( void * )(( loader_handle_impl ) set_get ( impl -> handle_impl_map , ( set_key ) handle ));
1052+ }
10281053
1029- return container_of ( handle , struct loader_handle_impl_type , module ) ;
1054+ return NULL ;
10301055}
10311056
10321057int loader_impl_handle_validate (void * handle )
@@ -1157,7 +1182,7 @@ value loader_impl_metadata(loader_impl impl)
11571182{
11581183 struct loader_impl_metadata_cb_iterator_type metadata_iterator ;
11591184
1160- value v = value_create_array (NULL , set_size (impl -> handle_impl_map ));
1185+ value v = value_create_array (NULL , set_size (impl -> handle_impl_path_map ));
11611186
11621187 if (v == NULL )
11631188 {
@@ -1167,7 +1192,7 @@ value loader_impl_metadata(loader_impl impl)
11671192 metadata_iterator .iterator = 0 ;
11681193 metadata_iterator .values = value_to_array (v );
11691194
1170- set_iterate (impl -> handle_impl_map , & loader_impl_metadata_cb_iterate , (set_cb_iterate_args )& metadata_iterator );
1195+ set_iterate (impl -> handle_impl_path_map , & loader_impl_metadata_cb_iterate , (set_cb_iterate_args )& metadata_iterator );
11711196
11721197 return v ;
11731198}
@@ -1180,7 +1205,9 @@ int loader_impl_clear(void *handle)
11801205
11811206 loader_impl impl = handle_impl -> impl ;
11821207
1183- int result = !(set_remove (impl -> handle_impl_map , (set_key )(handle_impl -> path )) == handle_impl );
1208+ int result = !(set_remove (impl -> handle_impl_path_map , (set_key )handle_impl -> path ) == handle_impl );
1209+
1210+ result |= !(set_remove (impl -> handle_impl_map , (set_key )handle_impl -> module ) == handle_impl );
11841211
11851212 loader_impl_destroy_handle (handle_impl );
11861213
@@ -1253,10 +1280,15 @@ void loader_impl_destroy_objects(loader_impl impl)
12531280 */
12541281 if (impl != NULL )
12551282 {
1256- set_iterate (impl -> handle_impl_map , & loader_impl_destroy_handle_map_cb_iterate , NULL );
1283+ /* Destroy all handles */
1284+ set_iterate (impl -> handle_impl_path_map , & loader_impl_destroy_handle_map_cb_iterate , NULL );
1285+
1286+ set_destroy (impl -> handle_impl_path_map );
12571287
1288+ /* Destroy the handle to handle implementation indexing */
12581289 set_destroy (impl -> handle_impl_map );
12591290
1291+ /* Destroy all the types */
12601292 set_iterate (impl -> type_info_map , & loader_impl_destroy_type_map_cb_iterate , NULL );
12611293
12621294 set_destroy (impl -> type_info_map );
0 commit comments