Switch to K&R style indentation.
This commit is contained in:
parent
f75dcef72a
commit
9f38e39463
8 changed files with 771 additions and 787 deletions
289
lib/avl_tree.c
289
lib/avl_tree.c
|
@ -29,7 +29,7 @@
|
||||||
library for inclusion into tinc (http://tinc.nl.linux.org/) by
|
library for inclusion into tinc (http://tinc.nl.linux.org/) by
|
||||||
Guus Sliepen <guus@sliepen.eu.org>.
|
Guus Sliepen <guus@sliepen.eu.org>.
|
||||||
|
|
||||||
$Id: avl_tree.c,v 1.1.2.9 2002/06/21 10:11:11 guus Exp $
|
$Id: avl_tree.c,v 1.1.2.10 2002/09/09 21:49:16 guus Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -56,41 +56,46 @@
|
||||||
int lg(unsigned int u)
|
int lg(unsigned int u)
|
||||||
{
|
{
|
||||||
int r = 1;
|
int r = 1;
|
||||||
if (!u)
|
|
||||||
|
if(!u)
|
||||||
return 0;
|
return 0;
|
||||||
if (u & 0xffff0000)
|
|
||||||
{
|
if(u & 0xffff0000) {
|
||||||
u >>= 16;
|
u >>= 16;
|
||||||
r += 16;
|
r += 16;
|
||||||
}
|
}
|
||||||
if (u & 0x0000ff00)
|
|
||||||
{
|
if(u & 0x0000ff00) {
|
||||||
u >>= 8;
|
u >>= 8;
|
||||||
r += 8;
|
r += 8;
|
||||||
}
|
}
|
||||||
if (u & 0x000000f0)
|
|
||||||
{
|
if(u & 0x000000f0) {
|
||||||
u >>= 4;
|
u >>= 4;
|
||||||
r += 4;
|
r += 4;
|
||||||
}
|
}
|
||||||
if (u & 0x0000000c)
|
|
||||||
{
|
if(u & 0x0000000c) {
|
||||||
u >>= 2;
|
u >>= 2;
|
||||||
r += 2;
|
r += 2;
|
||||||
}
|
}
|
||||||
if (u & 0x00000002)
|
|
||||||
|
if(u & 0x00000002)
|
||||||
r++;
|
r++;
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Internal helper functions */
|
/* Internal helper functions */
|
||||||
|
|
||||||
int avl_check_balance(avl_node_t *node)
|
int avl_check_balance(avl_node_t * node)
|
||||||
{
|
{
|
||||||
#ifdef AVL_DEPTH
|
#ifdef AVL_DEPTH
|
||||||
int d;
|
int d;
|
||||||
|
|
||||||
d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
|
d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
|
||||||
|
|
||||||
return d < -1 ? -1 : d > 1 ? 1 : 0;
|
return d < -1 ? -1 : d > 1 ? 1 : 0;
|
||||||
#else
|
#else
|
||||||
/* int d;
|
/* int d;
|
||||||
|
@ -102,15 +107,17 @@ int avl_check_balance(avl_node_t *node)
|
||||||
pl = lg(AVL_L_COUNT(node));
|
pl = lg(AVL_L_COUNT(node));
|
||||||
r = AVL_R_COUNT(node);
|
r = AVL_R_COUNT(node);
|
||||||
|
|
||||||
if (r >> pl + 1)
|
if(r >> pl + 1)
|
||||||
return 1;
|
return 1;
|
||||||
if (pl < 2 || r >> pl - 2)
|
|
||||||
|
if(pl < 2 || r >> pl - 2)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
void avl_rebalance(avl_tree_t * tree, avl_node_t * node)
|
||||||
{
|
{
|
||||||
avl_node_t *child;
|
avl_node_t *child;
|
||||||
avl_node_t *gchild;
|
avl_node_t *gchild;
|
||||||
|
@ -119,25 +126,25 @@ void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
||||||
|
|
||||||
parent = node;
|
parent = node;
|
||||||
|
|
||||||
while (node)
|
while(node) {
|
||||||
{
|
|
||||||
parent = node->parent;
|
parent = node->parent;
|
||||||
|
|
||||||
superparent = parent ? node == parent->left ? &parent->left : &parent->right : &tree->root;
|
superparent =
|
||||||
|
parent ? node ==
|
||||||
|
parent->left ? &parent->left : &parent->right : &tree->root;
|
||||||
|
|
||||||
switch (avl_check_balance(node))
|
switch (avl_check_balance(node)) {
|
||||||
{
|
|
||||||
case -1:
|
case -1:
|
||||||
child = node->left;
|
child = node->left;
|
||||||
#ifdef AVL_DEPTH
|
#ifdef AVL_DEPTH
|
||||||
if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
|
if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
|
||||||
#else
|
#else
|
||||||
if (AVL_L_COUNT(child) >= AVL_R_COUNT(child))
|
if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
node->left = child->right;
|
node->left = child->right;
|
||||||
if (node->left)
|
if(node->left)
|
||||||
node->left->parent = node;
|
node->left->parent = node;
|
||||||
|
|
||||||
child->right = node;
|
child->right = node;
|
||||||
node->parent = child;
|
node->parent = child;
|
||||||
*superparent = child;
|
*superparent = child;
|
||||||
|
@ -150,22 +157,26 @@ void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
||||||
node->depth = AVL_CALC_DEPTH(node);
|
node->depth = AVL_CALC_DEPTH(node);
|
||||||
child->depth = AVL_CALC_DEPTH(child);
|
child->depth = AVL_CALC_DEPTH(child);
|
||||||
#endif
|
#endif
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
gchild = child->right;
|
gchild = child->right;
|
||||||
node->left = gchild->right;
|
node->left = gchild->right;
|
||||||
if (node->left)
|
|
||||||
|
if(node->left)
|
||||||
node->left->parent = node;
|
node->left->parent = node;
|
||||||
child->right = gchild->left;
|
child->right = gchild->left;
|
||||||
if (child->right)
|
|
||||||
|
if(child->right)
|
||||||
child->right->parent = child;
|
child->right->parent = child;
|
||||||
gchild->right = node;
|
gchild->right = node;
|
||||||
if (gchild->right)
|
|
||||||
|
if(gchild->right)
|
||||||
gchild->right->parent = gchild;
|
gchild->right->parent = gchild;
|
||||||
gchild->left = child;
|
gchild->left = child;
|
||||||
if (gchild->left)
|
|
||||||
|
if(gchild->left)
|
||||||
gchild->left->parent = gchild;
|
gchild->left->parent = gchild;
|
||||||
*superparent = gchild;
|
*superparent = gchild;
|
||||||
|
|
||||||
gchild->parent = parent;
|
gchild->parent = parent;
|
||||||
#ifdef AVL_COUNT
|
#ifdef AVL_COUNT
|
||||||
node->count = AVL_CALC_COUNT(node);
|
node->count = AVL_CALC_COUNT(node);
|
||||||
|
@ -179,16 +190,16 @@ void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
child = node->right;
|
child = node->right;
|
||||||
#ifdef AVL_DEPTH
|
#ifdef AVL_DEPTH
|
||||||
if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
|
if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
|
||||||
#else
|
#else
|
||||||
if (AVL_R_COUNT(child) >= AVL_L_COUNT(child))
|
if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
node->right = child->left;
|
node->right = child->left;
|
||||||
if (node->right)
|
if(node->right)
|
||||||
node->right->parent = node;
|
node->right->parent = node;
|
||||||
child->left = node;
|
child->left = node;
|
||||||
node->parent = child;
|
node->parent = child;
|
||||||
|
@ -202,21 +213,25 @@ void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
||||||
node->depth = AVL_CALC_DEPTH(node);
|
node->depth = AVL_CALC_DEPTH(node);
|
||||||
child->depth = AVL_CALC_DEPTH(child);
|
child->depth = AVL_CALC_DEPTH(child);
|
||||||
#endif
|
#endif
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
gchild = child->left;
|
gchild = child->left;
|
||||||
node->right = gchild->left;
|
node->right = gchild->left;
|
||||||
if (node->right)
|
|
||||||
|
if(node->right)
|
||||||
node->right->parent = node;
|
node->right->parent = node;
|
||||||
child->left = gchild->right;
|
child->left = gchild->right;
|
||||||
if (child->left)
|
|
||||||
|
if(child->left)
|
||||||
child->left->parent = child;
|
child->left->parent = child;
|
||||||
gchild->left = node;
|
gchild->left = node;
|
||||||
if (gchild->left)
|
|
||||||
|
if(gchild->left)
|
||||||
gchild->left->parent = gchild;
|
gchild->left->parent = gchild;
|
||||||
gchild->right = child;
|
gchild->right = child;
|
||||||
if (gchild->right)
|
|
||||||
|
if(gchild->right)
|
||||||
gchild->right->parent = gchild;
|
gchild->right->parent = gchild;
|
||||||
|
|
||||||
*superparent = gchild;
|
*superparent = gchild;
|
||||||
gchild->parent = parent;
|
gchild->parent = parent;
|
||||||
#ifdef AVL_COUNT
|
#ifdef AVL_COUNT
|
||||||
|
@ -231,6 +246,7 @@ void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
#ifdef AVL_COUNT
|
#ifdef AVL_COUNT
|
||||||
node->count = AVL_CALC_COUNT(node);
|
node->count = AVL_CALC_COUNT(node);
|
||||||
|
@ -256,117 +272,106 @@ avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_free_tree(avl_tree_t *tree)
|
void avl_free_tree(avl_tree_t * tree)
|
||||||
{
|
{
|
||||||
free(tree);
|
free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_alloc_node(void)
|
avl_node_t *avl_alloc_node(void)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
return (avl_node_t *)xmalloc_and_zero(sizeof(avl_node_t));
|
||||||
|
|
||||||
node = xmalloc_and_zero(sizeof(avl_node_t));
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_free_node(avl_tree_t *tree, avl_node_t *node)
|
void avl_free_node(avl_tree_t * tree, avl_node_t * node)
|
||||||
{
|
{
|
||||||
if(node->data && tree->delete)
|
if(node->data && tree->delete)
|
||||||
tree->delete(node->data);
|
tree->delete(node->data);
|
||||||
|
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Searching */
|
/* Searching */
|
||||||
|
|
||||||
void *avl_search(const avl_tree_t *tree, const void *data)
|
void *avl_search(const avl_tree_t * tree, const void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
|
|
||||||
node = avl_search_node(tree, data);
|
node = avl_search_node(tree, data);
|
||||||
|
|
||||||
return node?node->data:NULL;
|
return node ? node->data : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
|
void *avl_search_closest(const avl_tree_t * tree, const void *data, int *result)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
|
|
||||||
node = avl_search_closest_node(tree, data, result);
|
node = avl_search_closest_node(tree, data, result);
|
||||||
|
|
||||||
return node?node->data:NULL;
|
return node ? node->data : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
|
void *avl_search_closest_smaller(const avl_tree_t * tree, const void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
|
|
||||||
node = avl_search_closest_smaller_node(tree, data);
|
node = avl_search_closest_smaller_node(tree, data);
|
||||||
|
|
||||||
return node?node->data:NULL;
|
return node ? node->data : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
|
void *avl_search_closest_greater(const avl_tree_t * tree, const void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
|
|
||||||
node = avl_search_closest_greater_node(tree, data);
|
node = avl_search_closest_greater_node(tree, data);
|
||||||
|
|
||||||
return node?node->data:NULL;
|
return node ? node->data : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
|
avl_node_t *avl_search_node(const avl_tree_t * tree, const void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
node = avl_search_closest_node(tree, data, &result);
|
node = avl_search_closest_node(tree, data, &result);
|
||||||
|
|
||||||
return result?NULL:node;
|
return result ? NULL : node;
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data, int *result)
|
avl_node_t *avl_search_closest_node(const avl_tree_t * tree, const void *data,
|
||||||
|
int *result)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
node = tree->root;
|
node = tree->root;
|
||||||
|
|
||||||
if (!node)
|
if(!node) {
|
||||||
{
|
|
||||||
if(result)
|
if(result)
|
||||||
*result = 0;
|
*result = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;)
|
for(;;) {
|
||||||
{
|
|
||||||
c = tree->compare(data, node->data);
|
c = tree->compare(data, node->data);
|
||||||
|
|
||||||
if (c < 0)
|
if(c < 0) {
|
||||||
{
|
if(node->left)
|
||||||
if (node->left)
|
|
||||||
node = node->left;
|
node = node->left;
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
if(result)
|
if(result)
|
||||||
*result = -1;
|
*result = -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
} else if(c > 0) {
|
||||||
else if (c > 0)
|
if(node->right)
|
||||||
{
|
|
||||||
if (node->right)
|
|
||||||
node = node->right;
|
node = node->right;
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
if(result)
|
if(result)
|
||||||
*result = 1;
|
*result = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
if(result)
|
if(result)
|
||||||
*result = 0;
|
*result = 0;
|
||||||
break;
|
break;
|
||||||
|
@ -376,7 +381,8 @@ avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data, in
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree, const void *data)
|
avl_node_t *avl_search_closest_smaller_node(const avl_tree_t * tree,
|
||||||
|
const void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
int result;
|
int result;
|
||||||
|
@ -389,7 +395,8 @@ avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree, const void *
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree, const void *data)
|
avl_node_t *avl_search_closest_greater_node(const avl_tree_t * tree,
|
||||||
|
const void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
int result;
|
int result;
|
||||||
|
@ -404,32 +411,31 @@ avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree, const void *
|
||||||
|
|
||||||
/* Insertion and deletion */
|
/* Insertion and deletion */
|
||||||
|
|
||||||
avl_node_t *avl_insert(avl_tree_t *tree, void *data)
|
avl_node_t *avl_insert(avl_tree_t * tree, void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *closest, *new;
|
avl_node_t *closest, *new;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if (!tree->root)
|
if(!tree->root) {
|
||||||
{
|
|
||||||
new = avl_alloc_node();
|
new = avl_alloc_node();
|
||||||
new->data = data;
|
new->data = data;
|
||||||
avl_insert_top(tree, new);
|
avl_insert_top(tree, new);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
closest = avl_search_closest_node(tree, data, &result);
|
closest = avl_search_closest_node(tree, data, &result);
|
||||||
switch(result)
|
|
||||||
{
|
switch (result) {
|
||||||
case -1:
|
case -1:
|
||||||
new = avl_alloc_node();
|
new = avl_alloc_node();
|
||||||
new->data = data;
|
new->data = data;
|
||||||
avl_insert_before(tree, closest, new);
|
avl_insert_before(tree, closest, new);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
new = avl_alloc_node();
|
new = avl_alloc_node();
|
||||||
new->data = data;
|
new->data = data;
|
||||||
avl_insert_after(tree, closest, new);
|
avl_insert_after(tree, closest, new);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -445,24 +451,25 @@ avl_node_t *avl_insert(avl_tree_t *tree, void *data)
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
|
avl_node_t *avl_insert_node(avl_tree_t * tree, avl_node_t * node)
|
||||||
{
|
{
|
||||||
avl_node_t *closest;
|
avl_node_t *closest;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if (!tree->root)
|
if(!tree->root)
|
||||||
avl_insert_top(tree, node);
|
avl_insert_top(tree, node);
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
closest = avl_search_closest_node(tree, node->data, &result);
|
closest = avl_search_closest_node(tree, node->data, &result);
|
||||||
switch(result)
|
|
||||||
{
|
switch (result) {
|
||||||
case -1:
|
case -1:
|
||||||
avl_insert_before(tree, closest, node);
|
avl_insert_before(tree, closest, node);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
avl_insert_after(tree, closest, node);
|
avl_insert_after(tree, closest, node);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 0:
|
case 0:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -478,15 +485,16 @@ avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
|
void avl_insert_top(avl_tree_t * tree, avl_node_t * node)
|
||||||
{
|
{
|
||||||
node->prev = node->next = node->parent = NULL;
|
node->prev = node->next = node->parent = NULL;
|
||||||
tree->head = tree->tail = tree->root = node;
|
tree->head = tree->tail = tree->root = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_insert_before(avl_tree_t *tree, avl_node_t *before, avl_node_t *node)
|
void avl_insert_before(avl_tree_t * tree, avl_node_t * before,
|
||||||
|
avl_node_t * node)
|
||||||
{
|
{
|
||||||
if (!before)
|
if(!before)
|
||||||
return tree->tail ? avl_insert_after(tree, tree->tail, node) : avl_insert_top(tree, node);
|
return tree->tail ? avl_insert_after(tree, tree->tail, node) : avl_insert_top(tree, node);
|
||||||
|
|
||||||
node->next = before;
|
node->next = before;
|
||||||
|
@ -496,7 +504,7 @@ void avl_insert_before(avl_tree_t *tree, avl_node_t *before, avl_node_t *node)
|
||||||
if(before->left)
|
if(before->left)
|
||||||
return avl_insert_after(tree, before->prev, node);
|
return avl_insert_after(tree, before->prev, node);
|
||||||
|
|
||||||
if (before->prev)
|
if(before->prev)
|
||||||
before->prev->next = node;
|
before->prev->next = node;
|
||||||
else
|
else
|
||||||
tree->head = node;
|
tree->head = node;
|
||||||
|
@ -507,10 +515,12 @@ void avl_insert_before(avl_tree_t *tree, avl_node_t *before, avl_node_t *node)
|
||||||
avl_rebalance(tree, before->parent);
|
avl_rebalance(tree, before->parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
|
void avl_insert_after(avl_tree_t * tree, avl_node_t * after, avl_node_t * node)
|
||||||
{
|
{
|
||||||
if (!after)
|
if(!after)
|
||||||
return tree->head ? avl_insert_before(tree, tree->head, node) : avl_insert_top(tree, node);
|
return tree->head ? avl_insert_before(tree, tree->head,
|
||||||
|
node) : avl_insert_top(tree,
|
||||||
|
node);
|
||||||
|
|
||||||
if(after->right)
|
if(after->right)
|
||||||
return avl_insert_before(tree, after->next, node);
|
return avl_insert_before(tree, after->next, node);
|
||||||
|
@ -519,7 +529,7 @@ void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
|
||||||
node->parent = after;
|
node->parent = after;
|
||||||
node->next = after->next;
|
node->next = after->next;
|
||||||
|
|
||||||
if (after->next)
|
if(after->next)
|
||||||
after->next->prev = node;
|
after->next->prev = node;
|
||||||
else
|
else
|
||||||
tree->tail = node;
|
tree->tail = node;
|
||||||
|
@ -530,7 +540,7 @@ void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
|
||||||
avl_rebalance(tree, after->parent);
|
avl_rebalance(tree, after->parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
|
avl_node_t *avl_unlink(avl_tree_t * tree, void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
|
|
||||||
|
@ -542,54 +552,57 @@ avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
|
void avl_unlink_node(avl_tree_t * tree, avl_node_t * node)
|
||||||
{
|
{
|
||||||
avl_node_t *parent;
|
avl_node_t *parent;
|
||||||
avl_node_t **superparent;
|
avl_node_t **superparent;
|
||||||
avl_node_t *subst, *left, *right;
|
avl_node_t *subst, *left, *right;
|
||||||
avl_node_t *balnode;
|
avl_node_t *balnode;
|
||||||
|
|
||||||
if (node->prev)
|
if(node->prev)
|
||||||
node->prev->next = node->next;
|
node->prev->next = node->next;
|
||||||
else
|
else
|
||||||
tree->head = node->next;
|
tree->head = node->next;
|
||||||
if (node->next)
|
if(node->next)
|
||||||
node->next->prev = node->prev;
|
node->next->prev = node->prev;
|
||||||
else
|
else
|
||||||
tree->tail = node->prev;
|
tree->tail = node->prev;
|
||||||
|
|
||||||
parent = node->parent;
|
parent = node->parent;
|
||||||
|
|
||||||
superparent = parent ? node == parent->left ? &parent->left : &parent->right : &tree->root;
|
superparent =
|
||||||
|
parent ? node ==
|
||||||
|
parent->left ? &parent->left : &parent->right : &tree->root;
|
||||||
|
|
||||||
left = node->left;
|
left = node->left;
|
||||||
right = node->right;
|
right = node->right;
|
||||||
if (!left)
|
if(!left) {
|
||||||
{
|
|
||||||
*superparent = right;
|
*superparent = right;
|
||||||
if (right)
|
|
||||||
|
if(right)
|
||||||
right->parent = parent;
|
right->parent = parent;
|
||||||
|
|
||||||
balnode = parent;
|
balnode = parent;
|
||||||
} else if (!right)
|
} else if(!right) {
|
||||||
{
|
|
||||||
*superparent = left;
|
*superparent = left;
|
||||||
left->parent = parent;
|
left->parent = parent;
|
||||||
balnode = parent;
|
balnode = parent;
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
subst = node->prev;
|
subst = node->prev;
|
||||||
if (subst == left)
|
|
||||||
{
|
if(subst == left) {
|
||||||
balnode = subst;
|
balnode = subst;
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
balnode = subst->parent;
|
balnode = subst->parent;
|
||||||
balnode->right = subst->left;
|
balnode->right = subst->left;
|
||||||
if (balnode->right)
|
|
||||||
|
if(balnode->right)
|
||||||
balnode->right->parent = balnode;
|
balnode->right->parent = balnode;
|
||||||
|
|
||||||
subst->left = left;
|
subst->left = left;
|
||||||
left->parent = subst;
|
left->parent = subst;
|
||||||
}
|
}
|
||||||
|
|
||||||
subst->right = right;
|
subst->right = right;
|
||||||
subst->parent = parent;
|
subst->parent = parent;
|
||||||
right->parent = subst;
|
right->parent = subst;
|
||||||
|
@ -608,30 +621,29 @@ void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
|
void avl_delete_node(avl_tree_t * tree, avl_node_t * node)
|
||||||
{
|
{
|
||||||
avl_unlink_node(tree, node);
|
avl_unlink_node(tree, node);
|
||||||
avl_free_node(tree, node);
|
avl_free_node(tree, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_delete(avl_tree_t *tree, void *data)
|
void avl_delete(avl_tree_t * tree, void *data)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
|
|
||||||
node = avl_search_node(tree, data);
|
node = avl_search_node(tree, data);
|
||||||
|
|
||||||
if (node)
|
if(node)
|
||||||
avl_delete_node(tree, node);
|
avl_delete_node(tree, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fast tree cleanup */
|
/* Fast tree cleanup */
|
||||||
|
|
||||||
void avl_delete_tree(avl_tree_t *tree)
|
void avl_delete_tree(avl_tree_t * tree)
|
||||||
{
|
{
|
||||||
avl_node_t *node, *next;
|
avl_node_t *node, *next;
|
||||||
|
|
||||||
for(node = tree->root; node; node = next)
|
for(node = tree->root; node; node = next) {
|
||||||
{
|
|
||||||
next = node->next;
|
next = node->next;
|
||||||
avl_free_node(tree, node);
|
avl_free_node(tree, node);
|
||||||
}
|
}
|
||||||
|
@ -641,23 +653,21 @@ void avl_delete_tree(avl_tree_t *tree)
|
||||||
|
|
||||||
/* Tree walking */
|
/* Tree walking */
|
||||||
|
|
||||||
void avl_foreach(avl_tree_t *tree, avl_action_t action)
|
void avl_foreach(avl_tree_t * tree, avl_action_t action)
|
||||||
{
|
{
|
||||||
avl_node_t *node, *next;
|
avl_node_t *node, *next;
|
||||||
|
|
||||||
for(node = tree->head; node; node = next)
|
for(node = tree->head; node; node = next) {
|
||||||
{
|
|
||||||
next = node->next;
|
next = node->next;
|
||||||
action(node->data);
|
action(node->data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void avl_foreach_node(avl_tree_t *tree, avl_action_t action)
|
void avl_foreach_node(avl_tree_t * tree, avl_action_t action)
|
||||||
{
|
{
|
||||||
avl_node_t *node, *next;
|
avl_node_t *node, *next;
|
||||||
|
|
||||||
for(node = tree->head; node; node = next)
|
for(node = tree->head; node; node = next) {
|
||||||
{
|
|
||||||
next = node->next;
|
next = node->next;
|
||||||
action(node);
|
action(node);
|
||||||
}
|
}
|
||||||
|
@ -666,31 +676,27 @@ void avl_foreach_node(avl_tree_t *tree, avl_action_t action)
|
||||||
/* Indexing */
|
/* Indexing */
|
||||||
|
|
||||||
#ifdef AVL_COUNT
|
#ifdef AVL_COUNT
|
||||||
unsigned int avl_count(avl_tree_t *tree)
|
unsigned int avl_count(avl_tree_t * tree)
|
||||||
{
|
{
|
||||||
return AVL_NODE_COUNT(tree->root);
|
return AVL_NODE_COUNT(tree->root);
|
||||||
}
|
}
|
||||||
|
|
||||||
avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
|
avl_node_t *avl_get_node(const avl_tree_t * tree, unsigned int index)
|
||||||
{
|
{
|
||||||
avl_node_t *node;
|
avl_node_t *node;
|
||||||
unsigned int c;
|
unsigned int c;
|
||||||
|
|
||||||
node = tree->root;
|
node = tree->root;
|
||||||
|
|
||||||
while (node)
|
while(node) {
|
||||||
{
|
|
||||||
c = AVL_L_COUNT(node);
|
c = AVL_L_COUNT(node);
|
||||||
|
|
||||||
if (index < c)
|
if(index < c) {
|
||||||
{
|
|
||||||
node = node->left;
|
node = node->left;
|
||||||
} else if (index > c)
|
} else if(index > c) {
|
||||||
{
|
|
||||||
node = node->right;
|
node = node->right;
|
||||||
index -= c + 1;
|
index -= c + 1;
|
||||||
} else
|
} else {
|
||||||
{
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -698,16 +704,15 @@ avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int avl_index(const avl_node_t *node)
|
unsigned int avl_index(const avl_node_t * node)
|
||||||
{
|
{
|
||||||
avl_node_t *next;
|
avl_node_t *next;
|
||||||
unsigned int index;
|
unsigned int index;
|
||||||
|
|
||||||
index = AVL_L_COUNT(node);
|
index = AVL_L_COUNT(node);
|
||||||
|
|
||||||
while ((next = node->parent))
|
while((next = node->parent)) {
|
||||||
{
|
if(node == next->right)
|
||||||
if (node == next->right)
|
|
||||||
index += AVL_L_COUNT(next) + 1;
|
index += AVL_L_COUNT(next) + 1;
|
||||||
node = next;
|
node = next;
|
||||||
}
|
}
|
||||||
|
@ -716,7 +721,7 @@ unsigned int avl_index(const avl_node_t *node)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef AVL_DEPTH
|
#ifdef AVL_DEPTH
|
||||||
unsigned int avl_depth(avl_tree_t *tree)
|
unsigned int avl_depth(avl_tree_t * tree)
|
||||||
{
|
{
|
||||||
return AVL_NODE_DEPTH(tree->root);
|
return AVL_NODE_DEPTH(tree->root);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
library for inclusion into tinc (http://tinc.nl.linux.org/) by
|
library for inclusion into tinc (http://tinc.nl.linux.org/) by
|
||||||
Guus Sliepen <guus@sliepen.eu.org>.
|
Guus Sliepen <guus@sliepen.eu.org>.
|
||||||
|
|
||||||
$Id: avl_tree.h,v 1.1.2.5 2002/06/21 10:11:11 guus Exp $
|
$Id: avl_tree.h,v 1.1.2.6 2002/09/09 21:49:16 guus Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,9 +37,9 @@
|
||||||
#define __AVL_TREE_H__
|
#define __AVL_TREE_H__
|
||||||
|
|
||||||
#ifndef AVL_DEPTH
|
#ifndef AVL_DEPTH
|
||||||
#ifndef AVL_COUNT
|
#ifndef AVL_COUNT
|
||||||
#define AVL_DEPTH
|
#define AVL_DEPTH
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct avl_node_t {
|
typedef struct avl_node_t {
|
||||||
|
@ -94,7 +94,7 @@ extern avl_tree_t *avl_alloc_tree(avl_compare_t, avl_action_t);
|
||||||
extern void avl_free_tree(avl_tree_t *);
|
extern void avl_free_tree(avl_tree_t *);
|
||||||
|
|
||||||
extern avl_node_t *avl_alloc_node(void);
|
extern avl_node_t *avl_alloc_node(void);
|
||||||
extern void avl_free_node(avl_tree_t *tree, avl_node_t *);
|
extern void avl_free_node(avl_tree_t * tree, avl_node_t *);
|
||||||
|
|
||||||
/* Insertion and deletion */
|
/* Insertion and deletion */
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ extern void avl_insert_before(avl_tree_t *, avl_node_t *, avl_node_t *);
|
||||||
extern void avl_insert_after(avl_tree_t *, avl_node_t *, avl_node_t *);
|
extern void avl_insert_after(avl_tree_t *, avl_node_t *, avl_node_t *);
|
||||||
|
|
||||||
extern avl_node_t *avl_unlink(avl_tree_t *, void *);
|
extern avl_node_t *avl_unlink(avl_tree_t *, void *);
|
||||||
extern void avl_unlink_node(avl_tree_t *tree, avl_node_t *);
|
extern void avl_unlink_node(avl_tree_t * tree, avl_node_t *);
|
||||||
extern void avl_delete(avl_tree_t *, void *);
|
extern void avl_delete(avl_tree_t *, void *);
|
||||||
extern void avl_delete_node(avl_tree_t *, avl_node_t *);
|
extern void avl_delete_node(avl_tree_t *, avl_node_t *);
|
||||||
|
|
||||||
|
|
39
lib/dropin.c
39
lib/dropin.c
|
@ -17,7 +17,7 @@
|
||||||
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
|
||||||
$Id: dropin.c,v 1.1.2.11 2002/07/12 11:45:21 guus Exp $
|
$Id: dropin.c,v 1.1.2.12 2002/09/09 21:49:16 guus Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
@ -57,8 +57,7 @@ int daemon(int nochdir, int noclose)
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
||||||
/* Check if forking failed */
|
/* Check if forking failed */
|
||||||
if(pid < 0)
|
if(pid < 0) {
|
||||||
{
|
|
||||||
perror("fork");
|
perror("fork");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
@ -68,31 +67,25 @@ int daemon(int nochdir, int noclose)
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
/* Detach by becoming the new process group leader */
|
/* Detach by becoming the new process group leader */
|
||||||
if(setsid() < 0)
|
if(setsid() < 0) {
|
||||||
{
|
|
||||||
perror("setsid");
|
perror("setsid");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Change working directory to the root (to avoid keeping mount
|
/* Change working directory to the root (to avoid keeping mount
|
||||||
points busy) */
|
points busy) */
|
||||||
if(!nochdir)
|
if(!nochdir) {
|
||||||
{
|
|
||||||
chdir("/");
|
chdir("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Redirect stdin/out/err to /dev/null */
|
/* Redirect stdin/out/err to /dev/null */
|
||||||
if(!noclose)
|
if(!noclose) {
|
||||||
{
|
|
||||||
fd = open("/dev/null", O_RDWR);
|
fd = open("/dev/null", O_RDWR);
|
||||||
|
|
||||||
if(fd < 0)
|
if(fd < 0) {
|
||||||
{
|
|
||||||
perror("opening /dev/null");
|
perror("opening /dev/null");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
dup2(fd, 0);
|
dup2(fd, 0);
|
||||||
dup2(fd, 1);
|
dup2(fd, 1);
|
||||||
dup2(fd, 2);
|
dup2(fd, 2);
|
||||||
|
@ -103,9 +96,6 @@ int daemon(int nochdir, int noclose)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef HAVE_GET_CURRENT_DIR_NAME
|
#ifndef HAVE_GET_CURRENT_DIR_NAME
|
||||||
/*
|
/*
|
||||||
Replacement for the GNU get_current_dir_name function:
|
Replacement for the GNU get_current_dir_name function:
|
||||||
|
@ -127,10 +117,10 @@ char *get_current_dir_name(void)
|
||||||
|
|
||||||
errno = 0; /* Success */
|
errno = 0; /* Success */
|
||||||
r = getcwd(buf, size);
|
r = getcwd(buf, size);
|
||||||
|
|
||||||
/* getcwd returns NULL and sets errno to ERANGE if the bufferspace
|
/* getcwd returns NULL and sets errno to ERANGE if the bufferspace
|
||||||
is insufficient to contain the entire working directory. */
|
is insufficient to contain the entire working directory. */
|
||||||
while(r == NULL && errno == ERANGE)
|
while(r == NULL && errno == ERANGE) {
|
||||||
{
|
|
||||||
free(buf);
|
free(buf);
|
||||||
size <<= 1; /* double the size */
|
size <<= 1; /* double the size */
|
||||||
buf = xmalloc(size);
|
buf = xmalloc(size);
|
||||||
|
@ -152,18 +142,17 @@ int asprintf(char **buf, const char *fmt, ...)
|
||||||
*buf = xmalloc(len);
|
*buf = xmalloc(len);
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
status = vsnprintf (*buf, len, fmt, ap);
|
status = vsnprintf(*buf, len, fmt, ap);
|
||||||
va_end (ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(status >= 0)
|
if(status >= 0)
|
||||||
*buf = xrealloc(*buf, status);
|
*buf = xrealloc(*buf, status);
|
||||||
|
|
||||||
if(status > len-1)
|
if(status > len - 1) {
|
||||||
{
|
|
||||||
len = status;
|
len = status;
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
status = vsnprintf (*buf, len, fmt, ap);
|
status = vsnprintf(*buf, len, fmt, ap);
|
||||||
va_end (ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
|
||||||
$Id: dropin.h,v 1.1.2.8 2002/06/21 10:11:11 guus Exp $
|
$Id: dropin.h,v 1.1.2.9 2002/09/09 21:49:16 guus Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __DROPIN_H__
|
#ifndef __DROPIN_H__
|
||||||
|
@ -28,7 +28,7 @@ extern int daemon(int, int);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_GET_CURRENT_DIR_NAME
|
#ifndef HAVE_GET_CURRENT_DIR_NAME
|
||||||
extern char* get_current_dir_name(void);
|
extern char *get_current_dir_name(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_ASPRINTF
|
#ifndef HAVE_ASPRINTF
|
||||||
|
|
43
lib/list.c
43
lib/list.c
|
@ -17,7 +17,7 @@
|
||||||
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
|
||||||
$Id: list.c,v 1.1.2.11 2002/06/21 10:11:11 guus Exp $
|
$Id: list.c,v 1.1.2.12 2002/09/09 21:49:16 guus Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
@ -41,21 +41,17 @@ list_t *list_alloc(list_action_t delete)
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_free(list_t *list)
|
void list_free(list_t * list)
|
||||||
{
|
{
|
||||||
free(list);
|
free(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
list_node_t *list_alloc_node(void)
|
list_node_t *list_alloc_node(void)
|
||||||
{
|
{
|
||||||
list_node_t *node;
|
return (list_node_t *)xmalloc_and_zero(sizeof(list_node_t));
|
||||||
|
|
||||||
node = xmalloc_and_zero(sizeof(list_node_t));
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_free_node(list_t *list, list_node_t *node)
|
void list_free_node(list_t * list, list_node_t * node)
|
||||||
{
|
{
|
||||||
if(node->data && list->delete)
|
if(node->data && list->delete)
|
||||||
list->delete(node->data);
|
list->delete(node->data);
|
||||||
|
@ -65,7 +61,7 @@ void list_free_node(list_t *list, list_node_t *node)
|
||||||
|
|
||||||
/* Insertion and deletion */
|
/* Insertion and deletion */
|
||||||
|
|
||||||
list_node_t *list_insert_head(list_t *list, void *data)
|
list_node_t *list_insert_head(list_t * list, void *data)
|
||||||
{
|
{
|
||||||
list_node_t *node;
|
list_node_t *node;
|
||||||
|
|
||||||
|
@ -86,7 +82,7 @@ list_node_t *list_insert_head(list_t *list, void *data)
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
list_node_t *list_insert_tail(list_t *list, void *data)
|
list_node_t *list_insert_tail(list_t * list, void *data)
|
||||||
{
|
{
|
||||||
list_node_t *node;
|
list_node_t *node;
|
||||||
|
|
||||||
|
@ -107,7 +103,7 @@ list_node_t *list_insert_tail(list_t *list, void *data)
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_unlink_node(list_t *list, list_node_t *node)
|
void list_unlink_node(list_t * list, list_node_t * node)
|
||||||
{
|
{
|
||||||
if(node->prev)
|
if(node->prev)
|
||||||
node->prev->next = node->next;
|
node->prev->next = node->next;
|
||||||
|
@ -122,25 +118,25 @@ void list_unlink_node(list_t *list, list_node_t *node)
|
||||||
list->count--;
|
list->count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_delete_node(list_t *list, list_node_t *node)
|
void list_delete_node(list_t * list, list_node_t * node)
|
||||||
{
|
{
|
||||||
list_unlink_node(list, node);
|
list_unlink_node(list, node);
|
||||||
list_free_node(list, node);
|
list_free_node(list, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_delete_head(list_t *list)
|
void list_delete_head(list_t * list)
|
||||||
{
|
{
|
||||||
list_delete_node(list, list->head);
|
list_delete_node(list, list->head);
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_delete_tail(list_t *list)
|
void list_delete_tail(list_t * list)
|
||||||
{
|
{
|
||||||
list_delete_node(list, list->tail);
|
list_delete_node(list, list->tail);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Head/tail lookup */
|
/* Head/tail lookup */
|
||||||
|
|
||||||
void *list_get_head(list_t *list)
|
void *list_get_head(list_t * list)
|
||||||
{
|
{
|
||||||
if(list->head)
|
if(list->head)
|
||||||
return list->head->data;
|
return list->head->data;
|
||||||
|
@ -148,7 +144,7 @@ void *list_get_head(list_t *list)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *list_get_tail(list_t *list)
|
void *list_get_tail(list_t * list)
|
||||||
{
|
{
|
||||||
if(list->tail)
|
if(list->tail)
|
||||||
return list->tail->data;
|
return list->tail->data;
|
||||||
|
@ -158,12 +154,11 @@ void *list_get_tail(list_t *list)
|
||||||
|
|
||||||
/* Fast list deletion */
|
/* Fast list deletion */
|
||||||
|
|
||||||
void list_delete_list(list_t *list)
|
void list_delete_list(list_t * list)
|
||||||
{
|
{
|
||||||
list_node_t *node, *next;
|
list_node_t *node, *next;
|
||||||
|
|
||||||
for(node = list->head; node; node = next)
|
for(node = list->head; node; node = next) {
|
||||||
{
|
|
||||||
next = node->next;
|
next = node->next;
|
||||||
list_free_node(list, node);
|
list_free_node(list, node);
|
||||||
}
|
}
|
||||||
|
@ -173,23 +168,21 @@ void list_delete_list(list_t *list)
|
||||||
|
|
||||||
/* Traversing */
|
/* Traversing */
|
||||||
|
|
||||||
void list_foreach_node(list_t *list, list_action_node_t action)
|
void list_foreach_node(list_t * list, list_action_node_t action)
|
||||||
{
|
{
|
||||||
list_node_t *node, *next;
|
list_node_t *node, *next;
|
||||||
|
|
||||||
for(node = list->head; node; node = next)
|
for(node = list->head; node; node = next) {
|
||||||
{
|
|
||||||
next = node->next;
|
next = node->next;
|
||||||
action(node);
|
action(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_foreach(list_t *list, list_action_t action)
|
void list_foreach(list_t * list, list_action_t action)
|
||||||
{
|
{
|
||||||
list_node_t *node, *next;
|
list_node_t *node, *next;
|
||||||
|
|
||||||
for(node = list->head; node; node = next)
|
for(node = list->head; node; node = next) {
|
||||||
{
|
|
||||||
next = node->next;
|
next = node->next;
|
||||||
if(node->data)
|
if(node->data)
|
||||||
action(node->data);
|
action(node->data);
|
||||||
|
|
|
@ -17,14 +17,13 @@
|
||||||
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
|
||||||
$Id: list.h,v 1.1.2.6 2002/06/21 10:11:11 guus Exp $
|
$Id: list.h,v 1.1.2.7 2002/09/09 21:49:16 guus Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __TINC_LIST_H__
|
#ifndef __TINC_LIST_H__
|
||||||
#define __TINC_LIST_H__
|
#define __TINC_LIST_H__
|
||||||
|
|
||||||
typedef struct list_node_t
|
typedef struct list_node_t {
|
||||||
{
|
|
||||||
struct list_node_t *prev;
|
struct list_node_t *prev;
|
||||||
struct list_node_t *next;
|
struct list_node_t *next;
|
||||||
|
|
||||||
|
@ -36,8 +35,7 @@ typedef struct list_node_t
|
||||||
typedef void (*list_action_t) (const void *);
|
typedef void (*list_action_t) (const void *);
|
||||||
typedef void (*list_action_node_t) (const list_node_t *);
|
typedef void (*list_action_node_t) (const list_node_t *);
|
||||||
|
|
||||||
typedef struct list_t
|
typedef struct list_t {
|
||||||
{
|
|
||||||
list_node_t *head;
|
list_node_t *head;
|
||||||
list_node_t *tail;
|
list_node_t *tail;
|
||||||
int count;
|
int count;
|
||||||
|
|
41
lib/utils.c
41
lib/utils.c
|
@ -48,17 +48,16 @@ int charhex2bin(char c)
|
||||||
void hex2bin(char *src, char *dst, int length)
|
void hex2bin(char *src, char *dst, int length)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i=0; i<length; i++)
|
for(i = 0; i < length; i++)
|
||||||
dst[i] = charhex2bin(src[i*2])*16 + charhex2bin(src[i*2+1]);
|
dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bin2hex(char *src, char *dst, int length)
|
void bin2hex(char *src, char *dst, int length)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i=length-1; i>=0; i--)
|
for(i = length - 1; i >= 0; i--) {
|
||||||
{
|
dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
|
||||||
dst[i*2+1] = hexadecimals[(unsigned char)src[i] & 15];
|
dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
|
||||||
dst[i*2] = hexadecimals[(unsigned char)src[i]>>4];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,21 +65,21 @@ void bin2hex(char *src, char *dst, int length)
|
||||||
void cp_trace()
|
void cp_trace()
|
||||||
{
|
{
|
||||||
syslog(LOG_DEBUG, "Checkpoint trace: %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d...",
|
syslog(LOG_DEBUG, "Checkpoint trace: %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d...",
|
||||||
cp_file[(cp_index+15)%16], cp_line[(cp_index+15)%16],
|
cp_file[(cp_index + 15) % 16], cp_line[(cp_index + 15) % 16],
|
||||||
cp_file[(cp_index+14)%16], cp_line[(cp_index+14)%16],
|
cp_file[(cp_index + 14) % 16], cp_line[(cp_index + 14) % 16],
|
||||||
cp_file[(cp_index+13)%16], cp_line[(cp_index+13)%16],
|
cp_file[(cp_index + 13) % 16], cp_line[(cp_index + 13) % 16],
|
||||||
cp_file[(cp_index+12)%16], cp_line[(cp_index+12)%16],
|
cp_file[(cp_index + 12) % 16], cp_line[(cp_index + 12) % 16],
|
||||||
cp_file[(cp_index+11)%16], cp_line[(cp_index+11)%16],
|
cp_file[(cp_index + 11) % 16], cp_line[(cp_index + 11) % 16],
|
||||||
cp_file[(cp_index+10)%16], cp_line[(cp_index+10)%16],
|
cp_file[(cp_index + 10) % 16], cp_line[(cp_index + 10) % 16],
|
||||||
cp_file[(cp_index+9)%16], cp_line[(cp_index+9)%16],
|
cp_file[(cp_index + 9) % 16], cp_line[(cp_index + 9) % 16],
|
||||||
cp_file[(cp_index+8)%16], cp_line[(cp_index+8)%16],
|
cp_file[(cp_index + 8) % 16], cp_line[(cp_index + 8) % 16],
|
||||||
cp_file[(cp_index+7)%16], cp_line[(cp_index+7)%16],
|
cp_file[(cp_index + 7) % 16], cp_line[(cp_index + 7) % 16],
|
||||||
cp_file[(cp_index+6)%16], cp_line[(cp_index+6)%16],
|
cp_file[(cp_index + 6) % 16], cp_line[(cp_index + 6) % 16],
|
||||||
cp_file[(cp_index+5)%16], cp_line[(cp_index+5)%16],
|
cp_file[(cp_index + 5) % 16], cp_line[(cp_index + 5) % 16],
|
||||||
cp_file[(cp_index+4)%16], cp_line[(cp_index+4)%16],
|
cp_file[(cp_index + 4) % 16], cp_line[(cp_index + 4) % 16],
|
||||||
cp_file[(cp_index+3)%16], cp_line[(cp_index+3)%16],
|
cp_file[(cp_index + 3) % 16], cp_line[(cp_index + 3) % 16],
|
||||||
cp_file[(cp_index+2)%16], cp_line[(cp_index+2)%16],
|
cp_file[(cp_index + 2) % 16], cp_line[(cp_index + 2) % 16],
|
||||||
cp_file[(cp_index+1)%16], cp_line[(cp_index+1)%16],
|
cp_file[(cp_index + 1) % 16], cp_line[(cp_index + 1) % 16],
|
||||||
cp_file[cp_index], cp_line[cp_index]
|
cp_file[cp_index], cp_line[cp_index]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
10
lib/utils.h
10
lib/utils.h
|
@ -46,12 +46,12 @@ extern volatile char *cp_file[];
|
||||||
extern volatile int cp_index;
|
extern volatile int cp_index;
|
||||||
extern void cp_trace(void);
|
extern void cp_trace(void);
|
||||||
|
|
||||||
#define cp() { cp_line[cp_index] = __LINE__; cp_file[cp_index] = __FILE__; cp_index++; cp_index %= 16; }
|
#define cp() { cp_line[cp_index] = __LINE__; cp_file[cp_index] = __FILE__; cp_index++; cp_index %= 16; }
|
||||||
#define ecp() { fprintf(stderr, "Explicit checkpoint in %s line %d\n", __FILE__, __LINE__); }
|
#define ecp() { fprintf(stderr, "Explicit checkpoint in %s line %d\n", __FILE__, __LINE__); }
|
||||||
#else
|
#else
|
||||||
#define cp()
|
#define cp()
|
||||||
#define ecp()
|
#define ecp()
|
||||||
#define cp_trace()
|
#define cp_trace()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern void hex2bin(char *src, char *dst, int length);
|
extern void hex2bin(char *src, char *dst, int length);
|
||||||
|
|
Loading…
Reference in a new issue