X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmeta_data.c;h=48dcabcec884d8bed391ece4eb184ee3aed9f700;hb=9c3ed6462c15fd1cb664cd0ec2a5efb289aa1af6;hp=6a336c4b67c734b62cf1516c3375b649fd826cac;hpb=ce0118ae5a67654cce06dddc9998dc494beb4251;p=collectd.git diff --git a/src/meta_data.c b/src/meta_data.c index 6a336c4b..48dcabce 100644 --- a/src/meta_data.c +++ b/src/meta_data.c @@ -1,6 +1,6 @@ /** * collectd - src/meta_data.c - * Copyright (C) 2008,2009 Florian octo Forster + * Copyright (C) 2008-2011 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -101,6 +101,24 @@ 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) /* {{{ */ +{ + meta_entry_t *copy; + + if (orig == NULL) + return (NULL); + + copy = md_entry_alloc (orig->key); + copy->type = orig->type; + if (copy->type == MD_TYPE_STRING) + copy->value.mv_string = strdup (orig->value.mv_string); + else + copy->value = orig->value; + + copy->next = md_entry_clone (orig->next); + return (copy); +} /* }}} meta_entry_t *md_entry_clone */ + static void md_entry_free (meta_entry_t *e) /* {{{ */ { if (e == NULL) @@ -209,12 +227,32 @@ meta_data_t *meta_data_create (void) /* {{{ */ return (md); } /* }}} meta_data_t *meta_data_create */ +meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */ +{ + meta_data_t *copy; + + if (orig == NULL) + return (NULL); + + copy = meta_data_create (); + if (copy == NULL) + return (NULL); + + pthread_mutex_lock (&orig->lock); + copy->head = md_entry_clone (orig->head); + pthread_mutex_unlock (&orig->lock); + + return (copy); +} /* }}} meta_data_t *meta_data_clone */ + void meta_data_destroy (meta_data_t *md) /* {{{ */ { if (md == NULL) return; + pthread_mutex_destroy(&md->lock); md_entry_free (md->head); + pthread_mutex_destroy (&md->lock); free (md); } /* }}} void meta_data_destroy */ @@ -275,7 +313,13 @@ int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */ for (e = md->head; e != NULL; e = e->next) ++count; - *toc = malloc(count * sizeof(**toc)); + if (count == 0) + { + pthread_mutex_unlock (&md->lock); + return (count); + } + + *toc = calloc(count, sizeof(**toc)); for (e = md->head; e != NULL; e = e->next) (*toc)[i++] = strdup(e->key); @@ -445,7 +489,7 @@ int meta_data_get_string (meta_data_t *md, /* {{{ */ if (e->type != MD_TYPE_STRING) { - ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key); + ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key); pthread_mutex_unlock (&md->lock); return (-ENOENT); }