diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-05-12 09:28:25 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-05-12 09:28:25 +0100 |
commit | df902a57f916c1744c8a8a56d27cf89f7de9b885 (patch) | |
tree | 7dd46e3eae0ff87317fdd18662b1282083f327aa /src/libutil/radix.c | |
parent | 58cab14e7fd55d3a882d9f8b2448b1df7a8fcc43 (diff) | |
download | rspamd-df902a57f916c1744c8a8a56d27cf89f7de9b885.tar.gz rspamd-df902a57f916c1744c8a8a56d27cf89f7de9b885.zip |
Remove old radix code (no functional changes).
Diffstat (limited to 'src/libutil/radix.c')
-rw-r--r-- | src/libutil/radix.c | 328 |
1 files changed, 1 insertions, 327 deletions
diff --git a/src/libutil/radix.c b/src/libutil/radix.c index 17b73b6cc..605d86668 100644 --- a/src/libutil/radix.c +++ b/src/libutil/radix.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2014, Vsevolod Stakhov + * Copyright (c) 2009-2015, Vsevolod Stakhov * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,7 +28,6 @@ #include "main.h" #include "mem_pool.h" - struct radix_compressed_node { union { struct { @@ -52,331 +51,6 @@ struct radix_tree_compressed { size_t size; }; - -#ifdef LEGACY_RADIX -static void * radix_alloc (radix_tree_t * tree); - -#undef RADIX_DEBUG -#ifndef RADIX_DEBUG -#undef msg_debug -#define msg_debug(...) do {} while (0) -#endif - -struct radix_node_s { - radix_node_t *right; - radix_node_t *left; - radix_node_t *parent; - uintptr_t value; - guint32 key; -}; -struct radix_tree_s { - radix_node_t *root; - size_t size; - rspamd_mempool_t *pool; -}; - -radix_tree_t * -radix_tree_create (void) -{ - radix_tree_t *tree; - - tree = g_malloc (sizeof (radix_tree_t)); - if (tree == NULL) { - return NULL; - } - - tree->pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); - tree->size = 0; - - tree->root = radix_alloc (tree); - if (tree->root == NULL) { - return NULL; - } - - tree->root->right = NULL; - tree->root->left = NULL; - tree->root->parent = NULL; - tree->root->value = RADIX_NO_VALUE; - - return tree; -} - -static uintptr_t -radix32tree_insert_common (radix_tree_t * tree, - guint32 key, - guint32 mask, - uintptr_t value, - enum radix_insert_type type) -{ - guint32 bit; - radix_node_t *node, *next; - - bit = 0x80000000; - - node = tree->root; - next = tree->root; - /* Find a place in trie to insert */ - while (bit & mask) { - if (key & bit) { - next = node->right; - } - else { - next = node->left; - } - - if (next == NULL) { - break; - } - - bit >>= 1; - node = next; - } - - if (next) { - if (node->value != RADIX_NO_VALUE) { - /* Value was found, switch on insert type */ - switch (type) { - case RADIX_INSERT: - return 1; - case RADIX_ADD: - node->value += value; - return value; - case RADIX_REPLACE: - node->value = value; - return 1; - } - } - - node->value = value; - node->key = key; - return 0; - } - /* Inserting value in trie creating all path components */ - while (bit & mask) { - next = radix_alloc (tree); - if (next == NULL) { - return -1; - } - - next->right = NULL; - next->left = NULL; - next->parent = node; - next->value = RADIX_NO_VALUE; - - if (key & bit) { - node->right = next; - - } - else { - node->left = next; - } - - bit >>= 1; - node = next; - } - - node->value = value; - node->key = key; - - return 0; -} - -gint -radix32tree_insert (radix_tree_t *tree, - guint32 key, - guint32 mask, - uintptr_t value) -{ - return (gint)radix32tree_insert_common (tree, key, mask, value, - RADIX_INSERT); -} - -uintptr_t -radix32tree_add (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value) -{ - return radix32tree_insert_common (tree, key, mask, value, RADIX_ADD); -} - -gint -radix32tree_replace (radix_tree_t *tree, - guint32 key, - guint32 mask, - uintptr_t value) -{ - return (gint)radix32tree_insert_common (tree, - key, - mask, - value, - RADIX_REPLACE); -} - -/* - * per recursion step: - * ptr + ptr + ptr + gint = 4 words - * result = 1 word - * 5 words total in stack - */ -static gboolean -radix_recurse_nodes (radix_node_t *node, - radix_tree_traverse_func func, - void *user_data, - gint level) -{ - if (node->left) { - if (radix_recurse_nodes (node->left, func, user_data, level + 1)) { - return TRUE; - } - } - - if (node->value != RADIX_NO_VALUE) { - if (func (node->key, level, node->value, user_data)) { - return TRUE; - } - } - - if (node->right) { - if (radix_recurse_nodes (node->right, func, user_data, level + 1)) { - return TRUE; - } - } - - return FALSE; -} - -void -radix32tree_traverse (radix_tree_t *tree, - radix_tree_traverse_func func, - void *user_data) -{ - radix_recurse_nodes (tree->root, func, user_data, 0); -} - - -gint -radix32tree_delete (radix_tree_t * tree, guint32 key, guint32 mask) -{ - guint32 bit; - radix_node_t *node; - - bit = 0x80000000; - node = tree->root; - - while (node && (bit & mask)) { - if (key & bit) { - node = node->right; - - } - else { - node = node->left; - } - - bit >>= 1; - } - - if (node == NULL || node->parent == NULL) { - return -1; - } - - if (node->right || node->left) { - if (node->value != RADIX_NO_VALUE) { - node->value = RADIX_NO_VALUE; - return 0; - } - - return -1; - } - - for (;; ) { - if (node->parent->right == node) { - node->parent->right = NULL; - - } - else { - node->parent->left = NULL; - } - - node = node->parent; - - if (node->right || node->left) { - break; - } - - if (node->value != RADIX_NO_VALUE) { - break; - } - - if (node->parent == NULL) { - break; - } - } - - return 0; -} - - -uintptr_t -radix32tree_find (radix_tree_t * tree, guint32 key) -{ - guint32 bit; - uintptr_t value; - radix_node_t *node; - - bit = 0x80000000; - value = RADIX_NO_VALUE; - node = tree->root; - - while (node) { - if (node->value != RADIX_NO_VALUE) { - value = node->value; - } - - if (key & bit) { - node = node->right; - - } - else { - node = node->left; - } - - bit >>= 1; - } - - return value; -} - - -static void * -radix_alloc (radix_tree_t * tree) -{ - gchar *p; - - p = rspamd_mempool_alloc (tree->pool, sizeof (radix_node_t)); - - tree->size += sizeof (radix_node_t); - - return p; -} - -void -radix_tree_free (radix_tree_t * tree) -{ - - g_return_if_fail (tree != NULL); - rspamd_mempool_delete (tree->pool); - g_free (tree); -} - -uintptr_t -radix32_tree_find_addr (radix_tree_t *tree, rspamd_inet_addr_t *addr) -{ - if (addr == NULL || addr->af != AF_INET) { - return RADIX_NO_VALUE; - } - - return radix32tree_find (tree, ntohl (addr->addr.s4.sin_addr.s_addr)); -} -#endif /* Old radix code */ - static gboolean radix_compare_compressed (struct radix_compressed_node *node, guint8 *key, guint keylen, guint cur_level) |