From 4694ac125f0dd752126de3f3f4e81b406cdd98e6 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Thu, 11 Sep 2008 14:42:50 +0200 Subject: [PATCH] Publish trie_lock and trie_unlock. Don't forget to unlock before deletion. Signed-off-by: Florent Bruneau --- common/trie.c | 51 +++++++++++++++++++++++++++-------------------- common/trie.h | 6 ++++++ common/tst-trie.c | 1 + 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/common/trie.c b/common/trie.c index 9a04cf4..56e9810 100644 --- a/common/trie.c +++ b/common/trie.c @@ -82,6 +82,7 @@ void trie_delete(trie_t **trie) { if (*trie) { trie_cleanup_build_data(*trie); + trie_unlock(*trie); p_delete(&(*trie)->entries); p_delete(&(*trie)->c); p_delete(trie); @@ -274,27 +275,6 @@ static inline void trie_compile_aux(trie_t *trie, int id, } } - -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; void trie_compile(trie_t *trie, bool memlock) @@ -313,7 +293,8 @@ void trie_compile(trie_t *trie, bool memlock) trie_compile_aux(trie, trie_add_leaf(trie, trie->keys[0]), 0, trie->keys_len, 0, 0); trie_cleanup_build_data(trie); - trie_shrink(trie); + p_shrink(&trie->entries, trie->entries_len, &trie->entries_size); + p_shrink(&trie->c, trie->c_len, &trie->c_size); if (memlock) { trie_lock(trie); } @@ -343,6 +324,32 @@ bool trie_lookup(const trie_t *trie, const char *key) } } +void trie_lock(trie_t *trie) +{ + if (trie->locked) { + return; + } + 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; +} + +void trie_unlock(trie_t *trie) +{ + if (!trie->locked) { + return; + } + munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len); + munlock(trie->c, trie->c_len); + trie->locked = false; +} /* Debug {{{1 */ diff --git a/common/trie.h b/common/trie.h index c00a4af..96fa75d 100644 --- a/common/trie.h +++ b/common/trie.h @@ -49,6 +49,12 @@ void trie_insert(trie_t *trie, const char* key); __attribute__((nonnull(1))) void trie_compile(trie_t *trie, bool memlock); +__attribute__((nonnull(1))) +void trie_lock(trie_t *trie); + +__attribute__((nonnull(1))) +void trie_unlock(trie_t *trie); + __attribute__((nonnull(1,2))) bool trie_lookup(const trie_t *trie, const char* key); diff --git a/common/tst-trie.c b/common/tst-trie.c index 25f31c5..cc8c8f3 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -151,6 +151,7 @@ int main(int argc, char *argv[]) */ if (argc > 1) { trie = create_trie_from_file(argv[1]); + trie_lock(trie); trie_inspect(trie, false); trie_delete(&trie); } -- 2.20.1