X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fmeta_data.c;h=9e4fd07ecfdf99716a11e64532a8ccb6d5654041;hb=5523a080010dcb7a61dc0dccc3969a2a048e52f9;hp=f87e35e41c78607d1eb3609688d6982c02d341c8;hpb=3dc0454f7bee3e852171d1f6944891693364ee01;p=collectd.git diff --git a/src/daemon/meta_data.c b/src/daemon/meta_data.c index f87e35e4..9e4fd07e 100644 --- a/src/daemon/meta_data.c +++ b/src/daemon/meta_data.c @@ -25,11 +25,10 @@ **/ #include "collectd.h" + #include "plugin.h" #include "meta_data.h" -#include - /* * Data types */ @@ -105,12 +104,15 @@ static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */ return (e); } /* }}} meta_entry_t *md_entry_alloc */ -static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */ +/* XXX: The lock on md must be held while calling this function! */ +static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */ { meta_entry_t *copy; - if (orig == NULL) - return (NULL); + /* WARNINGS : + * - we do not check that orig != NULL here. You should have done it before. + * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION + */ copy = md_entry_alloc (orig->key); if (copy == NULL) @@ -121,6 +123,18 @@ static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */ else copy->value = orig->value; + return (copy); +} /* }}} meta_entry_t *md_entry_clone_contents */ + +static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */ +{ + meta_entry_t *copy; + + if (orig == NULL) + return (NULL); + + copy = md_entry_clone_contents(orig); + copy->next = md_entry_clone (orig->next); return (copy); } /* }}} meta_entry_t *md_entry_clone */ @@ -197,6 +211,63 @@ static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */ } /* }}} int md_entry_insert */ /* XXX: The lock on md must be held while calling this function! */ +static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */ +{ + meta_entry_t *e; + meta_entry_t *this; + meta_entry_t *prev; + + /* WARNINGS : + * - we do not check that md and e != NULL here. You should have done it before. + * - we do not use the lock. You should have set it before. + */ + + e = md_entry_clone_contents(orig); + + prev = NULL; + this = md->head; + while (this != NULL) + { + if (strcasecmp (e->key, this->key) == 0) + break; + + prev = this; + this = this->next; + } + + if (this == NULL) + { + /* This key does not exist yet. */ + if (md->head == NULL) + md->head = e; + else + { + assert (prev != NULL); + prev->next = e; + } + + e->next = NULL; + } + else /* (this != NULL) */ + { + if (prev == NULL) + md->head = e; + else + prev->next = e; + + e->next = this->next; + } + + if (this != NULL) + { + this->next = NULL; + md_entry_free (this); + } + + return (0); +} /* }}} int md_entry_insert_clone */ + +/* XXX: The lock on md must be held while calling this function! */ static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */ const char *key) { @@ -213,6 +284,17 @@ static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */ } /* }}} meta_entry_t *md_entry_lookup */ /* + * Each value_list_t*, as it is going through the system, is handled by exactly + * one thread. Plugins which pass a value_list_t* to another thread, e.g. the + * rrdtool plugin, must create a copy first. The meta data within a + * value_list_t* is not thread safe and doesn't need to be. + * + * The meta data associated with cache entries are a different story. There, we + * need to ensure exclusive locking to prevent leaks and other funky business. + * This is ensured by the uc_meta_data_get_*() functions. + */ + +/* * Public functions */ meta_data_t *meta_data_create (void) /* {{{ */ @@ -249,6 +331,26 @@ meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */ return (copy); } /* }}} meta_data_t *meta_data_clone */ +int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */ +{ + if (orig == NULL) + return (0); + + if (*dest == NULL) { + *dest = meta_data_clone(orig); + return(0); + } + + pthread_mutex_lock (&orig->lock); + for (meta_entry_t *e=orig->head; e != NULL; e = e->next) + { + md_entry_insert_clone((*dest), e); + } + pthread_mutex_unlock (&orig->lock); + + return (0); +} /* }}} int meta_data_clone_merge */ + void meta_data_destroy (meta_data_t *md) /* {{{ */ { if (md == NULL) @@ -261,14 +363,12 @@ void meta_data_destroy (meta_data_t *md) /* {{{ */ int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */ { - meta_entry_t *e; - if ((md == NULL) || (key == NULL)) return (-EINVAL); pthread_mutex_lock (&md->lock); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) { if (strcasecmp (key, e->key) == 0) { @@ -283,14 +383,12 @@ int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */ int meta_data_type (meta_data_t *md, const char *key) /* {{{ */ { - meta_entry_t *e; - if ((md == NULL) || (key == NULL)) return -EINVAL; pthread_mutex_lock (&md->lock); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) { if (strcasecmp (key, e->key) == 0) { @@ -306,14 +404,13 @@ int meta_data_type (meta_data_t *md, const char *key) /* {{{ */ int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */ { int i = 0, count = 0; - meta_entry_t *e; if ((md == NULL) || (toc == NULL)) return -EINVAL; pthread_mutex_lock (&md->lock); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) ++count; if (count == 0) @@ -323,7 +420,7 @@ int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */ } *toc = calloc(count, sizeof(**toc)); - for (e = md->head; e != NULL; e = e->next) + for (meta_entry_t *e = md->head; e != NULL; e = e->next) (*toc)[i++] = strdup(e->key); pthread_mutex_unlock (&md->lock);