如何实现“malloc"在瓦斯姆 [英] How to implement "malloc" in Wasm

查看:51
本文介绍了如何实现“malloc"在瓦斯姆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习 WebAssembly,我发现 Wasm 世界无法轻松访问外部函数(libc 或第三方函数)(使用 import 是通用解决方案).

I am learning WebAssembly now and I find that the external function (libc or third party function) can't be accessed by Wasm world easily (using import is a general solution).

我正在尝试使用 emcc 将我的源代码编译成 wasm,但是 mallocfree 在我的源代码中被广泛使用.我认为从现实世界中导入 mallocfree 是不可行的.

I'm trying to use emcc to compile my source code into wasm but malloc and free are widely used in my source code. I don't think that importing malloc and free from real world is practicable.

所以,我很好奇如何在 wasm 世界中实现 malloc.我已经知道 malloc 在 glibc 中是如何工作的:使用 brk() 或 sbrk() 来扩展堆和线性地址的一些内存管理.但是在 wasm 世界中,我认为不可能调用 brk()sbrk() 来获取班轮地址.

So, I'm pretty curious about how to implement malloc in wasm world. I have already known how the malloc works in glibc:using brk() or sbrk() to extend heap and some memory management for the liner address. But in wasm world I think it impossible to call brk() or sbrk() to get the liner address.

像这样使用全局变量实现malloc合理吗?

Is it reasonable to use global var to implement malloc like this ?

u_char mem[10240];


void *wasm_malloc(size_t num)
{
    /*get the free mem idx*/
    return &mem[idx];
}

推荐答案

等等,你不需要那样做.

Wait, you don't need to do that.

我认为从现实世界中导入 malloc 和 free 是不可行的.

I don't think that importing malloc and free from real world is practicable.

不正确.这正是使用 Emscripten 的重点.Emscripten 不仅仅是一个从 C/C++ 到 Wasm 的编译器,而是一个完整的工具链,其中包括一个 Web 运行时和它自己的 libc,专门设计用于以最少的源代码修改为 Web 浏览器运行 C/C++ 程序.

Incorrect. That's exactly the point of using Emscripten. Emscripten is not just a C/C++ to Wasm compiler, but a complete toolchain that includes a web runtime and its own libc specifically designed for running C/C++ program for web browsers with minimal source code modifications.

Emscripten libc 是 musl 的一个经过大量修改的分支.它实现/模拟了广泛的标准 C 库(包括 mallocsbrk)和 POSIX API(如 pthread 和 BSD 套接字),除了一些没有意义的 API在像 execfork 这样的 Wasm 环境中.通过使用 emcc 命令,您将开箱即用地链接这些 libc 端口.所以随意使用 malloc - 你不需要做任何事情!

Emscripten libc is a heavily modified fork of musl. It implements/emulates wide range of standard C libraries (including malloc, sbrk) and POSIX APIs (like pthread and BSD socket), except some APIs that doesn't make sense in a Wasm environment like exec and fork. By using emcc command, you will link those libc ports out of the box. So feel free just using malloc - you don't need to do anything!

如果您仍然想知道如何为 Emscripten 实现 malloc,Emscripten 有 malloc 实现的两个选项 - dlmalloc 和 emmalloc.

If you are still wondering how to implement malloc for Emscripten, Emscripten has two options of malloc implementations - dlmalloc and emmalloc.

dlmalloc 是一个著名的 malloc 实现.glibc 也使用它的分叉版本.你可以在 这里看到 Emscripten 的 dlmalloc 版本.

dlmalloc is a famous malloc implementation. glibc also uses a forked version of it. You can see Emscripten's version of dlmalloc here.

emmalloc 对你来说可能更有趣.它是由 Emscripten 团队设计的用于 Web 环境的简单紧凑的 malloc 实现.您可以在此处查看源代码.

emmalloc might be more interesting to you. It's a simple and compact malloc implementation for web enviroment, designed by the Emscripten team. You can see the source code here.

这篇关于如何实现“malloc"在瓦斯姆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