From d3e9745e0c0fa4f18bdcc8464fbd1cd00559b5e9 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Mon, 21 Nov 2016 16:18:49 +0000 Subject: [PATCH] Hcache backends list --- hcache-backend.h | 22 ++++++++-------- hcache-kc.c | 14 +++++----- hcache-tc.c | 14 +++++----- hcache.c | 66 +++++++++++++++++++++++++++++------------------- version.c | 4 ++- 5 files changed, 69 insertions(+), 51 deletions(-) diff --git a/hcache-backend.h b/hcache-backend.h index 70e5d11ee..ce858b1e9 100644 --- a/hcache-backend.h +++ b/hcache-backend.h @@ -91,6 +91,7 @@ typedef const char *(*hcache_backend_t)(void); typedef struct { + const char * name; hcache_open_t open; hcache_fetch_t fetch; hcache_store_t store; @@ -102,19 +103,20 @@ typedef struct #define HCACHE_BACKEND_LIST \ HCACHE_BACKEND(bdb) \ HCACHE_BACKEND(gdbm) \ - HCACHE_BACKEND(kc) \ + HCACHE_BACKEND(kyotocabinet) \ HCACHE_BACKEND(lmdb) \ HCACHE_BACKEND(qdbm) \ - HCACHE_BACKEND(tc) + HCACHE_BACKEND(tokyocabinet) -#define HCACHE_BACKEND_OPS(name) \ - const hcache_ops_t hcache_##name##_ops = { \ - .open = hcache_##name##_open, \ - .fetch = hcache_##name##_fetch, \ - .store = hcache_##name##_store, \ - .delete = hcache_##name##_delete, \ - .close = hcache_##name##_close, \ - .backend = hcache_##name##_backend \ +#define HCACHE_BACKEND_OPS(_name) \ + const hcache_ops_t hcache_##_name##_ops = { \ + .name = #_name, \ + .open = hcache_##_name##_open, \ + .fetch = hcache_##_name##_fetch, \ + .store = hcache_##_name##_store, \ + .delete = hcache_##_name##_delete, \ + .close = hcache_##_name##_close, \ + .backend = hcache_##_name##_backend \ }; #endif /* _HCACHE_BACKEND_H_ */ diff --git a/hcache-kc.c b/hcache-kc.c index 1d031115a..e437f03bf 100644 --- a/hcache-kc.c +++ b/hcache-kc.c @@ -28,7 +28,7 @@ #include static void * -hcache_kc_open(const char *path) +hcache_kyotocabinet_open(const char *path) { char kcdbpath[_POSIX_PATH_MAX]; int printfresult; @@ -59,7 +59,7 @@ hcache_kc_open(const char *path) } static void * -hcache_kc_fetch(void *ctx, const char *key, size_t keylen) +hcache_kyotocabinet_fetch(void *ctx, const char *key, size_t keylen) { size_t sp; @@ -71,7 +71,7 @@ hcache_kc_fetch(void *ctx, const char *key, size_t keylen) } static int -hcache_kc_store(void *ctx, const char* key, size_t keylen, void *data, size_t dlen) +hcache_kyotocabinet_store(void *ctx, const char* key, size_t keylen, void *data, size_t dlen) { if (!ctx) return -1; @@ -81,7 +81,7 @@ hcache_kc_store(void *ctx, const char* key, size_t keylen, void *data, size_t dl } static int -hcache_kc_delete(void *ctx, const char *key, size_t keylen) +hcache_kyotocabinet_delete(void *ctx, const char *key, size_t keylen) { if (!ctx) return -1; @@ -91,7 +91,7 @@ hcache_kc_delete(void *ctx, const char *key, size_t keylen) } static void -hcache_kc_close(void **ctx) +hcache_kyotocabinet_close(void **ctx) { if (!ctx || !*ctx) return; @@ -108,7 +108,7 @@ hcache_kc_close(void **ctx) } static const char * -hcache_kc_backend(void) +hcache_kyotocabinet_backend(void) { /* SHORT_STRING(128) should be more than enough for KCVERSION */ static char version_cache[SHORT_STRING] = ""; @@ -118,6 +118,6 @@ hcache_kc_backend(void) return version_cache; } -HCACHE_BACKEND_OPS(kc) +HCACHE_BACKEND_OPS(kyotocabinet) #endif /* HAVE_KC */ diff --git a/hcache-tc.c b/hcache-tc.c index ec8503dc6..27443612f 100644 --- a/hcache-tc.c +++ b/hcache-tc.c @@ -28,7 +28,7 @@ #include static void * -hcache_tc_open(const char *path) +hcache_tokyocabinet_open(const char *path) { TCBDB *db = tcbdbnew(); if (!db) @@ -49,7 +49,7 @@ hcache_tc_open(const char *path) } static void * -hcache_tc_fetch(void *ctx, const char *key, size_t keylen) +hcache_tokyocabinet_fetch(void *ctx, const char *key, size_t keylen) { int sp; @@ -61,7 +61,7 @@ hcache_tc_fetch(void *ctx, const char *key, size_t keylen) } static int -hcache_tc_store(void *ctx, const char *key, size_t keylen, void *data, size_t dlen) +hcache_tokyocabinet_store(void *ctx, const char *key, size_t keylen, void *data, size_t dlen) { if (!ctx) return -1; @@ -71,7 +71,7 @@ hcache_tc_store(void *ctx, const char *key, size_t keylen, void *data, size_t dl } static int -hcache_tc_delete(void *ctx, const char *key, size_t keylen) +hcache_tokyocabinet_delete(void *ctx, const char *key, size_t keylen) { if (!ctx) return -1; @@ -81,7 +81,7 @@ hcache_tc_delete(void *ctx, const char *key, size_t keylen) } static void -hcache_tc_close(void **ctx) +hcache_tokyocabinet_close(void **ctx) { if (!ctx || !*ctx) return; @@ -98,11 +98,11 @@ hcache_tc_close(void **ctx) } static const char * -hcache_tc_backend(void) +hcache_tokyocabinet_backend(void) { return "tokyocabinet " _TC_VERSION; } -HCACHE_BACKEND_OPS(tc) +HCACHE_BACKEND_OPS(tokyocabinet) #endif /* HAVE_TC */ diff --git a/hcache.c b/hcache.c index 1751f5f4c..dca6e0c3d 100644 --- a/hcache.c +++ b/hcache.c @@ -63,41 +63,46 @@ typedef union HCACHE_BACKEND_LIST #undef HCACHE_BACKEND -static const hcache_ops_t * -hcache_get_backend_ops(const char *backend) -{ - const char *b = NONULL(backend); - - // Keep this list sorted as it is in configure.ac to avoid user surprise if - // no header_cache_backend is specified. -#define EMPTY_OR_EQ(s1, s2) (!strlen(s1) || !strcmp((s1), (s2))) - +/* Keep this list sorted as it is in configure.ac to avoid user surprise if no + * header_cache_backend is specified. */ +const hcache_ops_t *hcache_ops[] = { #if defined(HAVE_TC) - if (EMPTY_OR_EQ(b, "tokyocabinet")) - return &hcache_tc_ops; + &hcache_tokyocabinet_ops, #endif #if defined(HAVE_KC) - if (EMPTY_OR_EQ(b, "kyotocabinet")) - return &hcache_kc_ops; + &hcache_kyotocabinet_ops, #endif #if defined(HAVE_QDBM) - if (EMPTY_OR_EQ(b, "qdbm")) - return &hcache_qdbm_ops; + &hcache_qdbm_ops, #endif #if defined(HAVE_GDBM) - if (EMPTY_OR_EQ(b, "gdbm")) - return &hcache_gdbm_ops; + &hcache_gdbm_ops, #endif #if defined(HAVE_BDB) - if (EMPTY_OR_EQ(b, "bdb")) - return &hcache_bdb_ops; + &hcache_bdb_ops, #endif #if defined(HAVE_LMDB) - if (EMPTY_OR_EQ(b, "lmdb")) - return &hcache_lmdb_ops; + &hcache_lmdb_ops, #endif + NULL +}; + +static const hcache_ops_t * +hcache_get_backend_ops(const char *backend) +{ + const char *b = NONULL(backend); + if (!b || !*b) + { + return hcache_ops[0]; + } -#undef EMPTY_OR_EQ + for (size_t i = 0; i < sizeof(hcache_ops)/sizeof(*hcache_ops) - 1; ++i) + { + if (!strcmp(b, hcache_ops[i]->name)) + { + return hcache_ops[i]; + } + } return NULL; } @@ -902,10 +907,19 @@ mutt_hcache_delete(header_cache_t *h, const char *key, size_t keylen) const char * mutt_hcache_backend() { - const hcache_ops_t *ops = hcache_get_ops(); - if (!ops) - return NULL; - return ops->backend(); + char tmp[STRING] = {0}; + size_t len = 0; + + for (size_t i = 0; i < sizeof(hcache_ops)/sizeof(*hcache_ops) - 1; ++i) + { + if (len != 0) + { + len += snprintf(tmp+len, STRING-len, ", "); + } + len += snprintf(tmp+len, STRING-len, "%s", hcache_ops[i]->name); + } + + return strdup(tmp); } int diff --git a/version.c b/version.c index fd3d12f9d..ae5e2a091 100644 --- a/version.c +++ b/version.c @@ -428,7 +428,9 @@ print_version (void) #endif #ifdef USE_HCACHE - printf ("\nhcache backend: %s", mutt_hcache_backend()); + const char *backends = mutt_hcache_backend(); + printf ("\nhcache backends: %s", backends); + FREE(&backends); #endif puts ("\n\nCompiler:"); -- 2.40.0