Changeset 10274


Ignore:
Timestamp:
Feb 23, 2010, 6:06:45 AM (12 years ago)
Author:
charles
Message:

(trunk libT) add a wrapper function tr_valloc() to try posix_memalign(), getpagesize(), valloc() etc

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r10239 r10274  
    1515#endif
    1616
     17#if defined(SYS_DARWIN)
     18 #define HAVE_GETPAGESIZE
     19 #define HAVE_VALLOC
     20 #undef HAVE_POSIX_MEMALIGN /* not supported on OS X 10.5 and lower */
     21#endif
     22
     23#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_VALLOC)
     24 #define _XOPEN_SOURCE 600 /* pick posix_memalign and valloc declarations */
     25#endif
     26
    1727#include <assert.h>
    1828#include <ctype.h> /* isalpha, tolower */
     
    2838#include <sys/types.h>
    2939#include <sys/stat.h>
    30 #include <unistd.h> /* usleep, stat, getcwd */
     40#include <unistd.h> /* usleep, stat, getcwd, getpagesize */
    3141
    3242#include "event.h"
     
    14411451    return 0;
    14421452}
     1453
     1454/***
     1455****
     1456***/
     1457
     1458void*
     1459tr_valloc( size_t bufLen )
     1460{
     1461    size_t allocLen;
     1462    void * buf = NULL;
     1463    static size_t pageSize = 0;
     1464
     1465    if( !pageSize ) {
     1466#ifdef HAVE_GETPAGESIZE
     1467        pageSize = getpagesize();
     1468#else /* guess */
     1469        pageSize = 4096;
     1470#endif
     1471    }
     1472
     1473    allocLen = pageSize;
     1474    while( allocLen < bufLen )
     1475        allocLen += pageSize;
     1476
     1477#ifdef HAVE_POSIX_MEMALIGN
     1478    if( !buf )
     1479        posix_memalign( &buf, pageSize, allocLen );
     1480#endif
     1481#ifdef HAVE_VALLOC
     1482    if( !buf )
     1483        buf = valloc( allocLen );
     1484#endif
     1485    if( !buf )
     1486        buf = malloc( allocLen );
     1487
     1488    tr_dbg( "tr_valloc(%zu) allocating %zu bytes", bufLen, allocLen );
     1489    return buf;
     1490}
  • trunk/libtransmission/utils.h

    r10239 r10274  
    335335#define tr_renew( struct_type, mem, n_structs )    \
    336336    ( (struct_type *) realloc ( ( mem ), ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
     337
     338void* tr_valloc( size_t bufLen );
    337339
    338340/**
  • trunk/libtransmission/verify.c

    r10264 r10274  
    1919#if defined(HAVE_POSIX_FADVISE) || defined(SYS_DARWIN)
    2020 #include <fcntl.h> /* posix_fadvise() / fcntl() */
    21 #endif
    22 #if defined(SYS_DARWIN)
    23  #define HAVE_GETPAGESIZE
    24  //#define HAVE_POSIX_MEMALIGN requires Mac OS X 10.6
    25  #define HAVE_VALLOC
    2621#endif
    2722
     
    6661    const time_t begin = tr_time( );
    6762    time_t end;
    68     int64_t buflen;
    69     uint8_t * buffer = NULL;
    70     const int64_t maxbuf = 16384;
    71 
    72 #ifdef HAVE_GETPAGESIZE
    73     buflen = getpagesize();
    74     while( buflen * 2 <= maxbuf )
    75         buflen *= 2;
    76 #else
    77     buflen = maxbuf;
    78 #endif
    79 
    80 #ifdef HAVE_POSIX_MEMALIGN
    81     if( !buffer )
    82         posix_memalign( (void**)&buffer, getpagesize(), buflen );
    83 #endif
    84 #ifdef HAVE_VALLOC
    85     if( !buffer )
    86         buffer = valloc( buflen );
    87 #endif
    88     if( !buffer )
    89         buffer = malloc( buflen );
     63    const int64_t buflen = 16384;
     64    uint8_t * buffer = tr_valloc( buflen );
    9065
    9166    SHA1_Init( &sha );
Note: See TracChangeset for help on using the changeset viewer.