Skip to content
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

Returning a pointer to a file in MEMFS to javascript #6858

Closed
athei opened this issue Jul 14, 2018 · 6 comments
Closed

Returning a pointer to a file in MEMFS to javascript #6858

athei opened this issue Jul 14, 2018 · 6 comments

Comments

@athei
Copy link

athei commented Jul 14, 2018

I am trying to return a C string back to javscript that was read from MEMFS. My steps are:

C++
Read from MEMFS to char*
Return char*
Javascript
Call _free() on the pointer

Is there a way to directly return a pointer to the MEMFS memory location? Did not found it im emscripten.h. This way I could avoid the alloc/copy/free cycle.

@kripken
Copy link
Member

kripken commented Jul 16, 2018

How MEMFS stores in memory may depend on various things, see the --no-heap-copy argument to the file packager. But if you keep the default behavior, then mmap should just return a pointer to the memory location, which is efficient and I think close to what you want?

@athei
Copy link
Author

athei commented Jul 17, 2018

Yes this is what I was looking for. Thanks! Is there a way to call munmap() from javascript like calling _free()?

@kripken
Copy link
Member

kripken commented Jul 18, 2018

You can add _munmap to EXPORTED_FUNCTIONS (free() is there by default, others need to be added).

@athei
Copy link
Author

athei commented Jul 23, 2018

Thanks for your reply! I have problems making munmap available in javascript. Mind that I only call it from places invisible to emscripten.

I added the following. Do not mind the escapes. It is copied from a cmake file:

"-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE='[\"memcpy\", \"memset\", \"malloc\", \"free\", \"munmap\"]'"
"-s EXTRA_EXPORTED_RUNTIME_METHODS='[\"_free\", \"_munmap\", \"allocateUTF8\", \"UTF8ToString\"]'"

The build yields this error and _munmap is not available:
warning: unresolved symbol: munmap

This is my js code invisible to emscripten. Every line works except for the _munmap:

const ptr_in = this.simc.allocateUTF8(this.state.profile);
const ptr_out = this.simc._simulate(ptr_in);
this.simc._free(ptr_in);
const result = this.simc.UTF8ToString(ptr_out);
this.simc.munmap(ptr_out);

@kripken
Copy link
Member

kripken commented Aug 9, 2018

This is a little tricky, because munmap is implemented in a combination of C (in libc) and JS (in FS, through the syscall ABI). Worse, on the C side a macro is used, pointing to __munmap. The simplest way to get it is to use munmap in a function in your C code, which acts as a wrapper for it. Then export that to JS and call it.

@athei
Copy link
Author

athei commented Aug 10, 2018

Ahh okay thanks for the tip. That actually worked.

@athei athei closed this as completed Aug 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants