/* * Adds signature. We're using a circular buffer. If we need to overwrite * an old signature, we need to evict it from the AVL tree. * */ void dd_add_sig(sig_t sig) { int i; sig_t sig_cpy = NULL; unsigned int b = sbuf_first; unsigned int e = sbuf_last < sbuf_first ? sbuf_last + SBUF_SIZE : sbuf_last; if ((e - b + 1) == SBUF_SIZE) { // Circular buffer is full, remove corresponding first element // of circular buffer from AVL tree. sig_cpy = (sig_t) AVL_delete( (const Element*) sig, sigtree ); sbuf_first = (++sbuf_first) % SBUF_SIZE; } // Add to circular buffer sbuf[sbuf_last] = sig; sbuf_last = (++sbuf_last) % SBUF_SIZE; // Make a copy of sig and add to AVL tree //dd_printf("ADD_SIG: "); //for (i = 0; i < SHA_DIGEST_LENGTH; ++i) dd_printf("%02x", sig[i]); //dd_printf("\n"); if (sig_cpy == NULL) sig_cpy = (sig_t) malloc( sizeof(unsigned char) * SHA_DIGEST_LENGTH ); memcpy( (void*)sig_cpy, (void*)sig, sizeof(unsigned char) * SHA_DIGEST_LENGTH ); AVL_insert( (const Element*)sig_cpy, sigtree ); }
AvlTree AVL_insert( u_long key, uint16_t port, struct pbsnode *node, AvlTree tree ) { if (tree == NULL) { /* Create and return a node */ if ((tree = ( AvlTree )calloc(1, sizeof( struct AvlNode ) )) == NULL) { return( tree ); } tree->key = key; tree->port = port; tree->pbsnode = node; tree->left = NULL; tree->right = NULL; tree->height = 0; } /* If key is less than current node value go left else go right. If equal compare port and go left or right accordingly */ if (key < tree->key) { tree->left = AVL_insert( key, port, node, tree->left ); if (height(tree->left) - height(tree->right) >= 2 ) { if (key <= tree->left->key ) tree = single_rotate_with_left( tree ); else tree = double_rotate_with_left( tree ); } } else if (key > tree->key ) { tree->right = AVL_insert( key, port, node, tree->right ); if (height(tree->right) - height(tree->left) >= 2 ) { if (key >= tree->right->key) tree = single_rotate_with_right( tree ); else tree = double_rotate_with_right( tree ); } } else { /* if it is in the tree, do not add it again */ if (port == tree->port) return(tree); /* the keys are equal. sort by port */ if (port != 0) { if (port < tree->port) { tree->left = AVL_insert( key, port, node, tree->left ); if (height(tree->left) - height(tree->right) >= 2) { if (port <= tree->left->port) tree = single_rotate_with_left( tree ); else tree = double_rotate_with_left( tree ); } } else if (port > tree->port ) { tree->right = AVL_insert( key, port, node, tree->right ); if (height(tree->right) - height(tree->left) >= 2) { if (port >= tree->right->port) tree = single_rotate_with_right( tree ); else tree = double_rotate_with_right( tree ); } } } } tree->height = Max(height(tree->left), height(tree->right)) + 1; return(tree); } /* End AVL_insert */