@@ -108,7 +108,7 @@ static pthread_mutex_t plugins_guard = PTHREAD_MUTEX_INITIALIZER;
108108 * unloaded with the destroy of the last context. Therefore, to reload the list of plugins, all the contexts must be
109109 * destroyed and with the creation of a first new context after that, the plugins will be reloaded.
110110 */
111- static uint32_t context_refcount = 0 ;
111+ static uint32_t context_refcount ;
112112
113113/**
114114 * @brief Record describing an implemented extension.
@@ -127,51 +127,45 @@ struct lyplg_record {
127127};
128128
129129#ifndef STATIC
130- static struct ly_set plugins_handlers = { 0 } ;
130+ static struct ly_set plugins_handlers ;
131131#endif
132- static struct ly_set plugins_types = { 0 } ;
133- static struct ly_set plugins_extensions = { 0 } ;
132+ struct ly_set ly_plugins_types ;
133+ struct ly_set ly_plugins_extensions ;
134134
135- /**
136- * @brief Get the plugin of the given @p type.
137- *
138- * @param[in] plugin_ref Reference to a plugin. Either an index of a built-in plugin (offset by +1)
139- * or a pointer to an external plugin.
140- * @param[in] type Type of the plugin to get.
141- * @param[in] plugins Array of the built-in plugins used in case @p plugin_ref is an index of a built-in plugin.
142- * @return Plugin of the given @p type or NULL if not found.
143- */
144- static void *
145- lysc_get_plugin (uintptr_t plugin_ref , enum LYPLG type , const struct ly_set * plugins )
135+ /* global counters for the number of static plugins */
136+ uint32_t ly_static_type_plugins_count ;
137+ uint32_t ly_static_ext_plugins_count ;
138+
139+ LIBYANG_API_DEF struct lyplg_type *
140+ lysc_get_type_plugin (uintptr_t plugin_ref )
146141{
147- /* plugin_ref is offset by +1, so 0 is invalid (NULL ptr equivalent) */
148142 if (!plugin_ref ) {
149143 return NULL ;
150144 }
151145
152- if (plugin_ref <= plugins -> count ) {
153- /* plugin is built-in, fetch it from the global list */
154- if (type == LYPLG_EXTENSION ) {
155- return & ((struct lyplg_ext_record * )plugins -> objs [plugin_ref - 1 ])-> plugin ;
156- } else {
157- return & ((struct lyplg_type_record * )plugins -> objs [plugin_ref - 1 ])-> plugin ;
158- }
146+ if (plugin_ref <= ly_plugins_types .count ) {
147+ /* plugin is static, fetch it from the global list */
148+ return & ((struct lyplg_type_record * )ly_plugins_types .objs [plugin_ref - 1 ])-> plugin ;
159149 } else {
160- /* plugin is external , return the pointer */
161- return (void * )plugin_ref ;
150+ /* plugin is dynamic , return the pointer */
151+ return (struct lyplg_type * )plugin_ref ;
162152 }
163153}
164154
165- LIBYANG_API_DEF struct lyplg_type *
166- lysc_get_type_plugin (uintptr_t plugin_ref )
167- {
168- return lysc_get_plugin (plugin_ref , LYPLG_TYPE , & plugins_types );
169- }
170-
171155LIBYANG_API_DEF struct lyplg_ext *
172156lysc_get_ext_plugin (uintptr_t plugin_ref )
173157{
174- return lysc_get_plugin (plugin_ref , LYPLG_EXTENSION , & plugins_extensions );
158+ if (!plugin_ref ) {
159+ return NULL ;
160+ }
161+
162+ if (plugin_ref <= ly_plugins_extensions .count ) {
163+ /* plugin is static, fetch it from the global list */
164+ return & ((struct lyplg_ext_record * )ly_plugins_extensions .objs [plugin_ref - 1 ])-> plugin ;
165+ } else {
166+ /* plugin is dynamic, return the pointer */
167+ return (struct lyplg_ext * )plugin_ref ;
168+ }
175169}
176170
177171/**
@@ -190,9 +184,9 @@ plugins_iter(const struct ly_ctx *ctx, enum LYPLG type, uint32_t *index)
190184 assert (index );
191185
192186 if (type == LYPLG_EXTENSION ) {
193- plugins = ctx ? & ctx -> plugins_extensions : & plugins_extensions ;
187+ plugins = ctx ? & ctx -> plugins_extensions : & ly_plugins_extensions ;
194188 } else {
195- plugins = ctx ? & ctx -> plugins_types : & plugins_types ;
189+ plugins = ctx ? & ctx -> plugins_types : & ly_plugins_types ;
196190 }
197191
198192 if (* index == plugins -> count ) {
@@ -255,35 +249,37 @@ lyplg_record_find(const struct ly_ctx *ctx, enum LYPLG type, const char *module,
255249 * @param[in] module Module name of the plugin.
256250 * @param[in] revision Revision of the @p module.
257251 * @param[in] name Name of the plugin.
258- * @return Accessor to the callbacks plugin structure, use ::lysc_get_type_plugin ()
259- * or ::lysc_get_ext_plugin () on the returned value to get the actual plugin. 0 if not found.
252+ * @return Reference to the callbacks plugin structure. Use ::LYSC_GET_TYPE_PLG ()
253+ * or ::LYSC_GET_EXT_PLG () on the returned value to get the actual plugin. 0 if not found.
260254 */
261255static uintptr_t
262256lyplg_plugin_find (const struct ly_ctx * ctx , enum LYPLG type , const char * module , const char * revision , const char * name )
263257{
264258 struct lyplg_type_record * record = NULL ;
265- uint32_t record_idx = 0 ;
259+ uint32_t record_idx = 0 , static_plugin_count ;
266260
267261 if (ctx ) {
268262 /* try to find context specific plugin */
269263 record = lyplg_record_find (ctx , type , module , revision , name , & record_idx );
264+ if (record ) {
265+ /* plugin found in the context, hence it is dynamic so return the ptr to it */
266+ return (uintptr_t )& record -> plugin ;
267+ }
270268 }
271269
272- if (!record ) {
273- /* try to find shared plugin */
274- record = lyplg_record_find (NULL , type , module , revision , name , & record_idx );
275- }
276-
270+ /* try to find shared plugin */
271+ record = lyplg_record_find (NULL , type , module , revision , name , & record_idx );
277272 if (!record ) {
278273 /* not found */
279274 return 0 ;
280275 }
281276
282- if (!strncmp (record -> plugin .id , "ly2 - " , 6 )) {
283- /* internal plugin, return an index with an offset of +1 in order to keep 0 as an invalid index (a NULL ptr) */
277+ static_plugin_count = (type == LYPLG_TYPE ) ? ly_static_type_plugins_count : ly_static_ext_plugins_count ;
278+ if (record_idx < static_plugin_count ) {
279+ /* static plugin, return an index with an offset of +1 in order to keep 0 as an invalid index (a NULL ptr) */
284280 return record_idx + 1 ;
285281 } else {
286- /* external plugin, return the pointer */
282+ /* dynamic plugin, return the pointer to it */
287283 return (uintptr_t )& record -> plugin ;
288284 }
289285}
@@ -324,15 +320,15 @@ plugins_insert(struct ly_ctx *ctx, enum LYPLG type, const void *recs)
324320 if (type == LYPLG_EXTENSION ) {
325321 const struct lyplg_ext_record * rec = (const struct lyplg_ext_record * )recs ;
326322
327- plugins = ctx ? & ctx -> plugins_extensions : & plugins_extensions ;
323+ plugins = ctx ? & ctx -> plugins_extensions : & ly_plugins_extensions ;
328324
329325 for (uint32_t i = 0 ; rec [i ].name ; i ++ ) {
330326 LY_CHECK_RET (ly_set_add (plugins , (void * )& rec [i ], 0 , NULL ));
331327 }
332328 } else { /* LYPLG_TYPE */
333329 const struct lyplg_type_record * rec = (const struct lyplg_type_record * )recs ;
334330
335- plugins = ctx ? & ctx -> plugins_types : & plugins_types ;
331+ plugins = ctx ? & ctx -> plugins_types : & ly_plugins_types ;
336332
337333 for (uint32_t i = 0 ; rec [i ].name ; i ++ ) {
338334 LY_CHECK_RET (ly_set_add (plugins , (void * )& rec [i ], 0 , NULL ));
@@ -358,9 +354,10 @@ lyplg_clean_(void)
358354 return ;
359355 }
360356
361- ly_set_erase (& plugins_types , NULL );
362- ly_set_erase (& plugins_extensions , NULL );
357+ ly_set_erase (& ly_plugins_types , NULL );
358+ ly_set_erase (& ly_plugins_extensions , NULL );
363359 ly_set_erase (& plugins_handlers , lyplg_close_cb );
360+ ly_static_type_plugins_count = ly_static_ext_plugins_count = 0 ;
364361}
365362
366363#endif
@@ -475,8 +472,8 @@ plugins_load_module(const char *pathname)
475472 }
476473
477474 /* remember the current plugins lists for recovery */
478- types_count = plugins_types .count ;
479- extensions_count = plugins_extensions .count ;
475+ types_count = ly_plugins_types .count ;
476+ extensions_count = ly_plugins_extensions .count ;
480477
481478 /* type plugin */
482479 ret = plugins_load (dlhandler , pathname , LYPLG_TYPE );
@@ -496,11 +493,11 @@ plugins_load_module(const char *pathname)
496493 dlclose (dlhandler );
497494
498495 /* revert changes in the lists */
499- while (plugins_types .count > types_count ) {
500- ly_set_rm_index (& plugins_types , plugins_types .count - 1 , NULL );
496+ while (ly_plugins_types .count > types_count ) {
497+ ly_set_rm_index (& ly_plugins_types , ly_plugins_types .count - 1 , NULL );
501498 }
502- while (plugins_extensions .count > extensions_count ) {
503- ly_set_rm_index (& plugins_extensions , plugins_extensions .count - 1 , NULL );
499+ while (ly_plugins_extensions .count > extensions_count ) {
500+ ly_set_rm_index (& ly_plugins_extensions , ly_plugins_extensions .count - 1 , NULL );
504501 }
505502
506503 return ret ;
@@ -619,6 +616,11 @@ lyplg_init(ly_bool builtin_type_plugins_only, ly_bool static_plugins_only)
619616 LY_CHECK_GOTO (ret = plugins_insert (NULL , LYPLG_EXTENSION , plugins_schema_mount ), error );
620617 LY_CHECK_GOTO (ret = plugins_insert (NULL , LYPLG_EXTENSION , plugins_structure ), error );
621618
619+ /* the global plugin sets contain only static plugins at this point, so assign to the counters here.
620+ * the counters are used to determine whether a plugin is static or not */
621+ ly_static_type_plugins_count = ly_plugins_types .count ;
622+ ly_static_ext_plugins_count = ly_plugins_extensions .count ;
623+
622624#ifndef STATIC
623625 if (!static_plugins_only ) {
624626 /* external types */
0 commit comments