Remove posix memalign (#1520)

* refactor: remove unnecessary func tr_valloc()

We're only using it in a handful of places, and none of them need the
kind of alignment that posix_memalign() provide. So we can drop a few
dozen lines by removing the portability wrapper.
This commit is contained in:
Charles Kerr 2020-11-15 15:53:42 -06:00 committed by GitHub
parent cfce6e2e3a
commit 41f225df7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 14 additions and 105 deletions

View File

@ -534,7 +534,6 @@ set(NEEDED_FUNCTIONS
fallocate64 fallocate64
flock flock
getmntent getmntent
getpagesize
gmtime_r gmtime_r
gmtime_s gmtime_s
htonll htonll
@ -545,7 +544,6 @@ set(NEEDED_FUNCTIONS
ntohll ntohll
posix_fadvise posix_fadvise
posix_fallocate posix_fallocate
posix_memalign
pread pread
pwrite pwrite
statvfs statvfs
@ -553,8 +551,7 @@ set(NEEDED_FUNCTIONS
strlcpy strlcpy
strsep strsep
syslog syslog
uselocale uselocale)
valloc)
foreach(F ${NEEDED_FUNCTIONS}) foreach(F ${NEEDED_FUNCTIONS})
tr_make_id("${F}" F_ID) tr_make_id("${F}" F_ID)

View File

@ -251,7 +251,7 @@ static bool recalculateHash(tr_torrent* tor, tr_piece_index_t pieceIndex, uint8_
uint32_t offset = 0; uint32_t offset = 0;
bool success = true; bool success = true;
size_t const buflen = tor->blockSize; size_t const buflen = tor->blockSize;
void* buffer = tr_valloc(buflen); void* const buffer = tr_malloc(buflen);
tr_sha1_ctx_t sha; tr_sha1_ctx_t sha;
TR_ASSERT(buffer != NULL); TR_ASSERT(buffer != NULL);

View File

@ -265,7 +265,7 @@ static uint8_t* getHashInfo(tr_metainfo_builder* b)
return ret; return ret;
} }
buf = tr_valloc(b->pieceSize); buf = tr_malloc(b->pieceSize);
b->pieceIndex = 0; b->pieceIndex = 0;
totalRemain = b->totalSize; totalRemain = b->totalSize;
fd = tr_sys_file_open(b->files[fileIndex].filename, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &error); fd = tr_sys_file_open(b->files[fileIndex].filename, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &error);

View File

@ -1676,13 +1676,11 @@ static void gotNewBlocklist(tr_session* session, bool did_connect, bool did_time
} }
else /* successfully fetched the blocklist... */ else /* successfully fetched the blocklist... */
{ {
tr_sys_file_t fd;
int err; int err;
char* filename;
z_stream stream; z_stream stream;
char const* configDir = tr_sessionGetConfigDir(session); char const* configDir = tr_sessionGetConfigDir(session);
size_t const buflen = 1024 * 128; /* 128 KiB buffer */ size_t const buflen = 1024 * 128; /* 128 KiB buffer */
uint8_t* buf = tr_valloc(buflen); uint8_t* const buf = tr_malloc(buflen);
tr_error* error = NULL; tr_error* error = NULL;
/* this is an odd Magic Number required by zlib to enable gz support. /* this is an odd Magic Number required by zlib to enable gz support.
@ -1696,8 +1694,8 @@ static void gotNewBlocklist(tr_session* session, bool did_connect, bool did_time
stream.avail_in = response_byte_count; stream.avail_in = response_byte_count;
inflateInit2(&stream, windowBits); inflateInit2(&stream, windowBits);
filename = tr_buildPath(configDir, "blocklist.tmp.XXXXXX", NULL); char* const filename = tr_buildPath(configDir, "blocklist.tmp.XXXXXX", NULL);
fd = tr_sys_file_open_temp(filename, &error); tr_sys_file_t const fd = tr_sys_file_open_temp(filename, &error);
if (fd == TR_BAD_SYS_FILE) if (fd == TR_BAD_SYS_FILE)
{ {

View File

@ -10,11 +10,6 @@
#define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */ #define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
#endif #endif
#if defined(XCODE_BUILD)
#define HAVE_GETPAGESIZE
#define HAVE_VALLOC
#endif
#include <ctype.h> /* isdigit(), tolower() */ #include <ctype.h> /* isdigit(), tolower() */
#include <errno.h> #include <errno.h>
#include <float.h> /* DBL_DIG */ #include <float.h> /* DBL_DIG */
@ -554,26 +549,6 @@ int tr_strcmp0(char const* str1, char const* str2)
return 0; return 0;
} }
int tr_memcmp0(void const* lhs, void const* rhs, size_t size)
{
if (lhs != NULL && rhs != NULL)
{
return memcmp(lhs, rhs, size);
}
if (lhs != NULL)
{
return 1;
}
if (rhs != NULL)
{
return -1;
}
return 0;
}
/**** /****
***** *****
****/ ****/
@ -1749,7 +1724,7 @@ bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error)
char* buf = NULL; char* buf = NULL;
tr_sys_path_info info; tr_sys_path_info info;
uint64_t bytesLeft; uint64_t bytesLeft;
size_t const buflen = 1024 * 1024; /* 1024 KiB buffer */ size_t const buflen = 1024 * 1024; // 1024 KiB buffer
/* make sure the old file exists */ /* make sure the old file exists */
if (!tr_sys_path_get_info(oldpath, 0, &info, error)) if (!tr_sys_path_get_info(oldpath, 0, &info, error))
@ -1801,7 +1776,7 @@ bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error)
return false; return false;
} }
buf = tr_valloc(buflen); buf = tr_malloc(buflen);
bytesLeft = info.size; bytesLeft = info.size;
while (bytesLeft > 0) while (bytesLeft > 0)
@ -1853,58 +1828,6 @@ bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error)
**** ****
***/ ***/
void* tr_valloc(size_t bufLen)
{
size_t allocLen;
void* buf = NULL;
static size_t pageSize = 0;
if (pageSize == 0)
{
#if defined(HAVE_GETPAGESIZE) && !defined(_WIN32)
pageSize = (size_t)getpagesize();
#else /* guess */
pageSize = 4096;
#endif
}
allocLen = pageSize;
while (allocLen < bufLen)
{
allocLen += pageSize;
}
#ifdef HAVE_POSIX_MEMALIGN
if ((buf == NULL) && (posix_memalign(&buf, pageSize, allocLen) != 0))
{
buf = NULL; /* just retry with valloc/malloc */
}
#endif
#ifdef HAVE_VALLOC
if (buf == NULL)
{
buf = valloc(allocLen);
}
#endif
if (buf == NULL)
{
buf = tr_malloc(allocLen);
}
return buf;
}
/***
****
***/
uint64_t tr_htonll(uint64_t x) uint64_t tr_htonll(uint64_t x)
{ {
#ifdef HAVE_HTONLL #ifdef HAVE_HTONLL

View File

@ -181,8 +181,6 @@ void* tr_memdup(void const* src, size_t byteCount);
((struct_type*)tr_realloc((mem), sizeof(struct_type) * (size_t)(n_structs))) ((struct_type*)tr_realloc((mem), sizeof(struct_type) * (size_t)(n_structs)))
/* *INDENT-ON* */ /* *INDENT-ON* */
void* tr_valloc(size_t bufLen);
/** /**
* @brief make a newly-allocated copy of a substring * @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 * @param in is a void* so that callers can pass in both signed & unsigned without a cast
@ -208,11 +206,6 @@ static inline bool tr_str_is_empty(char const* value)
return value == NULL || *value == '\0'; return value == NULL || *value == '\0';
} }
/**
* @brief like memcmp() but gracefully handles NULL pointers
*/
int tr_memcmp0(void const* lhs, void const* rhs, size_t size);
char* evbuffer_free_to_str(struct evbuffer* buf, size_t* result_len); char* evbuffer_free_to_str(struct evbuffer* buf, size_t* result_len);
/** @brief similar to bsearch() but returns the index of the lower bound */ /** @brief similar to bsearch() but returns the index of the lower bound */

View File

@ -18,7 +18,7 @@
#include "platform.h" /* tr_lock() */ #include "platform.h" /* tr_lock() */
#include "torrent.h" #include "torrent.h"
#include "tr-assert.h" #include "tr-assert.h"
#include "utils.h" /* tr_valloc(), tr_free() */ #include "utils.h" /* tr_malloc(), tr_free() */
#include "verify.h" #include "verify.h"
/*** /***
@ -32,8 +32,6 @@ enum
static bool verifyTorrent(tr_torrent* tor, bool* stopFlag) static bool verifyTorrent(tr_torrent* tor, bool* stopFlag)
{ {
time_t end;
tr_sha1_ctx_t sha;
tr_sys_file_t fd = TR_BAD_SYS_FILE; tr_sys_file_t fd = TR_BAD_SYS_FILE;
uint64_t filePos = 0; uint64_t filePos = 0;
bool changed = false; bool changed = false;
@ -44,10 +42,10 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag)
tr_file_index_t prevFileIndex = !fileIndex; tr_file_index_t prevFileIndex = !fileIndex;
tr_piece_index_t pieceIndex = 0; tr_piece_index_t pieceIndex = 0;
time_t const begin = tr_time(); time_t const begin = tr_time();
size_t const buflen = 1024 * 128; /* 128 KiB buffer */ size_t const buflen = 1024 * 128; // 128 KiB buffer
uint8_t* buffer = tr_valloc(buflen); uint8_t* const buffer = tr_malloc(buflen);
sha = tr_sha1_init(); tr_sha1_ctx_t sha = tr_sha1_init();
tr_logAddTorDbg(tor, "%s", "verifying torrent..."); tr_logAddTorDbg(tor, "%s", "verifying torrent...");
tr_torrentSetChecked(tor, 0); tr_torrentSetChecked(tor, 0);
@ -157,7 +155,7 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag)
free(buffer); free(buffer);
/* stopwatch */ /* stopwatch */
end = tr_time(); time_t const end = tr_time();
tr_logAddTorDbg(tor, "Verification is done. It took %d seconds to verify %" PRIu64 " bytes (%" PRIu64 " bytes per second)", tr_logAddTorDbg(tor, "Verification is done. It took %d seconds to verify %" PRIu64 " bytes (%" PRIu64 " bytes per second)",
(int)(end - begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize / (1 + (end - begin)))); (int)(end - begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize / (1 + (end - begin))));

View File

@ -19,7 +19,7 @@
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
QAXFACTORY_BEGIN("{1e405fc2-1a3a-468b-8bd6-bfbb58770390}", "{792d1aac-53cc-4dc9-bc29-e5295fdb93a9}") QAXFACTORY_BEGIN("{1e405fc2-1a3a-468b-8bd6-bfbb58770390}", "{792d1aac-53cc-4dc9-bc29-e5295fdb93a9}")
QAXCLASS(InteropObject) QAXCLASS(InteropObject)
QAXFACTORY_END() QAXFACTORY_END() // NOLINT
// These are ActiveQt internals; declaring here as I don't like their WinMain much... // These are ActiveQt internals; declaring here as I don't like their WinMain much...
extern HANDLE qAxInstance; // NOLINT extern HANDLE qAxInstance; // NOLINT