An open and free bittorrent tracker https://erdgeist.org/gitweb/opentracker
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

77 lines
2.2 KiB

  1. /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
  2. It is considered beerware. Prost. Skol. Cheers or whatever.
  3. $id$ */
  4. /* System */
  5. #include <sys/types.h>
  6. #include <sys/mman.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/uio.h>
  10. /* Libowfat */
  11. /* Opentracker */
  12. #include "ot_iovec.h"
  13. void *iovec_increase( int *iovec_entries, struct iovec **iovector, size_t new_alloc ) {
  14. void *new_ptr = realloc( *iovector, (1 + *iovec_entries ) * sizeof( struct iovec ) );
  15. if( !new_ptr )
  16. return NULL;
  17. *iovector = new_ptr;
  18. new_ptr = mmap( NULL, new_alloc, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0 );
  19. if( !new_ptr )
  20. return NULL;
  21. ((*iovector)[*iovec_entries]).iov_base = new_ptr;
  22. ((*iovector)[*iovec_entries]).iov_len = new_alloc;
  23. ++*iovec_entries;
  24. return new_ptr;
  25. }
  26. void iovec_free( int *iovec_entries, struct iovec **iovector ) {
  27. int i;
  28. for( i=0; i<*iovec_entries; ++i )
  29. munmap( ((*iovector)[i]).iov_base, ((*iovector)[i]).iov_len );
  30. *iovec_entries = 0;
  31. }
  32. void iovec_fixlast( int *iovec_entries, struct iovec **iovector, void *last_ptr ) {
  33. int page_size = getpagesize();
  34. size_t old_alloc, new_alloc, old_pages, new_pages;
  35. char * base = (char*)((*iovector)[ *iovec_entries - 1 ]).iov_base;
  36. if( !*iovec_entries ) return;
  37. old_alloc = ((*iovector)[ *iovec_entries - 1 ]).iov_len;
  38. new_alloc = ((char*)last_ptr) - base;
  39. old_pages = 1 + old_alloc / page_size;
  40. new_pages = 1 + new_alloc / page_size;
  41. if( old_pages != new_pages )
  42. munmap( base + new_pages * page_size, old_alloc - new_pages * page_size );
  43. ((*iovector)[*iovec_entries - 1 ]).iov_len = new_alloc;
  44. }
  45. void *iovec_fix_increase_or_free( int *iovec_entries, struct iovec **iovector, void *last_ptr, size_t new_alloc ) {
  46. void *new_ptr;
  47. iovec_fixlast( iovec_entries, iovector, last_ptr );
  48. if( !( new_ptr = iovec_increase( iovec_entries, iovector, new_alloc ) ) )
  49. iovec_free( iovec_entries, iovector );
  50. return new_ptr;
  51. }
  52. size_t iovec_length( int *iovec_entries, struct iovec **iovector ) {
  53. size_t length = 0;
  54. int i;
  55. for( i=0; i<*iovec_entries; ++i )
  56. length += ((*iovector)[i]).iov_len;
  57. return length;
  58. }
  59. const char *g_version_iovec_c = "$Source: /home/cvsroot/opentracker/ot_iovec.c,v $: $Revision: 1.7 $\n";