all: yet more cleanups
Changes include: - use standard names for PE constants - add some more "noexcept" - improve upx_is_integral type-trait - introduce is_same_all and is_same_any type-traits - prepare TE-size checks in packer.h - CI updates
This commit is contained in:
@@ -69,7 +69,7 @@ static forceinline constexpr bool use_simple_mcheck() { return true; }
|
||||
//
|
||||
**************************************************************************/
|
||||
|
||||
MemBuffer::MemBuffer(upx_uint64_t bytes) {
|
||||
MemBuffer::MemBuffer(upx_uint64_t bytes) : MemBufferBase<byte>() {
|
||||
alloc(bytes);
|
||||
debug_set(debug.last_return_address_alloc, upx_return_address());
|
||||
}
|
||||
@@ -198,7 +198,7 @@ void MemBuffer::alloc(upx_uint64_t bytes) {
|
||||
//
|
||||
assert(bytes > 0);
|
||||
debug_set(debug.last_return_address_alloc, upx_return_address());
|
||||
size_t malloc_bytes = mem_size(1, bytes);
|
||||
size_t malloc_bytes = mem_size(1, bytes); // check size
|
||||
if (use_simple_mcheck())
|
||||
malloc_bytes += 32;
|
||||
byte *p = (byte *) ::malloc(malloc_bytes);
|
||||
@@ -216,7 +216,7 @@ void MemBuffer::alloc(upx_uint64_t bytes) {
|
||||
}
|
||||
ptr = (pointer) (void *) p;
|
||||
#if DEBUG
|
||||
memset(ptr, 0xff, size_in_bytes);
|
||||
memset(ptr, 0xfb, size_in_bytes);
|
||||
(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes);
|
||||
#endif
|
||||
stats.global_alloc_counter += 1;
|
||||
|
||||
@@ -46,7 +46,7 @@ protected:
|
||||
size_type size_in_bytes;
|
||||
|
||||
public:
|
||||
MemBufferBase() noexcept : ptr(nullptr), size_in_bytes(0) {}
|
||||
inline MemBufferBase() noexcept : ptr(nullptr), size_in_bytes(0) {}
|
||||
inline ~MemBufferBase() noexcept {}
|
||||
|
||||
// NOTE: implicit conversion to underlying pointer
|
||||
@@ -83,7 +83,7 @@ public: // raw access
|
||||
|
||||
class MemBuffer final : public MemBufferBase<byte> {
|
||||
public:
|
||||
MemBuffer() : MemBufferBase<byte>() {}
|
||||
inline MemBuffer() noexcept : MemBufferBase<byte>() {}
|
||||
explicit MemBuffer(upx_uint64_t bytes);
|
||||
~MemBuffer() noexcept;
|
||||
|
||||
|
||||
+10
-7
@@ -118,7 +118,7 @@ int ptr_diff_bytes(const void *a, const void *b) {
|
||||
if very_unlikely (!mem_size_valid_bytes(d))
|
||||
throwCantPack("ptr_diff_bytes-1; take care");
|
||||
} else {
|
||||
if very_unlikely (!mem_size_valid_bytes(-d))
|
||||
if very_unlikely (!mem_size_valid_bytes(0ll - d))
|
||||
throwCantPack("ptr_diff_bytes-2; take care");
|
||||
}
|
||||
return ACC_ICONV(int, d);
|
||||
@@ -291,10 +291,10 @@ void upx_memswap(void *a, void *b, size_t n) {
|
||||
void upx_stable_sort(void *array, size_t n, size_t element_size,
|
||||
int (*compare)(const void *, const void *)) {
|
||||
for (size_t i = 1; i < n; i++) {
|
||||
char *a = (char *) array + element_size * i; // a = &array[i]
|
||||
if (i != 0 && compare(a - element_size, a) > 0) {
|
||||
upx_memswap(a - element_size, a, element_size); // swap elements a[-1] <=> a[0]
|
||||
i -= 2;
|
||||
char *a = (char *) array + element_size * i; // a := &array[i]
|
||||
if (i != 0 && compare(a - element_size, a) > 0) { // if a[-1] > a[0] then
|
||||
upx_memswap(a - element_size, a, element_size); // swap elements a[-1] <=> a[0]
|
||||
i -= 2; // and decrease i
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -312,8 +312,11 @@ TEST_CASE("upx_stable_sort") {
|
||||
CHECK((a[0] == 0 && a[1] == 1));
|
||||
}
|
||||
{
|
||||
unsigned a[] = {2, 1, 0};
|
||||
upx_stable_sort(a, 3, sizeof(*a), ne32_compare);
|
||||
LE64 a[3];
|
||||
a[0] = 2;
|
||||
a[1] = 1;
|
||||
a[2] = 0;
|
||||
upx_stable_sort(a, 3, sizeof(*a), le64_compare);
|
||||
CHECK((a[0] == 0 && a[1] == 1 && a[2] == 2));
|
||||
}
|
||||
#if __cplusplus >= 202002L // use C++20 std::next_permutation() to test all permutations
|
||||
|
||||
+2
-2
@@ -81,14 +81,14 @@ T *NewArray(upx_uint64_t n) {
|
||||
size_t bytes = mem_size(sizeof(T), n); // assert size
|
||||
T *array = new T[size_t(n)];
|
||||
if (array) {
|
||||
memset(array, 0xff, bytes);
|
||||
memset(array, 0xfb, bytes);
|
||||
(void) VALGRIND_MAKE_MEM_UNDEFINED(array, bytes);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
#define New(type, n) (NewArray<type>(n))
|
||||
#else
|
||||
#define New(type, n) new type[mem_size_get_n(sizeof(type), n)]
|
||||
#define New(type, n) new type[mem_size_get_n(sizeof(type), (n))]
|
||||
#endif
|
||||
|
||||
/*************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user