diff options
author | erdgeist <> | 2007-10-19 05:21:01 +0000 |
---|---|---|
committer | erdgeist <> | 2007-10-19 05:21:01 +0000 |
commit | cab763d4ee209ff4cf0ee2b44ee7d1158668267c (patch) | |
tree | bb868bedbc70c3ba8cb44126baf7e43a97240355 /trackerlogic.c | |
parent | 714ea1b88ea4f35968e7b6c909dfa0139a20e872 (diff) |
Release exactly all unused bytes in mmap allocation.
Diffstat (limited to 'trackerlogic.c')
-rw-r--r-- | trackerlogic.c | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/trackerlogic.c b/trackerlogic.c index 3f09d56..2cc1ca6 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
@@ -302,10 +302,20 @@ size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply | |||
302 | return r - reply; | 302 | return r - reply; |
303 | } | 303 | } |
304 | 304 | ||
305 | /* Release memory we allocated too much */ | ||
306 | static void fix_mmapallocation( void *buf, size_t old_alloc, size_t new_alloc ) { | ||
307 | int res, page_size = getpagesize(); | ||
308 | size_t old_pages = 1 + old_alloc / page_size; | ||
309 | size_t new_pages = 1 + new_alloc / page_size; | ||
310 | |||
311 | if( old_pages != new_pages ) | ||
312 | munmap( ((char*)buf) + new_pages * page_size, old_alloc - new_pages * page_size ); | ||
313 | } | ||
314 | |||
305 | /* Fetch full scrape info for all torrents */ | 315 | /* Fetch full scrape info for all torrents */ |
306 | size_t return_fullscrape_for_tracker( char **reply ) { | 316 | size_t return_fullscrape_for_tracker( char **reply ) { |
307 | size_t torrent_count = 0, j; | 317 | size_t torrent_count = 0, j; |
308 | size_t allocated, replysize, usedpages; | 318 | size_t allocated, replysize; |
309 | int i, k; | 319 | int i, k; |
310 | char *r; | 320 | char *r; |
311 | 321 | ||
@@ -338,17 +348,14 @@ size_t return_fullscrape_for_tracker( char **reply ) { | |||
338 | *r++='e'; *r++='e'; | 348 | *r++='e'; *r++='e'; |
339 | 349 | ||
340 | replysize = ( r - *reply ); | 350 | replysize = ( r - *reply ); |
341 | if( allocated > replysize ) { | 351 | fix_mmapallocation( *reply, allocated, replysize ); |
342 | usedpages = 1 + ( replysize / getpagesize() ); | ||
343 | munmap( *reply + usedpages * getpagesize(), allocated - replysize ); | ||
344 | } | ||
345 | 352 | ||
346 | return replysize; | 353 | return replysize; |
347 | } | 354 | } |
348 | 355 | ||
349 | size_t return_memstat_for_tracker( char **reply ) { | 356 | size_t return_memstat_for_tracker( char **reply ) { |
350 | size_t torrent_count = 0, j; | 357 | size_t torrent_count = 0, j; |
351 | size_t allocated, replysize, usedpages; | 358 | size_t allocated, replysize; |
352 | int i, k; | 359 | int i, k; |
353 | char *r; | 360 | char *r; |
354 | 361 | ||
@@ -375,10 +382,7 @@ size_t return_memstat_for_tracker( char **reply ) { | |||
375 | } | 382 | } |
376 | 383 | ||
377 | replysize = ( r - *reply ); | 384 | replysize = ( r - *reply ); |
378 | if( allocated > replysize ) { | 385 | fix_mmapallocation( *reply, allocated, replysize ); |
379 | usedpages = 1 + ( replysize / getpagesize() ); | ||
380 | munmap( *reply + usedpages * getpagesize(), allocated - replysize ); | ||
381 | } | ||
382 | 386 | ||
383 | return replysize; | 387 | return replysize; |
384 | } | 388 | } |