From a9ab9b0c0dbd5de29d1aef9927cca92ac522d4cb Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 8 Dec 2008 02:23:21 +0000 Subject: Make all torrents in their buckets sorted again. --- ot_mutex.c | 4 ++-- ot_vector.c | 12 ++++++------ trackerlogic.h | 11 +++++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/ot_mutex.c b/ot_mutex.c index 2300f5c..a28144b 100644 --- a/ot_mutex.c +++ b/ot_mutex.c @@ -80,7 +80,7 @@ ot_vector *mutex_bucket_lock( int bucket ) { } ot_vector *mutex_bucket_lock_by_hash( ot_hash *hash ) { - int bucket = uint32_read( *hash ) % OT_BUCKET_COUNT; + int bucket = uint32_read_big( (char*)*hash ) >> OT_BUCKET_COUNT_SHIFT; /* Can block */ mutex_bucket_lock( bucket ); @@ -95,7 +95,7 @@ void mutex_bucket_unlock( int bucket ) { } void mutex_bucket_unlock_by_hash( ot_hash *hash ) { - mutex_bucket_unlock( uint32_read( *hash ) % OT_BUCKET_COUNT ); + mutex_bucket_unlock( uint32_read_big( (char*)*hash ) >> OT_BUCKET_COUNT_SHIFT ); } /* TaskQueue Magic */ diff --git a/ot_vector.c b/ot_vector.c index 154d1c8..f92f7ac 100644 --- a/ot_vector.c +++ b/ot_vector.c @@ -17,8 +17,8 @@ #include "uint16.h" static int vector_compare_peer(const void *peer1, const void *peer2 ) { - int32_t cmp = READ32(peer1,0) - READ32(peer2,0); - if (cmp == 0) cmp = READ16(peer1,4) - READ16(peer2,4); + int32_t cmp = READ32(peer2,0) - READ32(peer1,0); + if (cmp == 0) cmp = READ16(peer2,4) - READ16(peer1,4); return cmp; } @@ -36,10 +36,10 @@ void *binary_search( const void * const key, const void * base, const size_t mem *exactmatch = 1; while( mc ) { - int32_t cmp = key_cache - READ32(lookat,0); + int32_t cmp = READ32(lookat,0) - key_cache; if (cmp == 0) { for( offs = 4; cmp == 0 && offs < compare_size; offs += 4 ) - cmp = READ32(key,offs) - READ32(lookat,offs); + cmp = READ32(lookat,offs) - READ32(key,offs); if( cmp == 0 ) return (void *)lookat; } @@ -65,8 +65,8 @@ ot_peer *binary_search_peer( const ot_peer * const peer, const ot_peer * base, c *exactmatch = 1; while( mc ) { - int32_t cmp = low - READ32(lookat,0); - if(cmp == 0) cmp = high - READ16(lookat,4); + int32_t cmp = READ32(lookat,0) - low; + if(cmp == 0) cmp = READ16(lookat,4) - high; if(cmp == 0) return (ot_peer*)lookat; if (cmp < 0) { diff --git a/trackerlogic.h b/trackerlogic.h index c7e2e97..75e98d2 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -38,16 +38,19 @@ typedef time_t ot_time; /* If peers come back before 10 minutes, don't live sync them */ #define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 -/* We maintain a list of 1024 pointers to sorted list of ot_torrent structs - Sort key is, of course, its hash */ -#define OT_BUCKET_COUNT 1024 - /* Number of tracker admin ip addresses allowed */ #define OT_ADMINIP_MAX 64 #define OT_MAX_THREADS 16 #define OT_PEER_TIMEOUT 45 +/* We maintain a list of 1024 pointers to sorted list of ot_torrent structs + Sort key is, of course, its hash */ +#define OT_BUCKET_COUNT_BITS 10 + +#define OT_BUCKET_COUNT (1<