Cleanup s24s code, remove an ugly mem leak, thanks to Vasya P. for pointing at the bug and suggesting a fix

dynamic-accesslists
erdgeist 15 years ago
parent d9287403a8
commit 6eeb16123c

@ -79,7 +79,7 @@ static time_t ot_start_time;
typedef union stats_network_node stats_network_node; typedef union stats_network_node stats_network_node;
union stats_network_node { union stats_network_node {
int counters[STATS_NETWORK_NODE_COUNT]; size_t counters[STATS_NETWORK_NODE_COUNT];
stats_network_node *children[STATS_NETWORK_NODE_COUNT]; stats_network_node *children[STATS_NETWORK_NODE_COUNT];
}; };
@ -87,46 +87,40 @@ union stats_network_node {
static stats_network_node *stats_network_counters_root; static stats_network_node *stats_network_counters_root;
#endif #endif
static int stat_increase_network_count( stats_network_node **node, int depth, uintptr_t ip ) { static int stat_increase_network_count( stats_network_node **pnode, int depth, uintptr_t ip ) {
int foo = __LDR(ip,depth); int foo = __LDR(ip,depth);
stats_network_node *node;
if( !*node ) { if( !*pnode ) {
*node = malloc( sizeof( stats_network_node ) ); *pnode = malloc( sizeof( stats_network_node ) );
if( !*node ) if( !*pnode )
return -1; return -1;
memset( *node, 0, sizeof( stats_network_node ) ); memset( *pnode, 0, sizeof( stats_network_node ) );
} }
node = *pnode;
if( depth < STATS_NETWORK_NODE_MAXDEPTH ) if( depth < STATS_NETWORK_NODE_MAXDEPTH )
return stat_increase_network_count( &(*node)->children[ foo ], depth+STATS_NETWORK_NODE_BITWIDTH, ip ); return stat_increase_network_count( node->children + foo, depth+STATS_NETWORK_NODE_BITWIDTH, ip );
(*node)->counters[ foo ]++; node->counters[ foo ]++;
return 0; return 0;
} }
static int stats_shift_down_network_count( stats_network_node **node, int depth, int shift ) { static int stats_shift_down_network_count( stats_network_node **node, int depth, int shift ) {
int i, rest = 0; int i, rest = 0;
if( !*node ) return 0;
depth += STATS_NETWORK_NODE_BITWIDTH; if( !*node )
if( depth == STATS_NETWORK_NODE_MAXDEPTH ) { return 0;
for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
rest += (*node)->counters[i] >>= shift;
return rest;
}
for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) {
stats_network_node **childnode = &(*node)->children[i];
int rest_val;
if( !*childnode ) continue;
rest += rest_val = stats_shift_down_network_count( childnode, depth, shift );
if( rest_val ) continue; for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
if( depth < STATS_NETWORK_NODE_MAXDEPTH )
rest += stats_shift_down_network_count( (*node)->children + i, depth+STATS_NETWORK_NODE_BITWIDTH, shift );
else
rest += (*node)->counters[i] >>= shift;
free( (*node)->children[i] ); if( !rest ) {
(*node)->children[i] = NULL; free( *node );
*node = NULL;
} }
return rest; return rest;
@ -135,15 +129,15 @@ static int stats_shift_down_network_count( stats_network_node **node, int depth,
static size_t stats_get_highscore_networks( stats_network_node *node, int depth, ot_ip6 node_value, size_t *scores, ot_ip6 *networks, int network_count, int limit ) { static size_t stats_get_highscore_networks( stats_network_node *node, int depth, ot_ip6 node_value, size_t *scores, ot_ip6 *networks, int network_count, int limit ) {
size_t score = 0; size_t score = 0;
int i; int i;
malloc(100);
if( !node ) return 0; if( !node ) return 0;
if( depth < limit ) { if( depth < limit ) {
for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
if( node->children[i] ) { if( node->children[i] ) {
__STR(node_value,depth,i); __STR(node_value,depth,i);
score += stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit ); score += stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit );
} }
return score; return score;
} }
@ -277,8 +271,7 @@ bailout_error:
r = reply; r = reply;
success: success:
stats_shift_down_network_count( &slash24s_network_counters_root, 0, sizeof(int)*8-1 ); stats_shift_down_network_count( &slash24s_network_counters_root, 0, sizeof(int)*8-1 );
if( slash24s_network_counters_root )
free( slash24s_network_counters_root );
return r-reply; return r-reply;
} }

Loading…
Cancel
Save