2 * collectd - src/meta_data.c
3 * Copyright (C) 2008-2011 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
29 #include "meta_data.h"
39 int64_t mv_signed_int;
40 uint64_t mv_unsigned_int;
44 typedef union meta_value_u meta_value_t;
47 typedef struct meta_entry_s meta_entry_t;
65 static char *md_strdup (const char *orig) /* {{{ */
73 sz = strlen (orig) + 1;
74 dest = (char *) malloc (sz);
78 memcpy (dest, orig, sz);
81 } /* }}} char *md_strdup */
83 static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */
87 e = (meta_entry_t *) malloc (sizeof (*e));
90 ERROR ("md_entry_alloc: malloc failed.");
93 memset (e, 0, sizeof (*e));
95 e->key = md_strdup (key);
99 ERROR ("md_entry_alloc: md_strdup failed.");
107 } /* }}} meta_entry_t *md_entry_alloc */
109 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
116 copy = md_entry_alloc (orig->key);
119 copy->type = orig->type;
120 if (copy->type == MD_TYPE_STRING)
121 copy->value.mv_string = strdup (orig->value.mv_string);
123 copy->value = orig->value;
125 copy->next = md_entry_clone (orig->next);
127 } /* }}} meta_entry_t *md_entry_clone */
129 static void md_entry_free (meta_entry_t *e) /* {{{ */
136 if (e->type == MD_TYPE_STRING)
137 free (e->value.mv_string);
140 md_entry_free (e->next);
143 } /* }}} void md_entry_free */
145 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
150 if ((md == NULL) || (e == NULL))
153 pthread_mutex_lock (&md->lock);
159 if (strcasecmp (e->key, this->key) == 0)
168 /* This key does not exist yet. */
169 if (md->head == NULL)
173 assert (prev != NULL);
179 else /* (this != NULL) */
186 e->next = this->next;
189 pthread_mutex_unlock (&md->lock);
194 md_entry_free (this);
198 } /* }}} int md_entry_insert */
200 /* XXX: The lock on md must be held while calling this function! */
201 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
206 if ((md == NULL) || (key == NULL))
209 for (e = md->head; e != NULL; e = e->next)
210 if (strcasecmp (key, e->key) == 0)
214 } /* }}} meta_entry_t *md_entry_lookup */
217 * Each value_list_t*, as it is going through the system, is handled by exactly
218 * one thread. Plugins which pass a value_list_t* to another thread, e.g. the
219 * rrdtool plugin, must create a copy first. The meta data within a
220 * value_list_t* is not thread safe and doesn't need to be.
222 * The meta data associated with cache entries are a different story. There, we
223 * need to ensure exclusive locking to prevent leaks and other funky business.
224 * This is ensured by the uc_meta_data_get_*() functions.
230 meta_data_t *meta_data_create (void) /* {{{ */
234 md = (meta_data_t *) malloc (sizeof (*md));
237 ERROR ("meta_data_create: malloc failed.");
240 memset (md, 0, sizeof (*md));
243 pthread_mutex_init (&md->lock, /* attr = */ NULL);
246 } /* }}} meta_data_t *meta_data_create */
248 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
255 copy = meta_data_create ();
259 pthread_mutex_lock (&orig->lock);
260 copy->head = md_entry_clone (orig->head);
261 pthread_mutex_unlock (&orig->lock);
264 } /* }}} meta_data_t *meta_data_clone */
266 void meta_data_destroy (meta_data_t *md) /* {{{ */
271 md_entry_free (md->head);
272 pthread_mutex_destroy (&md->lock);
274 } /* }}} void meta_data_destroy */
276 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
280 if ((md == NULL) || (key == NULL))
283 pthread_mutex_lock (&md->lock);
285 for (e = md->head; e != NULL; e = e->next)
287 if (strcasecmp (key, e->key) == 0)
289 pthread_mutex_unlock (&md->lock);
294 pthread_mutex_unlock (&md->lock);
296 } /* }}} int meta_data_exists */
298 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
302 if ((md == NULL) || (key == NULL))
305 pthread_mutex_lock (&md->lock);
307 for (e = md->head; e != NULL; e = e->next)
309 if (strcasecmp (key, e->key) == 0)
311 pthread_mutex_unlock (&md->lock);
316 pthread_mutex_unlock (&md->lock);
318 } /* }}} int meta_data_type */
320 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
322 int i = 0, count = 0;
325 if ((md == NULL) || (toc == NULL))
328 pthread_mutex_lock (&md->lock);
330 for (e = md->head; e != NULL; e = e->next)
335 pthread_mutex_unlock (&md->lock);
339 *toc = calloc(count, sizeof(**toc));
340 for (e = md->head; e != NULL; e = e->next)
341 (*toc)[i++] = strdup(e->key);
343 pthread_mutex_unlock (&md->lock);
345 } /* }}} int meta_data_toc */
347 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
352 if ((md == NULL) || (key == NULL))
355 pthread_mutex_lock (&md->lock);
361 if (strcasecmp (key, this->key) == 0)
370 pthread_mutex_unlock (&md->lock);
375 md->head = this->next;
377 prev->next = this->next;
379 pthread_mutex_unlock (&md->lock);
382 md_entry_free (this);
385 } /* }}} int meta_data_delete */
390 int meta_data_add_string (meta_data_t *md, /* {{{ */
391 const char *key, const char *value)
395 if ((md == NULL) || (key == NULL) || (value == NULL))
398 e = md_entry_alloc (key);
402 e->value.mv_string = md_strdup (value);
403 if (e->value.mv_string == NULL)
405 ERROR ("meta_data_add_string: md_strdup failed.");
409 e->type = MD_TYPE_STRING;
411 return (md_entry_insert (md, e));
412 } /* }}} int meta_data_add_string */
414 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
415 const char *key, int64_t value)
419 if ((md == NULL) || (key == NULL))
422 e = md_entry_alloc (key);
426 e->value.mv_signed_int = value;
427 e->type = MD_TYPE_SIGNED_INT;
429 return (md_entry_insert (md, e));
430 } /* }}} int meta_data_add_signed_int */
432 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
433 const char *key, uint64_t value)
437 if ((md == NULL) || (key == NULL))
440 e = md_entry_alloc (key);
444 e->value.mv_unsigned_int = value;
445 e->type = MD_TYPE_UNSIGNED_INT;
447 return (md_entry_insert (md, e));
448 } /* }}} int meta_data_add_unsigned_int */
450 int meta_data_add_double (meta_data_t *md, /* {{{ */
451 const char *key, double value)
455 if ((md == NULL) || (key == NULL))
458 e = md_entry_alloc (key);
462 e->value.mv_double = value;
463 e->type = MD_TYPE_DOUBLE;
465 return (md_entry_insert (md, e));
466 } /* }}} int meta_data_add_double */
468 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
469 const char *key, _Bool value)
473 if ((md == NULL) || (key == NULL))
476 e = md_entry_alloc (key);
480 e->value.mv_boolean = value;
481 e->type = MD_TYPE_BOOLEAN;
483 return (md_entry_insert (md, e));
484 } /* }}} int meta_data_add_boolean */
489 int meta_data_get_string (meta_data_t *md, /* {{{ */
490 const char *key, char **value)
495 if ((md == NULL) || (key == NULL) || (value == NULL))
498 pthread_mutex_lock (&md->lock);
500 e = md_entry_lookup (md, key);
503 pthread_mutex_unlock (&md->lock);
507 if (e->type != MD_TYPE_STRING)
509 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
510 pthread_mutex_unlock (&md->lock);
514 temp = md_strdup (e->value.mv_string);
517 pthread_mutex_unlock (&md->lock);
518 ERROR ("meta_data_get_string: md_strdup failed.");
522 pthread_mutex_unlock (&md->lock);
527 } /* }}} int meta_data_get_string */
529 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
530 const char *key, int64_t *value)
534 if ((md == NULL) || (key == NULL) || (value == NULL))
537 pthread_mutex_lock (&md->lock);
539 e = md_entry_lookup (md, key);
542 pthread_mutex_unlock (&md->lock);
546 if (e->type != MD_TYPE_SIGNED_INT)
548 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
549 pthread_mutex_unlock (&md->lock);
553 *value = e->value.mv_signed_int;
555 pthread_mutex_unlock (&md->lock);
557 } /* }}} int meta_data_get_signed_int */
559 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
560 const char *key, uint64_t *value)
564 if ((md == NULL) || (key == NULL) || (value == NULL))
567 pthread_mutex_lock (&md->lock);
569 e = md_entry_lookup (md, key);
572 pthread_mutex_unlock (&md->lock);
576 if (e->type != MD_TYPE_UNSIGNED_INT)
578 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
579 pthread_mutex_unlock (&md->lock);
583 *value = e->value.mv_unsigned_int;
585 pthread_mutex_unlock (&md->lock);
587 } /* }}} int meta_data_get_unsigned_int */
589 int meta_data_get_double (meta_data_t *md, /* {{{ */
590 const char *key, double *value)
594 if ((md == NULL) || (key == NULL) || (value == NULL))
597 pthread_mutex_lock (&md->lock);
599 e = md_entry_lookup (md, key);
602 pthread_mutex_unlock (&md->lock);
606 if (e->type != MD_TYPE_DOUBLE)
608 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
609 pthread_mutex_unlock (&md->lock);
613 *value = e->value.mv_double;
615 pthread_mutex_unlock (&md->lock);
617 } /* }}} int meta_data_get_double */
619 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
620 const char *key, _Bool *value)
624 if ((md == NULL) || (key == NULL) || (value == NULL))
627 pthread_mutex_lock (&md->lock);
629 e = md_entry_lookup (md, key);
632 pthread_mutex_unlock (&md->lock);
636 if (e->type != MD_TYPE_BOOLEAN)
638 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
639 pthread_mutex_unlock (&md->lock);
643 *value = e->value.mv_boolean;
645 pthread_mutex_unlock (&md->lock);
647 } /* }}} int meta_data_get_boolean */
649 /* vim: set sw=2 sts=2 et fdm=marker : */