Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2014-04-12 03:42:44 +0400
committerFelix Fietkau <nbd@openwrt.org>2014-04-12 03:42:44 +0400
commitd4b56b09403e6bf802633c447a3877eefb6ff41c (patch)
tree21c360d3b9eaedc516cf5948e096758235b84192 /kvlist.h
parentf59f33f2c723b3c52f221234656d0e522a4dae00 (diff)
kvlist: add a simply key/value store implementation
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
Diffstat (limited to 'kvlist.h')
-rw-r--r--kvlist.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/kvlist.h b/kvlist.h
new file mode 100644
index 0000000..339cc1d
--- /dev/null
+++ b/kvlist.h
@@ -0,0 +1,54 @@
+/*
+ * kvlist - simple key/value store
+ *
+ * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#ifndef __LIBUBOX_KVLIST_H
+#define __LIBUBOX_KVLIST_H
+
+#include "avl.h"
+
+struct kvlist {
+ struct avl_tree avl;
+
+ int (*get_len)(struct kvlist *kv, void *data);
+};
+
+struct kvlist_node {
+ struct avl_node avl;
+
+ char data[0] __attribute__((aligned(4)));
+};
+
+#define __ptr_to_kv(_ptr) container_of(((char *) (_ptr)), struct kvlist_node, data[0])
+#define __avl_list_to_kv(_l) container_of(_l, struct kvlist_node, avl.list)
+
+#define kvlist_for_each(kv, name, value) \
+ for (value = (void *) __avl_list_to_kv((kv)->avl.list_head.next)->data, \
+ name = (const char *) __ptr_to_kv(value)->avl.key; \
+ &__ptr_to_kv(value)->avl.list != &(kv)->avl.list_head; \
+ value = (void *) (__avl_list_to_kv(__ptr_to_kv(value)->avl.list.next))->data, \
+ name = (const char *) __ptr_to_kv(value)->avl.key)
+
+void kvlist_init(struct kvlist *kv, int (*get_len)(struct kvlist *kv, void *data));
+void kvlist_free(struct kvlist *kv);
+void *kvlist_get(struct kvlist *kv, const char *name);
+void kvlist_set(struct kvlist *kv, const char *name, void *data);
+bool kvlist_delete(struct kvlist *kv, const char *name);
+
+int kvlist_strlen(struct kvlist *kv, void *data);
+int kvlist_blob_len(struct kvlist *kv, void *data);
+
+#endif