From 9c02ad80dbfeaa3d2ee7cdc7e26ebf25ff872a6c Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Thu, 11 Sep 2008 13:19:00 +0200 Subject: [PATCH] Add shrink and memblock. Does not support duplicates anymore. Signed-off-by: Florent Bruneau --- common/mem.h | 8 ++++++++ common/trie.c | 32 +++++++++++++++++++++++++------- common/tst-trie.c | 10 +++++++--- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/common/mem.h b/common/mem.h index 44630ef..f7b9d80 100644 --- a/common/mem.h +++ b/common/mem.h @@ -53,6 +53,14 @@ #define p_dupstr(p, len) xmemdupstr((p), (len)) #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count)) +# define p_shrink(pp, goalnb, allocnb) \ + do { \ + if (*(allocnb) > goalnb) { \ + p_realloc(pp, goalnb); \ + *(allocnb) = goalnb; \ + } \ + } while(0) + # define p_alloc_nr(x) (((x) + 16) * 3 / 2) # define p_allocgrow(pp, goalnb, allocnb) \ do { \ diff --git a/common/trie.c b/common/trie.c index 030ff3a..2e3ada1 100644 --- a/common/trie.c +++ b/common/trie.c @@ -33,6 +33,8 @@ * Copyright © 2008 Florent Bruneau */ +#include + #include "str.h" #include "trie.h" @@ -67,9 +69,19 @@ trie_t *trie_new(void) return p_new(trie_t, 1); } +static inline void trie_cleanup_build_data(trie_t *trie) +{ + for (int i = 0 ; i < trie->keys_len ; ++i) { + p_delete(&trie->keys[i]); + } + p_delete(&trie->keys); + trie->keys_len = trie->keys_size = 0; +} + void trie_delete(trie_t **trie) { if (*trie) { + trie_cleanup_build_data(*trie); p_delete(&(*trie)->entries); p_delete(&(*trie)->c); p_delete(trie); @@ -259,20 +271,25 @@ static inline void trie_compile_aux(trie_t *trie, int id, } } -static inline void trie_cleanup_build_data(trie_t *trie) -{ - for (int i = 0 ; i < trie->keys_len ; ++i) { - p_delete(&trie->keys[i]); - } - p_delete(&trie->keys); -} static inline void trie_shrink(trie_t *trie) { + p_shrink(&trie->entries, trie->entries_len, &trie->entries_size); + p_shrink(&trie->c, trie->c_len, &trie->c_size); } static inline void trie_lock(trie_t *trie) { + if (mlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len) != 0) { + UNIXERR("mlock"); + return; + } + if (mlock(trie->c, trie->c_len) != 0) { + UNIXERR("mlock"); + munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len); + return; + } + trie->locked = true; } typedef char *str_t; @@ -339,6 +356,7 @@ static inline void trie_entry_inspect(const trie_t *trie, fputs("(nil)", stdout); } else { const char *c = trie->c + entry->c_offset; + printf("(%d) ", entry->c_len); for (int i = 0 ; i < entry->c_len ; ++i) { if (c[i]) { printf("%c ", c[i]); diff --git a/common/tst-trie.c b/common/tst-trie.c index 67eba9b..5e09079 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -117,7 +117,6 @@ int main(int argc, char *argv[]) trie_t *trie = trie_new(); trie_insert(trie, "abcde123456789"); trie_insert(trie, "abcde123654789"); - trie_insert(trie, "abcde123654789"); trie_insert(trie, "abcdefghi"); trie_insert(trie, "coucou"); trie_insert(trie, "coucou chez vous"); @@ -139,16 +138,21 @@ int main(int argc, char *argv[]) ASSERT_FALSE("coucou "); ASSERT_FALSE("abcde123"); ASSERT_FALSE("abcde"); + ASSERT_FALSE("coucou chez vous tous"); + ASSERT_TRUE("abcde123456789"); + ASSERT_TRUE("abcde123654789"); ASSERT_TRUE("abcdefghi"); ASSERT_TRUE("coucou"); - ASSERT_FALSE("coucou chez vous tous"); + ASSERT_TRUE("coucou chez vous"); + ASSERT_TRUE("debout !"); + trie_delete(&trie); /* Perf test */ if (argc > 1) { trie = create_trie_from_file(argv[1]); - trie_inspect(trie); +// trie_inspect(trie); trie_delete(&trie); } return 0; -- 2.20.1