+
+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
+ */
+
+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 = 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, &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, 0);
+}