Skip to content

Commit 3d552c6

Browse files
committed
plugins UPDATE replace plg get fn call with macro
Also replace plugin by plugin_ref
1 parent 9732792 commit 3d552c6

35 files changed

+259
-197
lines changed

src/log.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "in_internal.h"
3131
#include "ly_common.h"
3232
#include "plugins_exts.h"
33+
#include "plugins_internal.h"
3334
#include "set.h"
3435
#include "tree_data.h"
3536
#include "tree_data_internal.h"
@@ -885,7 +886,7 @@ lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance
885886
ly_vlog_build_path_line(PARSER_CTX(pctx), &data_path, &schema_path, &line);
886887

887888
va_start(ap, format);
888-
ly_ext_log(PARSER_CTX(pctx), lysc_get_ext_plugin(ext->plugin)->id, level, err, data_path, schema_path, line, format, ap);
889+
ly_ext_log(PARSER_CTX(pctx), LYSC_GET_EXT_PLG(ext->plugin_ref)->id, level, err, data_path, schema_path, line, format, ap);
889890
va_end(ap);
890891
}
891892

@@ -902,7 +903,7 @@ lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instanc
902903
}
903904

904905
va_start(ap, format);
905-
ly_ext_log(ext->module->ctx, lysc_get_ext_plugin(ext->def->plugin)->id, level, err, NULL, schema_path, 0, format, ap);
906+
ly_ext_log(ext->module->ctx, LYSC_GET_EXT_PLG(ext->def->plugin_ref)->id, level, err, NULL, schema_path, 0, format, ap);
906907
va_end(ap);
907908
}
908909

@@ -919,7 +920,7 @@ lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext
919920
}
920921

921922
va_start(ap, format);
922-
ly_ext_log(ext->module->ctx, lysc_get_ext_plugin(ext->def->plugin)->id, level, err, NULL, schema_path, 0, format, ap);
923+
ly_ext_log(ext->module->ctx, LYSC_GET_EXT_PLG(ext->def->plugin_ref)->id, level, err, NULL, schema_path, 0, format, ap);
923924
va_end(ap);
924925
}
925926

@@ -940,7 +941,7 @@ _lyplg_ext_compile_log_err(const struct ly_err_item *eitem, const struct lysc_ex
940941
}
941942

942943
va_start(ap, ext);
943-
ly_ext_log(ext->module->ctx, lysc_get_ext_plugin(ext->def->plugin)->id, eitem->level, eitem->err,
944+
ly_ext_log(ext->module->ctx, LYSC_GET_EXT_PLG(ext->def->plugin_ref)->id, eitem->level, eitem->err,
944945
data_path, schema_path, eitem->line, "%s", ap);
945946
va_end(ap);
946947
}

src/parser_lyb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "parser_internal.h"
3333
#include "plugins_exts.h"
3434
#include "plugins_exts/metadata.h"
35+
#include "plugins_internal.h"
3536
#include "set.h"
3637
#include "tree.h"
3738
#include "tree_data.h"
@@ -267,9 +268,9 @@ lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uin
267268
if (term->type->basetype == LY_TYPE_LEAFREF) {
268269
/* Leafref itself is ignored, the target is loaded directly. */
269270
type_lf = (struct lysc_type_leafref *)term->type;
270-
lyb_data_len = lysc_get_type_plugin(type_lf->realtype->plugin)->lyb_data_len;
271+
lyb_data_len = LYSC_GET_TYPE_PLG(type_lf->realtype->plugin_ref)->lyb_data_len;
271272
} else {
272-
lyb_data_len = lysc_get_type_plugin(term->type->plugin)->lyb_data_len;
273+
lyb_data_len = LYSC_GET_TYPE_PLG(term->type->plugin_ref)->lyb_data_len;
273274
}
274275

