-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wasm32 implementation for libc #51
Conversation
Thanks for the pr. I'm a little surprised that this is needed. It sounds like a common problem for wasm. Shouldn't these be provided by emscripten or some other crate? I'm worried that if I add these symbols, but someone links a "real" libc separately, they will conflict. |
The target here really is There may be another crate that provides some libc functions, though I couldn't find it! |
Hi! I'm the creator of the original (rejectec) pull-request for rust-libc. The weird thing with this situation is that Wasm has no libc at all, instead it has its own memory allocator, and rust-wasm rightfully uses this as the global allocator. So there are no Well, what happens is that there was some crates out there that can be used both with C native libraries or with pure Rust reimplementations of the same APIs. And some of those APIs used to pass memory blocks allocated with This project, For example:
could be reimplemented without copying as:
|
lodepng has this quirk of using raw malloc, because it exposes a C API, and callers are expected to be able to free data allocated by lodepng. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change it to only use wasm32-unknown-unknown
? So that -emscripten target uses real malloc?
@kornelski thanks for the feedback and @rodrigorc thanks for the detailed explanation! I'll change the PR for this file only to be used in |
Done! Here's the updated commit message for reference: Add wasm32 implementation for libcThis adds a wasm32 implementation for NOTE: This is only enabled for |
src/ffi.rs
Outdated
mod libc; | ||
|
||
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] | ||
use std::path::{PathBuf}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect this to break compilation on Windows, since the paths are later used with a different cfg. Also {Path}
is a redundant syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the {...}
. Can you clarify the issue with Windows? I'm not very experienced with Rust, let alone conditional compilation, thanks for your patience 😅
The |
This adds a wasm32 implementation for `free`, `malloc` and `realloc`. Originally written by @rodrigorc in rust-lang/libc#1092 and adapted. NOTE: This is only enabled for `wasm32-unknown-unknown` ([`target_arch = "wasm32"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch), [`target_os = "unknown"`](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)), because other wasm targets (like `wasm32-unknown-emscripten`) may provide a `libc` implementation.
@kornelski change undone! |
Thank you |
Fixes #43 .
This adds a wasm32 implementation for
free
,malloc
andrealloc
.Originally written by @rodrigorc in
rust-lang/libc#1092 and adapted.
Disclaimer: I don't know rust, and I haven't really looked at the implementation; I blindly copy-pasted. This should probably be reviewed before it goes in.