From d66ec885e2c3680b1b42e5cf99b18865c6c1736c Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Fri, 12 Sep 2008 22:04:40 +0200 Subject: [PATCH] Add array framework that works with any type. Signed-off-by: Florent Bruneau --- common/array.h | 113 ++++++++++++++++++++++++++++++++++++ common/buffer.h | 7 +-- common/trie.c | 151 +++++++++++++++++++----------------------------- 3 files changed, 174 insertions(+), 97 deletions(-) create mode 100644 common/array.h diff --git a/common/array.h b/common/array.h new file mode 100644 index 0000000..b85fc50 --- /dev/null +++ b/common/array.h @@ -0,0 +1,113 @@ +/******************************************************************************/ +/* pfixtools: a collection of postfix related tools */ +/* ~~~~~~~~~ */ +/* ________________________________________________________________________ */ +/* */ +/* Redistribution and use in source and binary forms, with or without */ +/* modification, are permitted provided that the following conditions */ +/* are met: */ +/* */ +/* 1. Redistributions of source code must retain the above copyright */ +/* notice, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright */ +/* notice, this list of conditions and the following disclaimer in the */ +/* documentation and/or other materials provided with the distribution. */ +/* 3. The names of its contributors may not be used to endorse or promote */ +/* products derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */ +/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */ +/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ +/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */ +/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */ +/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */ +/* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */ +/* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */ +/* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */ +/* THE POSSIBILITY OF SUCH DAMAGE. */ +/******************************************************************************/ + +/* + * Copyright © 2008 Florent Bruneau + */ + +#ifndef PFIXTOOLS_ARRAY_H +#define PFIXTOOLS_ARRAY_H + +#include "common.h" +#include "mem.h" + +#define PRIV_ARRAY(Type) \ + struct { \ + Type *data; \ + ssize_t len; \ + ssize_t size; \ + } +#define ARRAY(Type) \ + typedef PRIV_ARRAY(Type) Type ## _array_t; \ + \ + static inline Type ## _array_t *Type ## _array_new(void) \ + { \ + return p_new(Type ## _array_t, 1); \ + } \ + \ + static inline void Type ## _array_delete(Type ## _array_t **array) \ + { \ + if (*array) { \ + array_wipe(**array); \ + p_delete(array); \ + } \ + } + +#define ARRAY_INIT { NULL, 0, 0 } + +#define array_init(array) (array) = ARRAY_INIT +#define array_wipe(array) \ + do { \ + p_delete(&(array).data); \ + (array).len = 0; \ + (array).size = 0; \ + } while (0) +#define array_add(array, obj) \ + do { \ + array_ensure_capacity_delta(array, 1); \ + (array).data[(array).len++] = (obj); \ + } while (0) +#define array_append(array, objs, len) \ + do { \ + const ssize_t __len = (len); \ + array_ensure_capacity_delta(array, __len); \ + memcpy((array).data + (array).len, objs, \ + __len * sizeof(*(array).data)); \ + (array).len += __len; \ + } while (0) +#define array_ensure_capacity(array, goal) \ + do { \ + if ((array).size < (goal)) { \ + const ssize_t required_size = (goal); \ + ssize_t next_size = (array).size; \ + do { \ + next_size = p_alloc_nr(next_size); \ + } while (next_size < required_size); \ + p_allocgrow(&(array).data, next_size, &(array).size); \ + } \ + } while (0) +#define array_ensure_capacity_delta(array, delta) \ + array_ensure_capacity(array, (array).len + (delta)) +#define array_ensure_exact_capacity(array, goal) \ + if (array_size(array) < (goal)) { \ + p_allocgrow(&(array).data, (goal), &(array).size); \ + } +#define array_adjust(array) \ + do { \ + p_shrink(&(array).data, (array).len, &(array).size); \ + } while (0) +#define array_elt(array, n) (array).data[(n)] +#define array_ptr(array, n) (array).data + (n) + +ARRAY(char) +ARRAY(int) + +#endif diff --git a/common/buffer.h b/common/buffer.h index 24bc590..d044114 100644 --- a/common/buffer.h +++ b/common/buffer.h @@ -39,12 +39,9 @@ #include #include "mem.h" #include "str.h" +#include "array.h" -typedef struct buffer_t { - char *data; - ssize_t len; - ssize_t size; -} buffer_t; +typedef char_array_t buffer_t; #define BUFFER_INIT {NULL, 0, 0} diff --git a/common/trie.c b/common/trie.c index 124c072..9ffa9b5 100644 --- a/common/trie.c +++ b/common/trie.c @@ -35,6 +35,7 @@ #include +#include "array.h" #include "str.h" #include "trie.h" @@ -47,23 +48,14 @@ struct trie_entry_t { int32_t children_offset; int32_t children_len; }; +#define TRIE_ENTRY_INIT { 0, 0, 0, 0 } +ARRAY(trie_entry_t) struct trie_t { - trie_entry_t *entries; - int32_t entries_len; - int32_t entries_size; - - char *c; - int32_t c_len; - int32_t c_size; - - char *keys; - int32_t keys_len; - int32_t keys_size; - - int *keys_offset; - int32_t keys_offset_len; - int32_t keys_offset_size; + trie_entry_t_array_t entries; + char_array_t c; + char_array_t keys; + int_array_t keys_offset; bool locked; }; @@ -75,10 +67,8 @@ trie_t *trie_new(void) static inline void trie_cleanup_build_data(trie_t *trie) { - p_delete(&trie->keys); - p_delete(&trie->keys_offset); - trie->keys_len = trie->keys_size = 0; - trie->keys_offset_len = trie->keys_offset_size = 0; + array_wipe(trie->keys); + array_wipe(trie->keys_offset); } void trie_delete(trie_t **trie) @@ -86,8 +76,8 @@ void trie_delete(trie_t **trie) if (*trie) { trie_cleanup_build_data(*trie); trie_unlock(*trie); - p_delete(&(*trie)->entries); - p_delete(&(*trie)->c); + array_wipe((*trie)->entries); + array_wipe((*trie)->c); p_delete(trie); } } @@ -98,7 +88,7 @@ static inline bool trie_entry_c_match(const trie_t *trie, const trie_entry_t *entry, const char *key, int *pos) { - const char *c = trie->c + entry->c_offset; + const char *c = array_ptr(trie->c, entry->c_offset); int i = 0; for (i = 0 ; i < entry->c_len ; ++i) { if (key[i] != c[i]) { @@ -117,7 +107,7 @@ static inline bool trie_entry_c_match(const trie_t *trie, static inline bool trie_entry_match(const trie_t *trie, const trie_entry_t *entry, const char *key) { - return !!(strcmp(trie->c + entry->c_offset, key) == 0); + return !!(strcmp(array_ptr(trie->c, entry->c_offset), key) == 0); } static inline bool trie_entry_is_leaf(const trie_entry_t *entry) @@ -139,8 +129,8 @@ static inline trie_entry_t* trie_entry_child(const trie_t *trie, while (start < end) { int mid = (start + end) / 2; - trie_entry_t* child = &trie->entries[mid]; - const char c2 = trie->c[child->c_offset]; + trie_entry_t* child = array_ptr(trie->entries, mid); + const char c2 = array_elt(trie->c, child->c_offset); if (child->c_len) { if (c2 == c) { @@ -158,41 +148,22 @@ static inline trie_entry_t* trie_entry_child(const trie_t *trie, return NULL; } -#define GROW(Buffer, Added, Len, Size) \ - do { \ - const int required_size = (Len) + (Added); \ - int next_size = (Size); \ - if (next_size >= required_size) { \ - break; \ - } \ - do { \ - next_size = p_alloc_nr(next_size); \ - } while (next_size < required_size); \ - p_allocgrow(&(Buffer), next_size, &(Size)); \ - } while(0) - -static inline void trie_grow(trie_t *trie, int delta) -{ - GROW(trie->entries, delta, trie->entries_len, trie->entries_size); -} - static inline int trie_entry_new(trie_t *trie) { - memset(trie->entries + trie->entries_len, 0, sizeof(trie_entry_t)); - return trie->entries_len++; + const trie_entry_t e = TRIE_ENTRY_INIT; + array_add(trie->entries, e); + return trie->entries.len; } static inline int trie_add_leaf(trie_t *trie, const char *key) { trie_entry_t *entry; int len = m_strlen(key) + 1; - entry = &trie->entries[trie_entry_new(trie)]; - GROW(trie->c, len, trie->c_len, trie->c_size); - memcpy(trie->c + trie->c_len, key, len); - entry->c_offset = trie->c_len; + entry = array_ptr(trie->entries, trie_entry_new(trie)); + entry->c_offset = trie->c.len; entry->c_len = len; - trie->c_len += len; - return trie->entries_len - 1; + array_append(trie->c, key, len); + return trie->entries.len - 1; } static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry, @@ -214,7 +185,7 @@ static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry, static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos) { trie_entry_t *child; - child = &trie->entries[trie_entry_new(trie)]; + child = array_ptr(trie->entries, trie_entry_new(trie)); if (pos == 0) { child->c_offset = entry->c_offset; child->c_len = entry->c_len; @@ -227,21 +198,17 @@ static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos) } child->children_offset = entry->children_offset; child->children_len = entry->children_len; - entry->children_offset = trie->entries_len - 1; + entry->children_offset = trie->entries.len - 1; entry->children_len = 1; } void trie_insert(trie_t *trie, const char* key) { - assert(trie->entries == NULL && "Trie already compiled"); + assert(trie->entries.len == 0 && "Trie already compiled"); int len = m_strlen(key) + 1; - GROW(trie->keys, len, trie->keys_len, trie->keys_size); - memcpy(trie->keys + trie->keys_len, key, len); - - GROW(trie->keys_offset, 1, trie->keys_offset_len, trie->keys_offset_size); - trie->keys_offset[trie->keys_offset_len++] = trie->keys_len; - trie->keys_len += len; + array_add(trie->keys_offset, trie->keys.len); + array_append(trie->keys, key, len); } @@ -254,16 +221,16 @@ static inline void trie_compile_aux(trie_t *trie, int id, char current = '\0'; for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) { - current = trie->keys[trie->keys_offset[first_key] + offset]; + current = array_elt(trie->keys, array_elt(trie->keys_offset, first_key) + offset); for (int i = first_key + 1 ; i < last_key ; ++i) { - const char *str = trie->keys + trie->keys_offset[i]; + const char *str = array_ptr(trie->keys, array_elt(trie->keys_offset, i)); const char c = str[offset]; if (c != current) { - trie_grow(trie, 2); + array_ensure_capacity_delta(trie->entries, 2); if (fork_pos == 0) { - trie_entry_split(trie, &trie->entries[id], off_diff); + trie_entry_split(trie, array_ptr(trie->entries, id), off_diff); } - trie_entry_insert_child(trie, &trie->entries[id], + trie_entry_insert_child(trie, array_ptr(trie->entries, id), trie_add_leaf(trie, str + offset)); forks[fork_pos++] = i; current = c; @@ -275,9 +242,9 @@ static inline void trie_compile_aux(trie_t *trie, int id, } forks[fork_pos] = last_key; - const int children_len = trie->entries[id].children_len; + const int children_len = array_elt(trie->entries, id).children_len; for (int i = 0 ; i < children_len ; ++i) { - int child = trie->entries[id].children_offset + i; + int child = array_elt(trie->entries, id).children_offset + i; if (forks[i] - 1 > first_key) { trie_compile_aux(trie, child, first_key, forks[i], offset, 1); } @@ -287,22 +254,22 @@ static inline void trie_compile_aux(trie_t *trie, int id, void trie_compile(trie_t *trie, bool memlock) { - assert(trie->entries == NULL && "Trie already compiled"); - assert(trie->keys != NULL && "Trying to compile an empty trie"); + assert(trie->entries.len == 0 && "Trie already compiled"); + assert(trie->keys.len != 0 && "Trying to compile an empty trie"); { # define QSORT_TYPE int -# define QSORT_BASE trie->keys_offset -# define QSORT_NELT trie->keys_offset_len -# define QSORT_LT(a,b) strcmp(trie->keys + *a, trie->keys + *b) < 0 +# define QSORT_BASE trie->keys_offset.data +# define QSORT_NELT trie->keys_offset.len +# define QSORT_LT(a,b) strcmp(trie->keys.data + *a, trie->keys.data + *b) < 0 # include "qsort.c" } - trie_grow(trie, trie->keys_len); - trie_compile_aux(trie, trie_add_leaf(trie, trie->keys), - 0, trie->keys_offset_len, 0, 0); + array_ensure_capacity(trie->entries, trie->keys.len); + trie_compile_aux(trie, trie_add_leaf(trie, trie->keys.data), + 0, trie->keys_offset.len, 0, 0); trie_cleanup_build_data(trie); - p_shrink(&trie->entries, trie->entries_len, &trie->entries_size); - p_shrink(&trie->c, trie->c_len, &trie->c_size); + array_adjust(trie->entries); + array_adjust(trie->c); if (memlock) { trie_lock(trie); } @@ -310,11 +277,11 @@ void trie_compile(trie_t *trie, bool memlock) bool trie_lookup(const trie_t *trie, const char *key) { - assert(trie->keys == NULL && "Can't lookup: trie not compiled"); - if (trie->entries_len == 0) { + assert(trie->keys.len == 0L && "Can't lookup: trie not compiled"); + if (trie->entries.len == 0) { return false; } else { - trie_entry_t *current = trie->entries; + trie_entry_t *current = array_ptr(trie->entries, 0); while (true) { int pos = 0; if (trie_entry_is_leaf(current)) { @@ -337,13 +304,13 @@ void trie_lock(trie_t *trie) if (trie->locked) { return; } - if (mlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len) != 0) { + if (mlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len) != 0) { UNIXERR("mlock"); return; } - if (mlock(trie->c, trie->c_len) != 0) { + if (mlock(trie->c.data, trie->c.len) != 0) { UNIXERR("mlock"); - munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len); + munlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len); return; } trie->locked = true; @@ -354,8 +321,8 @@ 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); + munlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len); + munlock(trie->c.data, trie->c.len); trie->locked = false; } @@ -383,7 +350,7 @@ static inline void trie_entry_inspect(const trie_t *trie, bool show_content, if (entry->c_len == 0) { fputs("(nil)", stdout); } else { - const char *c = trie->c + entry->c_offset; + const char *c = array_ptr(trie->c, entry->c_offset); printf("(%d) ", entry->c_len); for (int i = 0 ; i < entry->c_len ; ++i) { if (c[i]) { @@ -397,20 +364,20 @@ static inline void trie_entry_inspect(const trie_t *trie, bool show_content, } for (int i = entry->children_offset ; i < entry->children_offset + entry->children_len ; ++i) { - trie_entry_inspect(trie, show_content, &trie->entries[i], level + 1); + trie_entry_inspect(trie, show_content, array_ptr(trie->entries, i), level + 1); } if (level == 0) { - printf("Average char per node: %d\n", trie->c_len / trie->entries_len); - printf("Number of nodes: %d\n", trie->entries_len); + printf("Average char per node: %d\n", trie->c.len / trie->entries.len); + printf("Number of nodes: %d\n", trie->entries.len); printf("Number of leaves: %d\n", leaves); printf("Max depth: %d\n", max_depth); printf("Average leaf depth: %d\n", depth_sum / leaves); - printf("Memory used: %d\n", (trie->entries_size * sizeof(trie_entry_t)) - + (trie->c_size) + sizeof(trie_t)); + printf("Memory used: %d\n", (trie->entries.size * sizeof(trie_entry_t)) + + (trie->c.size) + sizeof(trie_t)); } } void trie_inspect(const trie_t *trie, bool show_content) { - trie_entry_inspect(trie, show_content, trie->entries, 0); + trie_entry_inspect(trie, show_content, trie->entries.data, 0); } -- 2.20.1