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

Does cstruct provides marshaling to/from a char bigarray (bigstring)? #310

Open
UnixJunkie opened this issue Feb 21, 2023 · 6 comments
Open

Comments

@UnixJunkie
Copy link

It would be nice to have such a functionality.
Since bigarrays are the only thing that can be memory mapped on unices.

@reynir
Copy link
Member

reynir commented Feb 21, 2023

Does Cstruct.of_bigarray or Cstruct.buffer do what you're looking for? (I'm not sure what the difference is)

https://github.com/mirage/ocaml-cstruct/blob/main/lib/cstruct.mli#L178
https://github.com/mirage/ocaml-cstruct/blob/main/lib/cstruct.mli#L629

@UnixJunkie
Copy link
Author

I don't think so.
What I am looking for is like:

val Marshal.to_buffer                                                        
    bytes -> int -> int -> 'a -> Stdlib__Marshal.extern_flags list -> int

but instead of bytes we would have a bigstring.
Also:

val Marshal.from_bytes bytes -> int -> 'a

with also bytes replaced by bigstring.
Also, the number of bytes that were consumed should be returned.

@reynir
Copy link
Member

reynir commented Feb 21, 2023

Sorry, I think I needed another cup of coffee :-)

I found that there are C symbols caml_output_value_to_malloc and caml_input_value_from_malloc. The former could be used to implement marshal_to_cstruct I think, but the latter implicitly calls free() on the buffer :/ I don't find it in cstruct.

@UnixJunkie
Copy link
Author

I know some related code in parmap, if you are interested:
https://github.com/rdicosmo/parmap/blob/master/src/bytearray_stubs.c
ml_marshal_to_bigarray_buffer and ml_unmarshal_from_bigarray are interesting.
I am not sure they have the exact required interface though.
Esp., if you don't return the number of bytes read, it is annoying
for the software layer which is managing the buffer.

@avsm
Copy link
Member

avsm commented Feb 21, 2023

It's worth noting that bytes are non-moving in OCaml 5.0 (and will likely remain so for 1KB+ allocations even if a compactor is added in the future), and so we should probably just resolve the question of whether Cstruct can be backed by bytes in OCaml 5.0+ and eliminate this bigarray split.

@UnixJunkie
Copy link
Author

The problem with bytes is that they cannot be memory mapped, as far as I know.

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

3 participants