From: Florian Forster Date: Tue, 22 Jun 2010 20:39:07 +0000 (+0200) Subject: src/graph{,_instance}.[ch]: Implement graph instances as an array in graph_config_t. X-Git-Tag: v4.0.0~200 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=7b68199fab8170cb1c383652379f4b41f1ecf0af;p=collection4.git src/graph{,_instance}.[ch]: Implement graph instances as an array in graph_config_t. The linked-list in graph_instance_t has been removed. --- diff --git a/src/graph.c b/src/graph.c index 6b89859..f6f40d5 100644 --- a/src/graph.c +++ b/src/graph.c @@ -32,20 +32,13 @@ struct graph_config_s /* {{{ */ graph_def_t *defs; - graph_instance_t *instances; + graph_instance_t **instances; + size_t instances_num; }; /* }}} struct graph_config_s */ /* * Private functions */ -static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */ - const graph_ident_t *ident) -{ - if ((cfg == NULL) || (ident == NULL)) - return (NULL); - - return (inst_find_matching (cfg->instances, ident)); -} /* }}} graph_instance_t *graph_find_instance */ /* * Config functions @@ -117,6 +110,8 @@ graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */ void graph_destroy (graph_config_t *cfg) /* {{{ */ { + size_t i; + if (cfg == NULL) return; @@ -126,7 +121,10 @@ void graph_destroy (graph_config_t *cfg) /* {{{ */ free (cfg->vertical_label); def_destroy (cfg->defs); - inst_destroy (cfg->instances); + + for (i = 0; i < cfg->instances_num; i++) + inst_destroy (cfg->instances[i]); + free (cfg->instances); } /* }}} void graph_destroy */ int graph_config_add (const oconfig_item_t *ci) /* {{{ */ @@ -170,17 +168,23 @@ int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */ { graph_instance_t *inst; - inst = graph_find_instance (cfg, file); + inst = graph_inst_find_matching (cfg, file); if (inst == NULL) { + graph_instance_t **tmp; + + tmp = realloc (cfg->instances, + sizeof (*cfg->instances) * (cfg->instances_num + 1)); + if (tmp == NULL) + return (ENOMEM); + cfg->instances = tmp; + inst = inst_create (cfg, file); if (inst == NULL) return (ENOMEM); - if (cfg->instances == NULL) - cfg->instances = inst; - else - inst_append (cfg->instances, inst); + cfg->instances[cfg->instances_num] = inst; + cfg->instances_num++; } return (inst_add_file (inst, file)); @@ -241,14 +245,6 @@ graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */ return (ident_clone (cfg->select)); } /* }}} graph_ident_t *graph_get_selector */ -graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */ -{ - if (cfg == NULL) - return (NULL); - - return (cfg->instances); -} /* }}} graph_instance_t *graph_get_instances */ - graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */ { if (cfg == NULL) @@ -279,43 +275,64 @@ _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */ return (ident_matches (cfg->select, ident)); } /* }}} _Bool graph_matches */ -struct graph_search_data_s +int graph_inst_foreach (graph_config_t *cfg, /* {{{ */ + inst_callback_t cb, void *user_data) { - graph_config_t *cfg; - graph_inst_callback_t callback; - void *user_data; -}; -typedef struct graph_search_data_s graph_search_data_t; + size_t i; + int status; -static int graph_search_submit (graph_instance_t *inst, /* {{{ */ - void *user_data) + for (i = 0; i < cfg->instances_num; i++) + { + status = (*cb) (cfg->instances[i], user_data); + if (status != 0) + return (status); + } + + return (0); +} /* }}} int graph_inst_foreach */ + +graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, /* {{{ */ + graph_ident_t *ident) { - graph_search_data_t *data = user_data; + size_t i; - if ((inst == NULL) || (data == NULL)) - return (EINVAL); + if ((cfg == NULL) || (ident == NULL)) + return (NULL); - return ((*data->callback) (data->cfg, inst, data->user_data)); -} /* }}} int graph_search_submit */ + for (i = 0; i < cfg->instances_num; i++) + if (inst_compare_ident (cfg->instances[i], ident) == 0) + return (cfg->instances[i]); -int graph_inst_foreach (graph_config_t *cfg, /* {{{ */ - inst_callback_t cb, void *user_data) + return (NULL); +} /* }}} graph_instance_t *graph_inst_find_exact */ + +graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, /* {{{ */ + const graph_ident_t *ident) { - return (inst_foreach (cfg->instances, cb, user_data)); -} /* }}} int graph_inst_foreach */ + size_t i; + + if ((cfg == NULL) || (ident == NULL)) + return (NULL); + + for (i = 0; i < cfg->instances_num; i++) + if (inst_matches_ident (cfg->instances[i], ident)) + return (cfg->instances[i]); + + return (NULL); +} /* }}} graph_instance_t *graph_inst_find_matching */ -int graph_search (graph_config_t *cfg, const char *term, /* {{{ */ - graph_inst_callback_t callback, +int graph_inst_search (graph_config_t *cfg, const char *term, /* {{{ */ + graph_inst_callback_t cb, void *user_data) { - graph_search_data_t data = { cfg, callback, user_data }; char buffer[1024]; int status; + size_t i; status = graph_get_title (cfg, buffer, sizeof (buffer)); if (status != 0) { - fprintf (stderr, "graph_search: graph_get_title failed\n"); + fprintf (stderr, "graph_inst_search: graph_get_title failed\n"); return (status); } @@ -323,20 +340,28 @@ int graph_search (graph_config_t *cfg, const char *term, /* {{{ */ if (strstr (buffer, term) != NULL) { - status = inst_foreach (cfg->instances, graph_search_submit, &data); - if (status != 0) - return (status); + for (i = 0; i < cfg->instances_num; i++) + { + status = (*cb) (cfg, cfg->instances[i], user_data); + if (status != 0) + return (status); + } } else { - status = inst_search (cfg, cfg->instances, term, - graph_search_submit, &data); - if (status != 0) - return (status); + for (i = 0; i < cfg->instances_num; i++) + { + if (inst_matches_string (cfg, cfg->instances[i], term)) + { + status = (*cb) (cfg, cfg->instances[i], user_data); + if (status != 0) + return (status); + } + } } return (0); -} /* }}} int graph_search */ +} /* }}} int graph_inst_search */ int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */ { @@ -348,11 +373,16 @@ int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */ int graph_clear_instances (graph_config_t *cfg) /* {{{ */ { + size_t i; + if (cfg == NULL) return (EINVAL); - inst_destroy (cfg->instances); + for (i = 0; i < cfg->instances_num; i++) + inst_destroy (cfg->instances[i]); + free (cfg->instances); cfg->instances = NULL; + cfg->instances_num = 0; return (0); } /* }}} int graph_clear_instances */ diff --git a/src/graph.h b/src/graph.h index 7f1115c..18d168c 100644 --- a/src/graph.h +++ b/src/graph.h @@ -23,8 +23,6 @@ int graph_get_params (graph_config_t *cfg, char *buffer, size_t buffer_size); graph_ident_t *graph_get_selector (graph_config_t *cfg); -graph_instance_t *graph_get_instances (graph_config_t *cfg); - graph_def_t *graph_get_defs (graph_config_t *cfg); int graph_add_def (graph_config_t *cfg, graph_def_t *def); @@ -32,9 +30,15 @@ int graph_add_def (graph_config_t *cfg, graph_def_t *def); _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident); int graph_inst_foreach (graph_config_t *cfg, - inst_callback_t cb, void *user_data); + inst_callback_t cb, void *user_data); + +graph_instance_t *graph_inst_find_exact (graph_config_t *cfg, + graph_ident_t *ident); + +graph_instance_t *graph_inst_find_matching (graph_config_t *cfg, + const graph_ident_t *ident); -int graph_search (graph_config_t *cfg, const char *term, +int graph_inst_search (graph_config_t *cfg, const char *term, graph_inst_callback_t callback, void *user_data); int graph_compare (graph_config_t *cfg, const graph_ident_t *ident); diff --git a/src/graph_instance.c b/src/graph_instance.c index 73aeb1b..e560527 100644 --- a/src/graph_instance.c +++ b/src/graph_instance.c @@ -19,8 +19,6 @@ struct graph_instance_s /* {{{ */ graph_ident_t **files; size_t files_num; - - graph_instance_t *next; }; /* }}} struct graph_instance_s */ struct def_callback_data_s @@ -191,21 +189,16 @@ graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */ i->files = NULL; i->files_num = 0; - i->next = NULL; - return (i); } /* }}} graph_instance_t *inst_create */ void inst_destroy (graph_instance_t *inst) /* {{{ */ { - graph_instance_t *next; size_t i; if (inst == NULL) return; - next = inst->next; - ident_destroy (inst->select); for (i = 0; i < inst->files_num; i++) @@ -213,8 +206,6 @@ void inst_destroy (graph_instance_t *inst) /* {{{ */ free (inst->files); free (inst); - - inst_destroy (next); } /* }}} void inst_destroy */ int inst_add_file (graph_instance_t *inst, /* {{{ */ @@ -240,9 +231,12 @@ graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */ { const char *host = get_part_from_param ("inst_host", "host"); const char *plugin = get_part_from_param ("inst_plugin", "plugin"); - const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance"); + const char *plugin_instance = get_part_from_param ("inst_plugin_instance", + "plugin_instance"); const char *type = get_part_from_param ("inst_type", "type"); - const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance"); + const char *type_instance = get_part_from_param ("inst_type_instance", + "type_instance"); + graph_ident_t *ident; graph_instance_t *inst; @@ -264,19 +258,16 @@ graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */ } ident = ident_create (host, plugin, plugin_instance, type, type_instance); - - for (inst = graph_get_instances (cfg); inst != NULL; inst = inst->next) + if (ident == NULL) { - if (ident_compare (ident, inst->select) != 0) - continue; - - ident_destroy (ident); - return (inst); + fprintf (stderr, "inst_get_selected: ident_create failed\n"); + return (NULL); } - DEBUG ("inst_get_selected: No match found.\n"); + inst = graph_inst_find_exact (cfg, ident); ident_destroy (ident); - return (NULL); + + return (inst); } /* }}} graph_instance_t *inst_get_selected */ int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */ @@ -380,90 +371,49 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */ return (0); } /* }}} int inst_get_params */ -int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */ +int inst_compare_ident (graph_instance_t *inst, /* {{{ */ + const graph_ident_t *ident) { - graph_instance_t *ptr; - - if ((head == NULL) || (inst == NULL)) - return (EINVAL); - - ptr = head; - while (ptr->next != NULL) - ptr = ptr->next; - - ptr->next = inst; + if ((inst == NULL) || (ident == NULL)) + return (0); - return (0); -} /* }}} int inst_append */ + return (ident_compare (inst->select, ident)); +} /* }}} int inst_compare_ident */ -int inst_foreach (graph_instance_t *inst, /* {{{ */ - inst_callback_t cb, void *user_data) +_Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */ + const graph_ident_t *ident) { - graph_instance_t *ptr; - - if ((inst == NULL) || (cb == NULL)) - return (EINVAL); - - for (ptr = inst; ptr != NULL; ptr = ptr->next) - { - int status; - - status = (*cb) (ptr, user_data); - if (status != 0) - return (status); - } + if ((inst == NULL) || (ident == NULL)) + return (0); - return (0); -} /* }}} int inst_foreach */ + return (ident_matches (inst->select, ident)); +} /* }}} _Bool inst_matches_ident */ -int inst_search (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */ - const char *term, inst_callback_t cb, void *user_data) +_Bool inst_matches_string (graph_config_t *cfg, /* {{{ */ + graph_instance_t *inst, + const char *term) { - graph_instance_t *ptr; char buffer[1024]; int status; - if ((inst == NULL) || (cb == NULL)) - return (EINVAL); + if ((cfg == NULL) || (inst == NULL) || (term == NULL)) + return (0); - for (ptr = inst; ptr != NULL; ptr = ptr->next) + status = inst_describe (cfg, inst, buffer, sizeof (buffer)); + if (status != 0) { - status = inst_describe (cfg, ptr, buffer, sizeof (buffer)); - if (status != 0) - { - fprintf (stderr, "inst_search: inst_describe failed\n"); - return (status); - } - - strtolower (buffer); - - /* no match */ - if (strstr (buffer, term) == NULL) - continue; - - /* match */ - status = (*cb) (ptr, user_data); - if (status != 0) - return (status); + fprintf (stderr, "inst_matches_string: inst_describe failed\n"); + return (status); } - return (0); -} /* }}} int inst_search */ + strtolower (buffer); -graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */ - const graph_ident_t *ident) -{ - graph_instance_t *ptr; - - if ((inst == NULL) || (ident == NULL)) - return (NULL); - - for (ptr = inst; ptr != NULL; ptr = ptr->next) - if (ident_matches (ptr->select, ident)) - return (ptr); + /* no match */ + if (strstr (buffer, term) == NULL) + return (0); - return (NULL); -} /* }}} graph_instance_t *inst_find_matching */ + return (1); +} /* }}} _Bool inst_matches_string */ int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */ char *buffer, size_t buffer_size) diff --git a/src/graph_instance.h b/src/graph_instance.h index f1a4486..f0425d2 100644 --- a/src/graph_instance.h +++ b/src/graph_instance.h @@ -26,17 +26,12 @@ int inst_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, graph_ident_t *inst_get_selector (graph_instance_t *inst); -int inst_append (graph_instance_t *head, graph_instance_t *inst); +int inst_compare_ident (graph_instance_t *inst, const graph_ident_t *ident); -int inst_foreach (graph_instance_t *inst, - inst_callback_t cb, void *user_data); +_Bool inst_matches_ident (graph_instance_t *inst, const graph_ident_t *ident); -int inst_search (graph_config_t *cfg, graph_instance_t *inst, - const char *term, inst_callback_t cb, - void *user_data); - -graph_instance_t *inst_find_matching (graph_instance_t *inst, - const graph_ident_t *ident); +_Bool inst_matches_string (graph_config_t *cfg, graph_instance_t *inst, + const char *term); int inst_describe (graph_config_t *cfg, graph_instance_t *inst, char *buffer, size_t buffer_size); diff --git a/src/graph_list.c b/src/graph_list.c index ca088a9..62a4ed3 100644 --- a/src/graph_list.c +++ b/src/graph_list.c @@ -268,7 +268,7 @@ int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */ { int status; - status = graph_search (gl_active[i], term, + status = graph_inst_search (gl_active[i], term, /* callback = */ callback, /* user data = */ user_data); if (status != 0)