summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
authorerdgeist <>2007-10-17 14:43:14 +0000
committererdgeist <>2007-10-17 14:43:14 +0000
commit8e78ac70121b3d634cc224726b169e26938525d9 (patch)
treeb319e83d0062e0c60e4edba50dcd771a14b50ded /trackerlogic.c
parent1e6b48cbc0f426901a9fa5e7045a52f28d110361 (diff)
HEADS UP: you need to check out the latest libowfat! Huge allocations are now taken from mmap()ed regions, not from heap anymore
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index ee7b7fc..3f09d56 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -27,8 +27,8 @@ static ot_vector accesslist;
27#define WANT_ACCESS_CONTROL 27#define WANT_ACCESS_CONTROL
28#endif 28#endif
29 29
30size_t changeset_size = 0; 30static size_t changeset_size = 0;
31time_t last_clean_time = 0; 31static time_t last_clean_time = 0;
32 32
33/* Converter function from memory to human readable hex strings 33/* Converter function from memory to human readable hex strings
34 - definitely not thread safe!!! 34 - definitely not thread safe!!!
@@ -305,14 +305,16 @@ size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply
305/* Fetch full scrape info for all torrents */ 305/* Fetch full scrape info for all torrents */
306size_t return_fullscrape_for_tracker( char **reply ) { 306size_t return_fullscrape_for_tracker( char **reply ) {
307 size_t torrent_count = 0, j; 307 size_t torrent_count = 0, j;
308 size_t allocated, replysize, usedpages;
308 int i, k; 309 int i, k;
309 char *r; 310 char *r;
310 311
311 for( i=0; i<256; ++i ) 312 for( i=0; i<256; ++i )
312 torrent_count += all_torrents[i].size; 313 torrent_count += all_torrents[i].size;
313 314
314 // one extra for pro- and epilogue 315 /* one extra for pro- and epilogue */
315 if( !( r = *reply = malloc( 100*(1+torrent_count) ) ) ) return 0; 316 allocated = 100*(1+torrent_count);
317 if( !( r = *reply = mmap( NULL, allocated, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0;
316 318
317 memmove( r, "d5:filesd", 9 ); r += 9; 319 memmove( r, "d5:filesd", 9 ); r += 9;
318 for( i=0; i<256; ++i ) { 320 for( i=0; i<256; ++i ) {
@@ -334,11 +336,19 @@ size_t return_fullscrape_for_tracker( char **reply ) {
334 } 336 }
335 337
336 *r++='e'; *r++='e'; 338 *r++='e'; *r++='e';
337 return r - *reply; 339
340 replysize = ( r - *reply );
341 if( allocated > replysize ) {
342 usedpages = 1 + ( replysize / getpagesize() );
343 munmap( *reply + usedpages * getpagesize(), allocated - replysize );
344 }
345
346 return replysize;
338} 347}
339 348
340size_t return_memstat_for_tracker( char **reply ) { 349size_t return_memstat_for_tracker( char **reply ) {
341 size_t torrent_count = 0, j; 350 size_t torrent_count = 0, j;
351 size_t allocated, replysize, usedpages;
342 int i, k; 352 int i, k;
343 char *r; 353 char *r;
344 354
@@ -347,7 +357,8 @@ size_t return_memstat_for_tracker( char **reply ) {
347 torrent_count += torrents_list->size; 357 torrent_count += torrents_list->size;
348 } 358 }
349 359
350 if( !( r = *reply = malloc( 256*32 + (43+OT_POOLS_COUNT*32)*torrent_count ) ) ) return 0; 360 allocated = 256*32 + (43+OT_POOLS_COUNT*32)*torrent_count;
361 if( !( r = *reply = mmap( NULL, allocated, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0;
351 362
352 for( i=0; i<256; ++i ) 363 for( i=0; i<256; ++i )
353 r += sprintf( r, "%02X: %08X %08X\n", i, (unsigned int)all_torrents[i].size, (unsigned int)all_torrents[i].space ); 364 r += sprintf( r, "%02X: %08X %08X\n", i, (unsigned int)all_torrents[i].size, (unsigned int)all_torrents[i].space );
@@ -363,7 +374,13 @@ size_t return_memstat_for_tracker( char **reply ) {
363 } 374 }
364 } 375 }
365 376
366 return r - *reply; 377 replysize = ( r - *reply );
378 if( allocated > replysize ) {
379 usedpages = 1 + ( replysize / getpagesize() );
380 munmap( *reply + usedpages * getpagesize(), allocated - replysize );
381 }
382
383 return replysize;
367} 384}
368 385
369/* Fetches scrape info for a specific torrent */ 386/* Fetches scrape info for a specific torrent */
@@ -506,9 +523,7 @@ size_t return_changeset_for_tracker( char **reply ) {
506 523
507 clean_all_torrents(); 524 clean_all_torrents();
508 525
509 *reply = malloc( 8 + changeset_size + 2 ); 526 if( !( *reply = mmap( NULL, 8 + changeset_size + 2, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 ) ) ) return 0;
510 if( !*reply )
511 return 0;
512 527
513 memmove( *reply, "d4:syncd", 8 ); 528 memmove( *reply, "d4:syncd", 8 );
514 for( i = 0; i < changeset.size; ++i ) { 529 for( i = 0; i < changeset.size; ++i ) {