summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ot_vector.c53
-rw-r--r--ot_vector.h4
2 files changed, 57 insertions, 0 deletions
diff --git a/ot_vector.c b/ot_vector.c
index 2a6d1b3..e60b27c 100644
--- a/ot_vector.c
+++ b/ot_vector.c
@@ -11,6 +11,40 @@
11#include "trackerlogic.h" 11#include "trackerlogic.h"
12#include "ot_vector.h" 12#include "ot_vector.h"
13 13
14#ifdef _DEBUG_VECTOR
15#include <stdio.h>
16
17static uint64_t vector_debug_inc[32];
18static uint64_t vector_debug_noinc[32];
19static uint64_t vector_debug_dec[32];
20static uint64_t vector_debug_nodec[32];
21static void vector_debug( size_t old_size, ssize_t diff_size, size_t old_space, ssize_t diff_space ) {
22 int x = 0;
23 while( old_space ) { old_space>>=1; ++x; }
24 old_size = old_size;
25
26 if( diff_size == -1 )
27 if( diff_space ) vector_debug_dec[x]++; else vector_debug_nodec[x]++;
28 else
29 if( diff_space ) vector_debug_inc[x]++; else vector_debug_noinc[x]++;
30
31}
32
33size_t vector_info( char * reply ) {
34 char * r = reply;
35 int i;
36 for( i=1; i<28; ++i )
37 r += sprintf( r, " inc % 12d -> % 12d: % 16lld\n", 1<<(i-1), 8<<(i-1), vector_debug_inc[i] );
38 for( i=1; i<28; ++i )
39 r += sprintf( r, "noinc % 12d -> % 12d: % 16lld\n", 1<<(i-1), 1<<(i-1), vector_debug_noinc[i] );
40 for( i=1; i<28; ++i )
41 r += sprintf( r, " dec % 12d -> % 12d: % 16lld\n", 1<<(i-1), 4<<(i-1), vector_debug_dec[i] );
42 for( i=1; i<28; ++i )
43 r += sprintf( r, "nodec % 12d -> % 12d: % 16lld\n", 1<<(i-1), 1<<(i-1), vector_debug_nodec[i] );
44 return r - reply;
45}
46#endif
47
14/* This function gives us a binary search that returns a pointer, even if 48/* This function gives us a binary search that returns a pointer, even if
15 no exact match is found. In that case it sets exactmatch 0 and gives 49 no exact match is found. In that case it sets exactmatch 0 and gives
16 calling functions the chance to insert data 50 calling functions the chance to insert data
@@ -44,6 +78,9 @@ void *binary_search( const void * const key, const void * base, const size_t mem
44*/ 78*/
45void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch ) { 79void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch ) {
46 uint8_t *match = binary_search( key, vector->data, vector->size, member_size, compare_size, exactmatch ); 80 uint8_t *match = binary_search( key, vector->data, vector->size, member_size, compare_size, exactmatch );
81#ifdef _DEBUG_VECTOR
82 size_t old_space = vector->space;
83#endif
47 84
48 if( *exactmatch ) return match; 85 if( *exactmatch ) return match;
49 86
@@ -59,6 +96,10 @@ void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, s
59 vector->space = new_space; 96 vector->space = new_space;
60 } 97 }
61 memmove( match + member_size, match, ((uint8_t*)vector->data) + member_size * vector->size - match ); 98 memmove( match + member_size, match, ((uint8_t*)vector->data) + member_size * vector->size - match );
99
100#ifdef _DEBUG_VECTOR
101 vector_debug( vector->size, 1, old_space, vector->space - old_space );
102#endif
62 vector->size++; 103 vector->size++;
63 return match; 104 return match;
64} 105}
@@ -74,6 +115,9 @@ int vector_remove_peer( ot_vector *vector, ot_peer *peer, int hysteresis ) {
74 size_t shrink_thresh = hysteresis ? OT_VECTOR_SHRINK_THRESH : OT_VECTOR_SHRINK_RATIO; 115 size_t shrink_thresh = hysteresis ? OT_VECTOR_SHRINK_THRESH : OT_VECTOR_SHRINK_RATIO;
75 ot_peer *end = ((ot_peer*)vector->data) + vector->size; 116 ot_peer *end = ((ot_peer*)vector->data) + vector->size;
76 ot_peer *match; 117 ot_peer *match;
118#ifdef _DEBUG_VECTOR
119 size_t old_space = vector->space;
120#endif
77 121
78 if( !vector->size ) return 0; 122 if( !vector->size ) return 0;
79 match = binary_search( peer, vector->data, vector->size, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch ); 123 match = binary_search( peer, vector->data, vector->size, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch );
@@ -92,11 +136,17 @@ int vector_remove_peer( ot_vector *vector, ot_peer *peer, int hysteresis ) {
92 vector->data = NULL; 136 vector->data = NULL;
93 vector->space = 0; 137 vector->space = 0;
94 } 138 }
139#ifdef _DEBUG_VECTOR
140 vector_debug( vector->size+1, -1, old_space, vector->space - old_space );
141#endif
95 return exactmatch; 142 return exactmatch;
96} 143}
97 144
98void vector_remove_torrent( ot_vector *vector, ot_torrent *match ) { 145void vector_remove_torrent( ot_vector *vector, ot_torrent *match ) {
99 ot_torrent *end = ((ot_torrent*)vector->data) + vector->size; 146 ot_torrent *end = ((ot_torrent*)vector->data) + vector->size;
147#ifdef _DEBUG_VECTOR
148 size_t old_space = vector->space;
149#endif
100 150
101 if( !vector->size ) return; 151 if( !vector->size ) return;
102 152
@@ -109,6 +159,9 @@ void vector_remove_torrent( ot_vector *vector, ot_torrent *match ) {
109 vector->space /= OT_VECTOR_SHRINK_RATIO; 159 vector->space /= OT_VECTOR_SHRINK_RATIO;
110 vector->data = realloc( vector->data, vector->space * sizeof( ot_torrent ) ); 160 vector->data = realloc( vector->data, vector->space * sizeof( ot_torrent ) );
111 } 161 }
162#ifdef _DEBUG_VECTOR
163 vector_debug( vector->size+1, -1, old_space, vector->space - old_space );
164#endif
112} 165}
113 166
114const char *g_version_vector_c = "$Source$: $Revision$\n"; 167const char *g_version_vector_c = "$Source$: $Revision$\n";
diff --git a/ot_vector.h b/ot_vector.h
index 8f3143b..ec053fa 100644
--- a/ot_vector.h
+++ b/ot_vector.h
@@ -25,4 +25,8 @@ void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, s
25int vector_remove_peer( ot_vector *vector, ot_peer *peer, int hysteresis ); 25int vector_remove_peer( ot_vector *vector, ot_peer *peer, int hysteresis );
26void vector_remove_torrent( ot_vector *vector, ot_torrent *match ); 26void vector_remove_torrent( ot_vector *vector, ot_torrent *match );
27 27
28#ifdef _DEBUG_VECTOR
29size_t vector_info( char * reply );
30#endif
31
28#endif 32#endif