Skip to content

Commit

Permalink
fix(plugins): make list_fields return a const pointer
Browse files Browse the repository at this point in the history
The fields returned by a plugin can be a static string and the plugin
framework doesn't have any business in modifying it so let's make it
const (to avoid a copy for plugins that can return a static string).

Note: I only bump the plugin API version by 0.0.1 since the changes
in this PR do not affect binary compatibility in any way.

Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek authored and poiana committed Mar 8, 2024
1 parent 4abc96d commit 5f4f7ec
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions userspace/libsinsp/plugin_table_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ struct sinsp_table_wrapper
const sinsp_plugin* m_table_plugin_owner;
ss_plugin_table_input* m_table_plugin_input;

static ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields)
static const ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields)
{
auto t = static_cast<sinsp_table_wrapper*>(_t);

Expand Down Expand Up @@ -1231,7 +1231,7 @@ ss_plugin_rc sinsp_table_wrapper::write_entry_field(ss_plugin_table_t* _t, ss_pl
// For sinsp-defined tables, the ss_plugin_table_input is a wrapper around
// the libsinsp::state::table interface. For plugin-defined tables, the
// ss_plugin_table_input is provided by the table-owner plugin itself.
static ss_plugin_table_fieldinfo* dispatch_list_fields(ss_plugin_table_t *_t, uint32_t *nfields)
static const ss_plugin_table_fieldinfo* dispatch_list_fields(ss_plugin_table_t *_t, uint32_t *nfields)
{
auto t = static_cast<ss_plugin_table_input*>(_t);
return t->fields_ext->list_table_fields(t->table, nfields);
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/test/plugins/sample_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class sample_table
return t->entries.size();
}

static ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields)
static const ss_plugin_table_fieldinfo* list_fields(ss_plugin_table_t* _t, uint32_t* nfields)
{
auto t = static_cast<sample_table*>(_t);
*nfields = (uint32_t) t->fields.size();
Expand Down
6 changes: 3 additions & 3 deletions userspace/plugin/plugin_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C" {
// todo(jasondellaluce): when/if major changes to v4, check and solve all todos
#define PLUGIN_API_VERSION_MAJOR 3
#define PLUGIN_API_VERSION_MINOR 4
#define PLUGIN_API_VERSION_PATCH 0
#define PLUGIN_API_VERSION_PATCH 1

//
// Just some not so smart defines to retrieve plugin api version as string
Expand All @@ -51,7 +51,7 @@ extern "C" {
// give this name to the associated *_ext struct.
typedef struct
{
ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields);
const ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields);
ss_plugin_table_field_t* (*get_table_field)(ss_plugin_table_t* t, const char* name, ss_plugin_state_type data_type);
ss_plugin_table_field_t* (*add_table_field)(ss_plugin_table_t* t, const char* name, ss_plugin_state_type data_type);
} ss_plugin_table_fields_vtable;
Expand All @@ -66,7 +66,7 @@ typedef struct
// available in the entries of the table. nfields will be filled with the number
// of elements of the returned array. The array's memory is owned by the
// tables's owner. Returns NULL in case of error.
ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields);
const ss_plugin_table_fieldinfo* (*list_table_fields)(ss_plugin_table_t* t, uint32_t* nfields);
//
// Returns an opaque pointer representing an accessor to a data field
// present in all entries of the table, given its name and type.
Expand Down

0 comments on commit 5f4f7ec

Please sign in to comment.