+
+void trie_lock(trie_t *trie)
+{
+ if (trie->locked) {
+ return;
+ }
+ if (!array_lock(trie->entries)) {
+ UNIXERR("mlock");
+ return;
+ }
+ if (!array_lock(trie->c)) {
+ UNIXERR("mlock");
+ array_unlock(trie->entries);
+ return;
+ }
+ trie->locked = true;
+}
+
+void trie_unlock(trie_t *trie)
+{
+ if (!trie->locked) {
+ return;
+ }
+ array_unlock(trie->entries);
+ array_unlock(trie->entries);
+ trie->locked = false;
+}
+
+/* Debug {{{1
+ */
+
+static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
+ const trie_entry_t *entry, int level)
+{
+ static int max_depth = 0;
+ static int leaves = 0;
+ static int depth_sum = 0;
+
+ if (trie_entry_is_leaf(entry)) {
+ if (level > max_depth) {
+ max_depth = level;
+ }
+ ++leaves;
+ depth_sum += level;
+ }
+ if (show_content) {
+ for (int i = 0 ; i < level ; ++i) {
+ fputs(" ", stdout);
+ }
+ if (entry->c_len == 0) {
+ fputs("(nil)", stdout);
+ } else {
+ 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]) {
+ printf("%c ", c[i]);
+ } else {
+ fputs("\\0 ", stdout);
+ }
+ }
+ }
+ fputs("\n", stdout);
+ }
+ for (int i = entry->children_offset ;
+ i < entry->children_offset + entry->children_len ; ++i) {
+ 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("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));
+ }
+}
+
+void trie_inspect(const trie_t *trie, bool show_content)
+{
+ trie_entry_inspect(trie, show_content, trie->entries.data, 0);
+}