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<SynchronizedObject>     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

相关推荐

  1. 构建 aarch64 以及 riscv64 交叉编译工具(裸机)

    2024-01-28 08:34:01       34 阅读
  2. Ubuntu搭建RiscV交叉编译环境

    2024-01-28 08:34:01       241 阅读
  3. 修复 Ubuntu 2204 Wi-Fi 热点无法连接问题

    2024-01-28 08:34:01       43 阅读
  4. Jemalloc编译安装

    2024-01-28 08:34:01       18 阅读
  5. ubuntu-base(arm64riscv64) 根文件系统

    2024-01-28 08:34:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 08:34:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 08:34:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 08:34:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 08:34:01       20 阅读

热门阅读

  1. LeetCode第468题 - 验证IP地址

    2024-01-28 08:34:01       40 阅读
  2. vue 前端给接口传file对象需要注意的问题

    2024-01-28 08:34:01       30 阅读
  3. K8S系列文章之 docker配置远程访问

    2024-01-28 08:34:01       33 阅读
  4. springboot2.2.9整合kafka之KafkaListener实现原理

    2024-01-28 08:34:01       28 阅读
  5. 力扣19-删除链表中倒数第N个节点

    2024-01-28 08:34:01       36 阅读
  6. 【leetcode100-069到073】【栈】五题合集

    2024-01-28 08:34:01       33 阅读
  7. 每日OJ题_算法_二分查找⑧_力扣LCR 173. 点名

    2024-01-28 08:34:01       34 阅读