275276
if (lyb_data_len < 0) {

src/plugins.c

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
171155
LIBYANG_API_DEF struct lyplg_ext *
172156
lysc_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
*/
261255
static uintptr_t
262256
lyplg_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 */

src/plugins_exts.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ struct lysp_ext_instance {
405405
const char *argument; /**< optional value of the extension's argument */
406406
LY_VALUE_FORMAT format; /**< prefix format of the extension name/argument (::LY_VALUE_XML is YIN format) */
407407
void *prefix_data; /**< format-specific data for prefix resolution (see ly_resolve_prefix()) */
408-
uintptr_t plugin; /**< extension plugin, use ::lysc_get_ext_plugin() */
408+
uintptr_t plugin_ref; /**< reference to extension's manipulation callbacks plugin,
409+
use ::lysc_get_ext_plugin() to get the plugin */
409410

410411
void *parent; /**< pointer to the parent statement holding the extension instance(s), use
411412
::lysp_ext_instance#parent_stmt to access the value/structure */

src/plugins_exts/nacm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "compat.h"
2121
#include "libyang.h"
2222
#include "plugins_exts.h"
23+
#include "plugins_internal.h"
2324

2425
struct nacm_dfs_arg {
2526
struct lysc_ext_instance *ext;
@@ -101,11 +102,11 @@ nacm_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
101102
return LY_ENOT;
102103
}
103104

104-
ext_plugin = lysc_get_ext_plugin(ext->plugin);
105+
ext_plugin = LYSC_GET_EXT_PLG(ext->plugin_ref);
105106

106107
/* check for duplication */
107108
LY_ARRAY_FOR(parent->exts, u) {
108-
parent_ext_plugin = lysc_get_ext_plugin(parent->exts[u].plugin);
109+
parent_ext_plugin = LYSC_GET_EXT_PLG(parent->exts[u].plugin_ref);
109110
if ((&parent->exts[u] != ext) && parent_ext_plugin && !strcmp(parent_ext_plugin->id, ext_plugin->id)) {
110111
/* duplication of a NACM extension on a single node
111112
* We check for all NACM plugins since we want to catch even the situation that there is default-deny-all

src/plugins_internal.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,50 @@
4343
#define LY_TYPE_INT32_STR "32bit integer" /**< text representation of ::LY_TYPE_INT32 */
4444
#define LY_TYPE_INT64_STR "64bit integer" /**< text representation of ::LY_TYPE_INT64 */
4545

46+
/**
47+
* @brief Shared (= not context specific) type plugins set.
48+
*/
49+
extern struct ly_set ly_plugins_types;
50+
51+
/**
52+
* @brief Shared (= not context specific) extension plugins set.
53+
*/
54+
extern struct ly_set ly_plugins_extensions;
55+
56+
/**
57+
* @brief Count of static type plugins.
58+
*/
59+
extern uint32_t ly_static_type_plugins_count;
60+
61+
/**
62+
* @brief Count of static extension plugins.
63+
*/
64+
extern uint32_t ly_static_ext_plugins_count;
65+
66+
/**
67+
* @brief Get a type plugin.
68+
*
69+
* @param[in] PLUGIN_REF Reference to a plugin. Either an index of a static plugin (offset by +1)
70+
* or a pointer to an external plugin.
71+
* @return Type plugin.
72+
*/
73+
#define LYSC_GET_TYPE_PLG(PLUGIN_REF) \
74+
(((uintptr_t)(PLUGIN_REF) <= (uintptr_t)ly_static_type_plugins_count) ? \
75+
(struct lyplg_type *)&((struct lyplg_type_record *)ly_plugins_types.objs[(PLUGIN_REF) - 1])->plugin : \
76+
(struct lyplg_type *)(PLUGIN_REF))
77+
78+
/**
79+
* @brief Get an extension plugin.
80+
*
81+
* @param[in] PLUGIN_REF Reference to a plugin. Either an index of a static plugin (offset by +1)
82+
* or a pointer to an external plugin.
83+
* @return Extension plugin.
84+
*/
85+
#define LYSC_GET_EXT_PLG(PLUGIN_REF) \
86+
(((uintptr_t)(PLUGIN_REF) <= (uintptr_t)ly_static_ext_plugins_count) ? \
87+
(struct lyplg_ext *)&((struct lyplg_ext_record *)ly_plugins_extensions.objs[(PLUGIN_REF) - 1])->plugin : \
88+
(struct lyplg_ext *)(PLUGIN_REF))
89+
4690
/**
4791
* @brief Initiate libyang plugins.
4892
*

src/plugins_types.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ lyplg_type_print_val(const struct lysc_node *node, const char *canon, LY_VALUE_F
903903
struct lyplg_type *type_plugin;
904904

905905
type = ((struct lysc_node_leaf *)node)->type;
906-
type_plugin = lysc_get_type_plugin(type->plugin);
906+
type_plugin = LYSC_GET_TYPE_PLG(type->plugin_ref);
907907

908908
/* store the value */
909909
r = type_plugin->store(node->module->ctx, type, canon, strlen(canon), LYPLG_TYPE_STORE_ONLY, LY_VALUE_CANON,
@@ -1099,7 +1099,7 @@ lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct ly
10991099
goto cleanup;
11001100
}
11011101

1102-
type = lysc_get_type_plugin(lref->plugin);
1102+
type = LYSC_GET_TYPE_PLG(lref->plugin_ref);
11031103

11041104
/* check the result */
11051105
if (target_path) {

0 commit comments

Comments
 (0)