kvlist: add a simply key/value store implementation
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
This commit is contained in:
parent
f59f33f2c7
commit
d4b56b0940
3 changed files with 151 additions and 1 deletions
|
@ -19,7 +19,7 @@ IF(JSONC_FOUND)
|
|||
INCLUDE_DIRECTORIES(${JSONC_INCLUDE_DIRS})
|
||||
ENDIF()
|
||||
|
||||
SET(SOURCES avl.c avl-cmp.c blob.c blobmsg.c uloop.c usock.c ustream.c ustream-fd.c vlist.c utils.c safe_list.c runqueue.c md5.c)
|
||||
SET(SOURCES avl.c avl-cmp.c blob.c blobmsg.c uloop.c usock.c ustream.c ustream-fd.c vlist.c utils.c safe_list.c runqueue.c md5.c kvlist.c)
|
||||
|
||||
ADD_LIBRARY(ubox SHARED ${SOURCES})
|
||||
|
||||
|
|
96
kvlist.c
Normal file
96
kvlist.c
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "avl-cmp.h"
|
||||
#include "blob.h"
|
||||
|
||||
#include "kvlist.h"
|
||||
|
||||
int kvlist_strlen(struct kvlist *kv, void *data)
|
||||
{
|
||||
return strlen(data) + 1;
|
||||
}
|
||||
|
||||
int kvlist_blob_len(struct kvlist *kv, void *data)
|
||||
{
|
||||
return blob_pad_len(data);
|
||||
}
|
||||
|
||||
void kvlist_init(struct kvlist *kv, int (*get_len)(struct kvlist *kv, void *data))
|
||||
{
|
||||
avl_init(&kv->avl, avl_strcmp, false, NULL);
|
||||
kv->get_len = get_len;
|
||||
}
|
||||
|
||||
static struct kvlist_node *__kvlist_get(struct kvlist *kv, const char *name)
|
||||
{
|
||||
struct kvlist_node *node;
|
||||
|
||||
return avl_find_element(&kv->avl, name, node, avl);
|
||||
}
|
||||
|
||||
void *kvlist_get(struct kvlist *kv, const char *name)
|
||||
{
|
||||
struct kvlist_node *node;
|
||||
|
||||
node = __kvlist_get(kv, name);
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
bool kvlist_delete(struct kvlist *kv, const char *name)
|
||||
{
|
||||
struct kvlist_node *node;
|
||||
|
||||
node = __kvlist_get(kv, name);
|
||||
if (node) {
|
||||
avl_delete(&kv->avl, &node->avl);
|
||||
free(node);
|
||||
}
|
||||
|
||||
return !!node;
|
||||
}
|
||||
|
||||
void kvlist_set(struct kvlist *kv, const char *name, void *data)
|
||||
{
|
||||
struct kvlist_node *node;
|
||||
char *name_buf;
|
||||
int len = kv->get_len(kv, data);
|
||||
|
||||
kvlist_delete(kv, name);
|
||||
|
||||
node = calloc_a(sizeof(struct kvlist_node) + len,
|
||||
&name_buf, strlen(name) + 1);
|
||||
memcpy(node->data, data, len);
|
||||
|
||||
node->avl.key = strcpy(name_buf, name);
|
||||
avl_insert(&kv->avl, &node->avl);
|
||||
}
|
||||
|
||||
void kvlist_free(struct kvlist *kv)
|
||||
{
|
||||
struct kvlist_node *node, *tmp;
|
||||
|
||||
avl_remove_all_elements(&kv->avl, node, avl, tmp)
|
||||
free(node);
|
||||
}
|
54
kvlist.h
Normal file
54
kvlist.h
Normal file
|
@ -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
|
Loading…
Reference in a new issue