Initial commit - move development to a public repo
[libautomation/elektra-notification.git] / lib / managed_data.h
blobd2f34c0a918a29326d13a9412fa0648256c2ef55
1 #ifndef _MANAGEDDATA_H
2 #define _MANAGEDDATA_H
4 #include <search.h>
5 #include <stdlib.h>
6 #include <string.h>
8 /* Collection of primitive data structures that for some reason are still
9 * not available as C library. Since this is only a header file, all functions
10 * have to be defined as inline.
13 #ifndef MD_DECL
14 #define MD_DECL static inline
15 #endif
17 struct MD_LUT_ENTRY {
18 const char *key; /* Needs to be first, so that strcmp() works */
19 void *data;
22 struct MD_LUT {
23 size_t n;
24 size_t size;
25 struct MD_LUT_ENTRY *start;
29 MD_DECL int _md_lut_compare(struct MD_LUT_ENTRY *k1, struct MD_LUT_ENTRY *k2) {
30 return strcmp(k1->key, k2->key);
34 MD_DECL struct MD_LUT_ENTRY *md_lut_find(struct MD_LUT *t, const char *key) {
35 struct MD_LUT_ENTRY ref = {key, NULL};
37 return lfind(&ref, t->start, &t->n, sizeof(struct MD_LUT_ENTRY),
38 (int (*)(const void *, const void *)) strcmp);
41 MD_DECL void md_lut_insert(struct MD_LUT *t, const char *key, void *data) {
42 if (t->n == t->size) {
43 t->size <<= 1;
44 t->start = realloc(t->start, t->size * sizeof(*t->start));
47 t->start[t->n].key = key;
48 t->start[t->n].data = data;
49 t->n++;
52 MD_DECL void md_lut_init(struct MD_LUT *t) {
53 t->size = 8;
54 t->n = 0;
55 t->start = malloc(8 * sizeof(*t->start));
58 #endif