mirror of
https://github.com/transmission/transmission.git
synced 2025-12-26 13:21:44 +00:00
(trunk libT) add a wrapper function tr_valloc() to try posix_memalign(), getpagesize(), valloc() etc
This commit is contained in:
@@ -14,6 +14,16 @@
|
||||
#define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
|
||||
#endif
|
||||
|
||||
#if defined(SYS_DARWIN)
|
||||
#define HAVE_GETPAGESIZE
|
||||
#define HAVE_VALLOC
|
||||
#undef HAVE_POSIX_MEMALIGN /* not supported on OS X 10.5 and lower */
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_VALLOC)
|
||||
#define _XOPEN_SOURCE 600 /* pick posix_memalign and valloc declarations */
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h> /* isalpha, tolower */
|
||||
#include <errno.h>
|
||||
@@ -27,7 +37,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h> /* usleep, stat, getcwd */
|
||||
#include <unistd.h> /* usleep, stat, getcwd, getpagesize */
|
||||
|
||||
#include "event.h"
|
||||
|
||||
@@ -1440,3 +1450,41 @@ tr_moveFile( const char * oldpath, const char * newpath, tr_bool * renamed )
|
||||
unlink( oldpath );
|
||||
return 0;
|
||||
}
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
||||
void*
|
||||
tr_valloc( size_t bufLen )
|
||||
{
|
||||
size_t allocLen;
|
||||
void * buf = NULL;
|
||||
static size_t pageSize = 0;
|
||||
|
||||
if( !pageSize ) {
|
||||
#ifdef HAVE_GETPAGESIZE
|
||||
pageSize = getpagesize();
|
||||
#else /* guess */
|
||||
pageSize = 4096;
|
||||
#endif
|
||||
}
|
||||
|
||||
allocLen = pageSize;
|
||||
while( allocLen < bufLen )
|
||||
allocLen += pageSize;
|
||||
|
||||
#ifdef HAVE_POSIX_MEMALIGN
|
||||
if( !buf )
|
||||
posix_memalign( &buf, pageSize, allocLen );
|
||||
#endif
|
||||
#ifdef HAVE_VALLOC
|
||||
if( !buf )
|
||||
buf = valloc( allocLen );
|
||||
#endif
|
||||
if( !buf )
|
||||
buf = malloc( allocLen );
|
||||
|
||||
tr_dbg( "tr_valloc(%zu) allocating %zu bytes", bufLen, allocLen );
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -335,6 +335,8 @@ static inline void* tr_memdup( const void * src, int byteCount )
|
||||
#define tr_renew( struct_type, mem, n_structs ) \
|
||||
( (struct_type *) realloc ( ( mem ), ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
|
||||
|
||||
void* tr_valloc( size_t bufLen );
|
||||
|
||||
/**
|
||||
* @brief make a newly-allocated copy of a substring
|
||||
* @param in is a void* so that callers can pass in both signed & unsigned without a cast
|
||||
|
||||
@@ -19,11 +19,6 @@
|
||||
#if defined(HAVE_POSIX_FADVISE) || defined(SYS_DARWIN)
|
||||
#include <fcntl.h> /* posix_fadvise() / fcntl() */
|
||||
#endif
|
||||
#if defined(SYS_DARWIN)
|
||||
#define HAVE_GETPAGESIZE
|
||||
//#define HAVE_POSIX_MEMALIGN requires Mac OS X 10.6
|
||||
#define HAVE_VALLOC
|
||||
#endif
|
||||
|
||||
#include <openssl/sha.h>
|
||||
|
||||
@@ -65,28 +60,8 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||
tr_piece_index_t pieceIndex = 0;
|
||||
const time_t begin = tr_time( );
|
||||
time_t end;
|
||||
int64_t buflen;
|
||||
uint8_t * buffer = NULL;
|
||||
const int64_t maxbuf = 16384;
|
||||
|
||||
#ifdef HAVE_GETPAGESIZE
|
||||
buflen = getpagesize();
|
||||
while( buflen * 2 <= maxbuf )
|
||||
buflen *= 2;
|
||||
#else
|
||||
buflen = maxbuf;
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_POSIX_MEMALIGN
|
||||
if( !buffer )
|
||||
posix_memalign( (void**)&buffer, getpagesize(), buflen );
|
||||
#endif
|
||||
#ifdef HAVE_VALLOC
|
||||
if( !buffer )
|
||||
buffer = valloc( buflen );
|
||||
#endif
|
||||
if( !buffer )
|
||||
buffer = malloc( buflen );
|
||||
const int64_t buflen = 16384;
|
||||
uint8_t * buffer = tr_valloc( buflen );
|
||||
|
||||
SHA1_Init( &sha );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user