From d9f2b6d6fa5ea80eadbb10f70cde36683ca5dd37 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Tue, 12 Aug 2008 21:24:41 +0200 Subject: [PATCH] WIP: add config_lookup skeleton --- src/backend/Makefile.am | 6 +- src/backend/config.c | 5 ++ src/backend/config.h | 7 +- src/backend/config_lookup.c | 151 ++++++++++++++++++++++++++++++++++++ src/backend/config_lookup.h | 102 ++++++++++++++++++++++++ 5 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 src/backend/config_lookup.c create mode 100644 src/backend/config_lookup.h diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index afe7cc1f3..f9d19d5e1 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -31,7 +31,8 @@ liblumibackend_a_SOURCES = \ $(liblumibackend_a_srcdir)/config.c \ $(liblumibackend_a_srcdir)/config_typed.c \ $(liblumibackend_a_srcdir)/configentry.c \ - $(liblumibackend_a_srcdir)/configitem.c + $(liblumibackend_a_srcdir)/configitem.c \ + $(liblumibackend_a_srcdir)/config_lookup.c noinst_HEADERS += \ @@ -43,5 +44,6 @@ noinst_HEADERS += \ $(liblumibackend_a_srcdir)/filehandlecache.h \ $(liblumibackend_a_srcdir)/config.h \ $(liblumibackend_a_srcdir)/configentry.h \ - $(liblumibackend_a_srcdir)/configitem.h + $(liblumibackend_a_srcdir)/configitem.h \ + $(liblumibackend_a_srcdir)/config_lookup.h diff --git a/src/backend/config.c b/src/backend/config.c index deec72697..b68d30168 100644 --- a/src/backend/config.c +++ b/src/backend/config.c @@ -43,6 +43,7 @@ NOBUG_DEFINE_FLAG_PARENT (config, config_all); NOBUG_DEFINE_FLAG_PARENT (config_typed, config_all); NOBUG_DEFINE_FLAG_PARENT (config_file, config_all); NOBUG_DEFINE_FLAG_PARENT (config_item, config_all); +NOBUG_DEFINE_FLAG_PARENT (config_lookup, config_all); LUMIERA_ERROR_DEFINE (CONFIG_SYNTAX, "syntax error in configfile"); LUMIERA_ERROR_DEFINE (CONFIG_SYNTAX_KEY, "syntax error in key"); @@ -67,9 +68,12 @@ lumiera_config_init (const char* path) NOBUG_INIT_FLAG (config_typed); NOBUG_INIT_FLAG (config_file); NOBUG_INIT_FLAG (config_item); + NOBUG_INIT_FLAG (config_lookup); lumiera_global_config = lumiera_malloc (sizeof (*lumiera_global_config)); lumiera_global_config->path = lumiera_strndup (path, SIZE_MAX); + lumiera_config_lookup_init (&lumiera_global_config->keys); + lumiera_rwlock_init (&lumiera_global_config->lock, "config rwlock", &NOBUG_FLAG (config)); return 0; @@ -83,6 +87,7 @@ lumiera_config_destroy () if (lumiera_global_config) { lumiera_rwlock_destroy (&lumiera_global_config->lock, &NOBUG_FLAG (config)); + lumiera_config_lookup_destroy (&lumiera_global_config->keys); lumiera_free (lumiera_global_config->path); lumiera_free (lumiera_global_config); lumiera_global_config = NULL; diff --git a/src/backend/config.h b/src/backend/config.h index 1daf54087..80a3f787f 100644 --- a/src/backend/config.h +++ b/src/backend/config.h @@ -40,6 +40,8 @@ NOBUG_DECLARE_FLAG (config_typed); NOBUG_DECLARE_FLAG (config_file); /* single config items */ NOBUG_DECLARE_FLAG (config_item); +/* lookup config keys */ +NOBUG_DECLARE_FLAG (config_lookup); LUMIERA_ERROR_DECLARE (CONFIG_SYNTAX); @@ -49,7 +51,7 @@ LUMIERA_ERROR_DECLARE (CONFIG_NO_ENTRY); LUMIERA_ERROR_DECLARE (CONFIG_DEFAULT); //TODO: Lumiera header includes// - +#include "backend/config_lookup.h" //TODO: System includes// #include @@ -65,7 +67,8 @@ LUMIERA_ERROR_DECLARE (CONFIG_DEFAULT); struct lumiera_config_struct { - // cuckoo hash + lumiera_config_lookup keys; + // configfile list char* path; /* diff --git a/src/backend/config_lookup.c b/src/backend/config_lookup.c new file mode 100644 index 000000000..b0b6caeaa --- /dev/null +++ b/src/backend/config_lookup.c @@ -0,0 +1,151 @@ +/* + config_lookup.c - Lookup functions for the config subsystem + + Copyright (C) Lumiera.org + 2008, Christian Thaeter + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +//TODO: Support library includes// +#include "lib/safeclib.h" + + +//TODO: Lumiera header includes// +#include "backend/config_lookup.h" +#include "backend/config.h" + +//TODO: System includes// +//#include +//#include + +//TODO: internal/static forward declarations// +static size_t +h1 (const void* item, const uint32_t r); + +static size_t +h2 (const void* item, const uint32_t r); + +static size_t +h3 (const void* item, const uint32_t r); + +static int +cmp (const void* keya, const void* keyb); + +static int +cmp (const void* keya, const void* keyb); + +/** + * @file + * + */ + + +//code goes here// + + + +LumieraConfigLookup +lumiera_config_lookup_init (LumieraConfigLookup self) +{ + self->hash = cuckoo_new (h1, h2, h3, cmp, sizeof (lumiera_config_lookupentry), 3, NULL, NULL); // TODO copy func, dtor + return self; +} + + +LumieraConfigLookup +lumiera_config_lookup_destroy (LumieraConfigLookup self) +{ + cuckoo_delete (self->hash); + return self; +} + + + + + + +LumieraConfigLookupentry +lumiera_config_lookupentry_new (const char* prefix, const char* name, const char* suffix) +{ + char* tmpstr = lumiera_tmpbuf_snprintf (LUMIERA_CONFIG_KEY_MAX, "%s%s%s%s%s", + prefix?prefix:"", prefix?".":"", + name?name:"", + suffix?".":"", suffix?suffix:""); + + TRACE (config_lookup, "new key %s", tmpstr); + + LumieraConfigLookupentry self = lumiera_malloc (sizeof (*self)); + + self->full_key = lumiera_strndup (tmpstr, LUMIERA_CONFIG_KEY_MAX); + llist_init (&self->configitems); + + return self; +} + +void +lumiera_config_lookupentry_delete (LumieraConfigLookupentry self) +{ + TRACE (config_lookup); + + lumiera_free (self->full_key); + ENSURE (llist_is_empty (&self->configitems)); + lumiera_free (self); +} + + + + +/* + Support functions for the cuckoo hash +*/ + +static size_t +h1 (const void* item, const uint32_t r) +{ + (void) item; + return r; +} + +static size_t +h2 (const void* item, const uint32_t r) +{ + (void) item; + return r; +} + +static size_t +h3 (const void* item, const uint32_t r) +{ + (void) item; + return r; +} + +static int +cmp (const void* keya, const void* keyb) +{ + (void) keya; + (void) keyb; + + return 0; +} + +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/ diff --git a/src/backend/config_lookup.h b/src/backend/config_lookup.h new file mode 100644 index 000000000..cb653dab8 --- /dev/null +++ b/src/backend/config_lookup.h @@ -0,0 +1,102 @@ +/* + config_lookup.h - Lookup functions for the config subsystem + + Copyright (C) Lumiera.org + 2008, Christian Thaeter + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef LUMIERA_CONFIG_LOOKUP_H +#define LUMIERA_CONFIG_LOOKUP_H + +//TODO: Support library includes// +#include "lib/cuckoo.h" +#include "lib/llist.h" + + +//TODO: Forward declarations// + + +//TODO: Lumiera header includes// + + +//TODO: System includes// +#include + + +/** + * @file + * + */ + +#define LUMIERA_CONFIG_KEY_MAX 1023 + + + +//TODO: declarations go here// + +/* + Lookup uses a cuckoo hash for now +*/ + +struct lumiera_config_lookup_struct +{ + Cuckoo hash; +}; +typedef struct lumiera_config_lookup_struct lumiera_config_lookup; +typedef lumiera_config_lookup* LumieraConfigLookup; + +LumieraConfigLookup +lumiera_config_lookup_init (LumieraConfigLookup self); + +LumieraConfigLookup +lumiera_config_lookup_destroy (LumieraConfigLookup self); + + + +/* + Lookup hash entries for the cuckoo hash +*/ + +struct lumiera_config_lookupentry_struct +{ + /* + we store a copy of the full key here + configentry keys are complete as expected + section keys are the prefix stored with a trailing dot + */ + char* full_key; + /* stack of all configitems stored under this key */ + llist configitems; +}; +typedef struct lumiera_config_lookupentry_struct lumiera_config_lookupentry; +typedef lumiera_config_lookupentry* LumieraConfigLookupentry; + +LumieraConfigLookupentry +lumiera_config_lookupentry_init (const char* prefix, const char* name, const char* suffix); + +void +lumiera_config_lookupentry_destroy (LumieraConfigLookupentry self); + + +#endif +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/