From e1499365513d332c245f8d1e54e5600d92b0e869 Mon Sep 17 00:00:00 2001 From: Gilles Vollant Date: Fri, 22 May 2020 12:22:24 +0200 Subject: [PATCH 1/2] modification for compatibility for smartversion compile (including android), some compatibility patch are from previous pull request --- lzhamdecomp/lzham_core.h | 41 +++++++++++++++- lzhamdecomp/lzham_mem.cpp | 95 +++++++++++++++++++++++++++++++++----- lzhamdecomp/lzham_traits.h | 2 +- lzhamdecomp/lzham_types.h | 7 +++ 4 files changed, 130 insertions(+), 15 deletions(-) diff --git a/lzhamdecomp/lzham_core.h b/lzhamdecomp/lzham_core.h index 2e55362..c49d69c 100644 --- a/lzhamdecomp/lzham_core.h +++ b/lzhamdecomp/lzham_core.h @@ -86,7 +86,7 @@ #define LZHAM_RESTRICT __restrict #define LZHAM_FORCE_INLINE __forceinline - #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) + #if (defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)) && (!(defined(PREVENT_LZHAM_USE_INTRINSIC))) #define LZHAM_USE_MSVC_INTRINSICS 1 #endif @@ -165,7 +165,7 @@ #error TODO: Unknown Apple target #endif -#elif defined(__linux__) && (defined(__i386__) || defined(__x86_64__)) && !defined(LZHAM_ANSI_CPLUSPLUS) +#elif defined(__linux__) && !defined(ANDROID) && (defined(__i386__) || defined(__x86_64__)) && !defined(LZHAM_ANSI_CPLUSPLUS) // --- Generic GCC/clang path for x86/x64, clang or GCC, Linux, OSX, FreeBSD or NetBSD, pthreads for threading, GCC built-ins for atomic ops. #define LZHAM_PLATFORM_PC 1 @@ -199,9 +199,46 @@ #define LZHAM_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__)) #endif + #define LZHAM_NOTE_UNUSED(x) (void)x +#elif defined(ANDROID) && !defined(LZHAM_ANSI_CPLUSPLUS) + // Generic GCC path for Android, GCC built-ins for atomic ops. Basically identical to iOS path. + // Pthreads disabled because spin lock is missing..? + #define LZHAM_PLATFORM_PC 0 + + #if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) + #define LZHAM_PLATFORM_PC_X64 0 + #define LZHAM_64BIT_POINTERS 1 + #define LZHAM_CPU_HAS_64BIT_REGISTERS 1 + #else + #define LZHAM_PLATFORM_PC_X86 0 + #define LZHAM_64BIT_POINTERS 0 + #define LZHAM_CPU_HAS_64BIT_REGISTERS 0 + #endif + + #define LZHAM_USE_UNALIGNED_INT_LOADS 0 + + #if __BIG_ENDIAN__ + #define LZHAM_BIG_ENDIAN_CPU 1 + #else + #define LZHAM_LITTLE_ENDIAN_CPU 1 + #endif + + #define LZHAM_USE_PTHREADS_API 0 + #define LZHAM_USE_GCC_ATOMIC_BUILTINS 1 + + #define LZHAM_RESTRICT + + #if defined(__clang__) + #define LZHAM_FORCE_INLINE inline + #else + #define LZHAM_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__)) + #endif + #define LZHAM_NOTE_UNUSED(x) (void)x #else +#ifndef _MSC_VER #warning Building as vanilla ANSI-C/C++, multi-threaded compression is disabled! Please configure lzhamdecomp/lzham_core.h. +#endif // --- Vanilla ANSI-C/C++ // No threading support, unaligned loads are NOT okay, no atomic ops. diff --git a/lzhamdecomp/lzham_mem.cpp b/lzhamdecomp/lzham_mem.cpp index e8163fa..6873cec 100644 --- a/lzhamdecomp/lzham_mem.cpp +++ b/lzhamdecomp/lzham_mem.cpp @@ -17,12 +17,83 @@ using namespace lzham; #define LZHAM_MEM_STATS 0 -#ifndef LZHAM_USE_WIN32_API - #ifndef __APPLE__ - #define _msize malloc_usable_size - #else - #define _msize malloc_size - #endif +#if !defined( ANDROID ) && !defined(_AIX43) && !defined(LZHAM_STANDARD_ALLOCATION) + + #define allocate( size ) malloc( size ) + #define reallocate( p, size ) realloc( p, size ) + #define deallocate( p ) free( p ) + #define getAllocationSize( p ) _msize( p ) + + #ifndef LZHAM_USE_WIN32_API + #if !defined(__APPLE__) && !defined(ANDROID) + #define getAllocationSize( p ) malloc_usable_size( p ) + #else + #define getAllocationSize( p ) malloc_size( p ) + #endif + #else + #define getAllocationSize( p ) _msize( p ) + #endif + +#else + +// Android does not have an API any more for discovering true allocation size, so we need to patch in that data ourselves. +static void* allocate( size_t size ) +{ + uint8* q = static_cast(malloc(LZHAM_MIN_ALLOC_ALIGNMENT + size)); + if (!q) + return NULL; + + uint8* p = q + LZHAM_MIN_ALLOC_ALIGNMENT; + reinterpret_cast(p)[-1] = size; + reinterpret_cast(p)[-2] = ~size; + + return p; +} + +static void deallocate( void* p ) +{ + if( p != NULL ) + { + const size_t num = reinterpret_cast(p)[-1]; + const size_t num_check = reinterpret_cast(p)[-2]; + LZHAM_ASSERT(num && (num == ~num_check)); + if (num == ~num_check) + { + free(reinterpret_cast(p) - LZHAM_MIN_ALLOC_ALIGNMENT); + } + } +} + +static size_t getAllocationSize( void* p ) +{ + const size_t num = reinterpret_cast(p)[-1]; + const size_t num_check = reinterpret_cast(p)[-2]; + LZHAM_ASSERT(num && (num == ~num_check)); + if (num == ~num_check) + return num; + + return 0; +} + +static void* reallocate( void* p, size_t size ) +{ + if( size == 0 ) + { + deallocate( p ); + return NULL; + } + + uint8* q = static_cast(realloc( p, LZHAM_MIN_ALLOC_ALIGNMENT + size )); + if (!q) + return NULL; + + uint8* newp = q + LZHAM_MIN_ALLOC_ALIGNMENT; + reinterpret_cast(newp)[-1] = size; + reinterpret_cast(newp)[-2] = ~size; + + return newp; +} + #endif namespace lzham @@ -86,15 +157,15 @@ namespace lzham if (!p) { - p_new = malloc(size); + p_new = allocate(size); LZHAM_ASSERT( (reinterpret_cast(p_new) & (LZHAM_MIN_ALLOC_ALIGNMENT - 1)) == 0 ); if (pActual_size) - *pActual_size = p_new ? _msize(p_new) : 0; + *pActual_size = p_new ? getAllocationSize(p_new) : 0; } else if (!size) { - free(p); + deallocate(p); p_new = NULL; if (pActual_size) @@ -117,7 +188,7 @@ namespace lzham } else if (movable) { - p_new = realloc(p, size); + p_new = reallocate(p, size); if (p_new) { @@ -127,7 +198,7 @@ namespace lzham } if (pActual_size) - *pActual_size = _msize(p_final_block); + *pActual_size = getAllocationSize(p_final_block); } return p_new; @@ -136,7 +207,7 @@ namespace lzham static size_t lzham_default_msize(void* p, void* pUser_data) { LZHAM_NOTE_UNUSED(pUser_data); - return p ? _msize(p) : 0; + return p ? getAllocationSize(p) : 0; } static lzham_realloc_func g_pRealloc = lzham_default_realloc; diff --git a/lzhamdecomp/lzham_traits.h b/lzhamdecomp/lzham_traits.h index ea7214f..c2012f4 100644 --- a/lzhamdecomp/lzham_traits.h +++ b/lzhamdecomp/lzham_traits.h @@ -67,7 +67,7 @@ namespace lzham // Defines type Q as bitwise copyable. #define LZHAM_DEFINE_BITWISE_COPYABLE(Q) template<> struct bitwise_copyable { enum { cFlag = true }; }; -#if defined(__APPLE__) || defined(__NetBSD__) +#if (defined(__APPLE__) || defined(__NetBSD__)) && (!defined(LZHAM_NO_STD_IS_POD)) #define LZHAM_IS_POD(T) std::__is_pod::__value #else #define LZHAM_IS_POD(T) __is_pod(T) diff --git a/lzhamdecomp/lzham_types.h b/lzhamdecomp/lzham_types.h index 2ea6a10..acd3248 100644 --- a/lzhamdecomp/lzham_types.h +++ b/lzhamdecomp/lzham_types.h @@ -2,6 +2,10 @@ // LZHAM is in the Public Domain. Please see the Public Domain declaration at the end of include/lzham.h #pragma once +#if (defined(_AIX43)) +#include +#endif + namespace lzham { typedef unsigned char uint8; @@ -16,6 +20,9 @@ namespace lzham #ifdef _MSC_VER typedef unsigned __int64 uint64; typedef signed __int64 int64; + #elif (defined(_AIX43)) + typedef uint64_t uint64; + typedef int64_t int64; #else typedef unsigned long long uint64; typedef long long int64; From 2fc671e697f412e0ea3eb3b227a98eee3e669321 Mon Sep 17 00:00:00 2001 From: Gilles Vollant Date: Sun, 31 Jan 2021 22:45:35 +0100 Subject: [PATCH 2/2] modification for compatibility with compilation for macos arm64 --- lzhamdecomp/lzham_platform.cpp | 4 ++-- lzhamdecomp/lzham_platform.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lzhamdecomp/lzham_platform.cpp b/lzhamdecomp/lzham_platform.cpp index cfc85c1..49bd38a 100644 --- a/lzhamdecomp/lzham_platform.cpp +++ b/lzhamdecomp/lzham_platform.cpp @@ -61,8 +61,8 @@ void lzham_debug_break(void) { #if LZHAM_USE_WIN32_API DebugBreak(); -#elif (TARGET_OS_MAC == 1) && (TARGET_IPHONE_SIMULATOR == 0) && (TARGET_OS_IPHONE == 0) - __asm {int 3} +#elif (TARGET_OS_MAC == 1) && (TARGET_IPHONE_SIMULATOR == 0) && (TARGET_OS_IPHONE == 0) && !(defined(_M_ARM64) || defined(__aarch64__)) + __asm {int 3} #else assert(0); #endif diff --git a/lzhamdecomp/lzham_platform.h b/lzhamdecomp/lzham_platform.h index 01704be..e487c80 100644 --- a/lzhamdecomp/lzham_platform.h +++ b/lzhamdecomp/lzham_platform.h @@ -21,7 +21,7 @@ void lzham_fail(const char* pExp, const char* pFile, unsigned line); #define LZHAM_BUILTIN_EXPECT(c, v) c #endif -#if defined(__GNUC__) && LZHAM_PLATFORM_PC +#if defined(__GNUC__) && LZHAM_PLATFORM_PC && !(defined(_M_ARM64) || defined(__aarch64__)) extern __inline__ __attribute__((__always_inline__,__gnu_inline__)) void lzham_yield_processor() { __asm__ __volatile__("pause");