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 /* XXX: The lock on md must be held while calling this function! */
110 static meta_entry_t *md_entry_clone_contents (const meta_entry_t *orig) /* {{{ */
115 * - we do not check that orig != NULL here. You should have done it before.
116 * - we do not set copy->next. DO NOT FORGET TO SET copy->next IN YOUR FUNCTION
119 copy = md_entry_alloc (orig->key);
120 copy->type = orig->type;
121 if (copy->type == MD_TYPE_STRING)
122 copy->value.mv_string = strdup (orig->value.mv_string);
124 copy->value = orig->value;
127 } /* }}} meta_entry_t *md_entry_clone_contents */
129 static meta_entry_t *md_entry_clone (const meta_entry_t *orig) /* {{{ */
136 copy = md_entry_clone_contents(orig);
138 copy->next = md_entry_clone (orig->next);
140 } /* }}} meta_entry_t *md_entry_clone */
142 static void md_entry_free (meta_entry_t *e) /* {{{ */
149 if (e->type == MD_TYPE_STRING)
150 free (e->value.mv_string);
153 md_entry_free (e->next);
156 } /* }}} void md_entry_free */
158 static int md_entry_insert (meta_data_t *md, meta_entry_t *e) /* {{{ */
163 if ((md == NULL) || (e == NULL))
166 pthread_mutex_lock (&md->lock);
172 if (strcasecmp (e->key, this->key) == 0)
181 /* This key does not exist yet. */
182 if (md->head == NULL)
186 assert (prev != NULL);
192 else /* (this != NULL) */
199 e->next = this->next;
202 pthread_mutex_unlock (&md->lock);
207 md_entry_free (this);
211 } /* }}} int md_entry_insert */
213 /* XXX: The lock on md must be held while calling this function! */
214 static int md_entry_insert_clone (meta_data_t *md, meta_entry_t *orig) /* {{{ */
221 * - we do not check that md and e != NULL here. You should have done it before.
222 * - we do not use the lock. You should have set it before.
225 e = md_entry_clone_contents(orig);
231 if (strcasecmp (e->key, this->key) == 0)
240 /* This key does not exist yet. */
241 if (md->head == NULL)
245 assert (prev != NULL);
251 else /* (this != NULL) */
258 e->next = this->next;
264 md_entry_free (this);
268 } /* }}} int md_entry_insert_clone */
270 /* XXX: The lock on md must be held while calling this function! */
271 static meta_entry_t *md_entry_lookup (meta_data_t *md, /* {{{ */
276 if ((md == NULL) || (key == NULL))
279 for (e = md->head; e != NULL; e = e->next)
280 if (strcasecmp (key, e->key) == 0)
284 } /* }}} meta_entry_t *md_entry_lookup */
289 meta_data_t *meta_data_create (void) /* {{{ */
293 md = (meta_data_t *) malloc (sizeof (*md));
296 ERROR ("meta_data_create: malloc failed.");
299 memset (md, 0, sizeof (*md));
302 pthread_mutex_init (&md->lock, /* attr = */ NULL);
305 } /* }}} meta_data_t *meta_data_create */
307 meta_data_t *meta_data_clone (meta_data_t *orig) /* {{{ */
314 copy = meta_data_create ();
318 pthread_mutex_lock (&orig->lock);
319 copy->head = md_entry_clone (orig->head);
320 pthread_mutex_unlock (&orig->lock);
323 } /* }}} meta_data_t *meta_data_clone */
325 int meta_data_clone_merge (meta_data_t **dest, meta_data_t *orig) /* {{{ */
333 *dest = meta_data_clone(orig);
337 pthread_mutex_lock (&orig->lock);
338 for(e=orig->head; NULL != e; e = e->next) {
339 md_entry_insert_clone((*dest), e);
341 pthread_mutex_unlock (&orig->lock);
344 } /* }}} int meta_data_clone_merge */
346 void meta_data_destroy (meta_data_t *md) /* {{{ */
351 pthread_mutex_destroy(&md->lock);
352 md_entry_free (md->head);
353 pthread_mutex_destroy (&md->lock);
355 } /* }}} void meta_data_destroy */
357 int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
361 if ((md == NULL) || (key == NULL))
364 pthread_mutex_lock (&md->lock);
366 for (e = md->head; e != NULL; e = e->next)
368 if (strcasecmp (key, e->key) == 0)
370 pthread_mutex_unlock (&md->lock);
375 pthread_mutex_unlock (&md->lock);
377 } /* }}} int meta_data_exists */
379 int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
383 if ((md == NULL) || (key == NULL))
386 pthread_mutex_lock (&md->lock);
388 for (e = md->head; e != NULL; e = e->next)
390 if (strcasecmp (key, e->key) == 0)
392 pthread_mutex_unlock (&md->lock);
397 pthread_mutex_unlock (&md->lock);
399 } /* }}} int meta_data_type */
401 int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
403 int i = 0, count = 0;
406 if ((md == NULL) || (toc == NULL))
409 pthread_mutex_lock (&md->lock);
411 for (e = md->head; e != NULL; e = e->next)
416 pthread_mutex_unlock (&md->lock);
420 *toc = calloc(count, sizeof(**toc));
421 for (e = md->head; e != NULL; e = e->next)
422 (*toc)[i++] = strdup(e->key);
424 pthread_mutex_unlock (&md->lock);
426 } /* }}} int meta_data_toc */
428 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
433 if ((md == NULL) || (key == NULL))
436 pthread_mutex_lock (&md->lock);
442 if (strcasecmp (key, this->key) == 0)
451 pthread_mutex_unlock (&md->lock);
456 md->head = this->next;
458 prev->next = this->next;
460 pthread_mutex_unlock (&md->lock);
463 md_entry_free (this);
466 } /* }}} int meta_data_delete */
471 int meta_data_add_string (meta_data_t *md, /* {{{ */
472 const char *key, const char *value)
476 if ((md == NULL) || (key == NULL) || (value == NULL))
479 e = md_entry_alloc (key);
483 e->value.mv_string = md_strdup (value);
484 if (e->value.mv_string == NULL)
486 ERROR ("meta_data_add_string: md_strdup failed.");
490 e->type = MD_TYPE_STRING;
492 return (md_entry_insert (md, e));
493 } /* }}} int meta_data_add_string */
495 int meta_data_add_signed_int (meta_data_t *md, /* {{{ */
496 const char *key, int64_t value)
500 if ((md == NULL) || (key == NULL))
503 e = md_entry_alloc (key);
507 e->value.mv_signed_int = value;
508 e->type = MD_TYPE_SIGNED_INT;
510 return (md_entry_insert (md, e));
511 } /* }}} int meta_data_add_signed_int */
513 int meta_data_add_unsigned_int (meta_data_t *md, /* {{{ */
514 const char *key, uint64_t value)
518 if ((md == NULL) || (key == NULL))
521 e = md_entry_alloc (key);
525 e->value.mv_unsigned_int = value;
526 e->type = MD_TYPE_UNSIGNED_INT;
528 return (md_entry_insert (md, e));
529 } /* }}} int meta_data_add_unsigned_int */
531 int meta_data_add_double (meta_data_t *md, /* {{{ */
532 const char *key, double value)
536 if ((md == NULL) || (key == NULL))
539 e = md_entry_alloc (key);
543 e->value.mv_double = value;
544 e->type = MD_TYPE_DOUBLE;
546 return (md_entry_insert (md, e));
547 } /* }}} int meta_data_add_double */
549 int meta_data_add_boolean (meta_data_t *md, /* {{{ */
550 const char *key, _Bool value)
554 if ((md == NULL) || (key == NULL))
557 e = md_entry_alloc (key);
561 e->value.mv_boolean = value;
562 e->type = MD_TYPE_BOOLEAN;
564 return (md_entry_insert (md, e));
565 } /* }}} int meta_data_add_boolean */
570 int meta_data_get_string (meta_data_t *md, /* {{{ */
571 const char *key, char **value)
576 if ((md == NULL) || (key == NULL) || (value == NULL))
579 pthread_mutex_lock (&md->lock);
581 e = md_entry_lookup (md, key);
584 pthread_mutex_unlock (&md->lock);
588 if (e->type != MD_TYPE_STRING)
590 ERROR ("meta_data_get_string: Type mismatch for key `%s'", e->key);
591 pthread_mutex_unlock (&md->lock);
595 temp = md_strdup (e->value.mv_string);
598 pthread_mutex_unlock (&md->lock);
599 ERROR ("meta_data_get_string: md_strdup failed.");
603 pthread_mutex_unlock (&md->lock);
608 } /* }}} int meta_data_get_string */
610 int meta_data_get_signed_int (meta_data_t *md, /* {{{ */
611 const char *key, int64_t *value)
615 if ((md == NULL) || (key == NULL) || (value == NULL))
618 pthread_mutex_lock (&md->lock);
620 e = md_entry_lookup (md, key);
623 pthread_mutex_unlock (&md->lock);
627 if (e->type != MD_TYPE_SIGNED_INT)
629 ERROR ("meta_data_get_signed_int: Type mismatch for key `%s'", e->key);
630 pthread_mutex_unlock (&md->lock);
634 *value = e->value.mv_signed_int;
636 pthread_mutex_unlock (&md->lock);
638 } /* }}} int meta_data_get_signed_int */
640 int meta_data_get_unsigned_int (meta_data_t *md, /* {{{ */
641 const char *key, uint64_t *value)
645 if ((md == NULL) || (key == NULL) || (value == NULL))
648 pthread_mutex_lock (&md->lock);
650 e = md_entry_lookup (md, key);
653 pthread_mutex_unlock (&md->lock);
657 if (e->type != MD_TYPE_UNSIGNED_INT)
659 ERROR ("meta_data_get_unsigned_int: Type mismatch for key `%s'", e->key);
660 pthread_mutex_unlock (&md->lock);
664 *value = e->value.mv_unsigned_int;
666 pthread_mutex_unlock (&md->lock);
668 } /* }}} int meta_data_get_unsigned_int */
670 int meta_data_get_double (meta_data_t *md, /* {{{ */
671 const char *key, double *value)
675 if ((md == NULL) || (key == NULL) || (value == NULL))
678 pthread_mutex_lock (&md->lock);
680 e = md_entry_lookup (md, key);
683 pthread_mutex_unlock (&md->lock);
687 if (e->type != MD_TYPE_DOUBLE)
689 ERROR ("meta_data_get_double: Type mismatch for key `%s'", e->key);
690 pthread_mutex_unlock (&md->lock);
694 *value = e->value.mv_double;
696 pthread_mutex_unlock (&md->lock);
698 } /* }}} int meta_data_get_double */
700 int meta_data_get_boolean (meta_data_t *md, /* {{{ */
701 const char *key, _Bool *value)
705 if ((md == NULL) || (key == NULL) || (value == NULL))
708 pthread_mutex_lock (&md->lock);
710 e = md_entry_lookup (md, key);
713 pthread_mutex_unlock (&md->lock);
717 if (e->type != MD_TYPE_BOOLEAN)
719 ERROR ("meta_data_get_boolean: Type mismatch for key `%s'", e->key);
720 pthread_mutex_unlock (&md->lock);
724 *value = e->value.mv_boolean;
726 pthread_mutex_unlock (&md->lock);
728 } /* }}} int meta_data_get_boolean */
730 /* vim: set sw=2 sts=2 et fdm=marker : */