Ubuntu上 RISC-V64 Jemalloc 编译补丁(修复无法链接问题)

它的问题跟这个是相同的;

RISC-V平台 std::atomic<T> 编译失败问题解决-CSDN博客

区别是自己写的代码,能改掉,但是 Jemalloc 编译好的静态库。

比如:我们是在其它平台上面交叉编译的 RISC-V程序,静态库是从 Ubuntu或 Debian 官方源仓库之中获取的 libjemalloc.dep 包,里面带的静态库就没法编译。

当然你自己编译的也行,一样的问题动态库出不来,要不然你自己去改 jemalloc 代码,要不然你就打补丁,或者换个内存分配池库。

如:nedmalloc、tcmalloc 这类的。

否则就在自己程序里面打补丁把,建议在自己程序之中打补丁,提高程序兼容各种平台的可编译性,能在自己的工程代码内做补丁是最容易控制的。

补丁:

/* risv: undefined reference to `__atomic_compare_exchange_1'、`__atomic_fetch_add_2`... */
#if defined(JEMALLOC)
#if defined(__riscv) || defined(__riscv__) || defined(__riscv32__) || defined(__riscv64__)
/* patch: jemalloc-risv */
static struct __ATOMIC final {
public:
    typedef std::mutex                              SynchronizedObject;
    typedef std::lock_guard     SynchronizedObjectScope;

public:
    SynchronizedObject                              Lock;
}                                                   __ATOMIC_;

// LLVM-CC:
// GUNL-CC:
// https://doc.dpdk.org/api-18.11/rte__atomic_8h_source.html
#ifdef __cplusplus 
extern "C" { 
#endif
    /* 
     * __atomic_exchange_n(dst, val, __ATOMIC_SEQ_CST);
     * __atomic_exchange_4(dst, val, __ATOMIC_SEQ_CST);
     */
    __attribute__((visibility("default"))) unsigned char __atomic_exchange_1(volatile void* ptr, unsigned char value, int memorder) noexcept {
        __ATOMIC::SynchronizedObjectScope scope(__ATOMIC_.Lock);
        unsigned char* dst = (unsigned char*)ptr;
        unsigned char old = *dst;
        *dst = value;
        return old;
    }

    /*
        volatile void *ptr: Pointer to the variable to be operated on.
        void *expected: Pointer to the value to be compared.
        void *desired: expected new value.
        bool weak: Indicates whether to use weak memory order (true indicates weak memory order, false indicates strong memory order).
        int success_memorder: Memory order upon success.
        int failure_memorder: Memory sequence of failure.
    */
    __attribute__((visibility("default"))) bool __atomic_compare_exchange_1(volatile void* ptr, void* expected, unsigned char desired, bool weak, int success_memorder, int failure_memorder) noexcept {
        __ATOMIC::SynchronizedObjectScope scope(__ATOMIC_.Lock);
        unsigned char* dst = (unsigned char*)ptr;
        unsigned char* exchange = (unsigned char*)expected;
        
        unsigned char old = *dst;
        if (old != *exchange) {
            return false;
        }

        *dst = desired;
        *exchange = old;
        return true;
    }
#ifdef __cplusplus 
}
#endif
#endif
#endif

你可能感兴趣的:(C/C++,Extension,risc-v)