caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] String, Array, Bigarray.char
Date: Sat, 11 May 2013 01:42:19 +0200	[thread overview]
Message-ID: <20130510234219.GB29570@frosties> (raw)
In-Reply-To: <CABooLwO=eYqxFQNJHUKFV_u-11R9b0U=BjH0OaGf--Uai457zQ@mail.gmail.com>

On Thu, May 09, 2013 at 02:32:20PM +0100, Tom Ridge wrote:
> Dear caml-list,
> 
> Quick question: I am working a lot with arrays of byte, which at
> various points I want to view as strings, and at various points I want
> to view as arrays. The exact types involved should be discernible from
> the code below.
> 
> I have some conversion functions e.g.:
> 
>   type myfusebuffer = (char, Bigarray.int8_unsigned_elt,
> Bigarray.c_layout) Bigarray.Array1.t
> 
>   module A = Bigarray.Array1
> 
>   (* convenience only; don't use in production code *)
>   let array_of_string bs = (
>     let arr = (Array.init (String.length bs) (String.get bs)) in
>     let contents = A.of_array Bigarray.char Bigarray.c_layout arr in
>     contents)
>   let (_:string -> myfusebuffer) = array_of_string
> 
> This presumably takes O(n) time (where n is the length of the string
> bs). My question is: is there functionality to move values between
> these types at cost O(1)? Basically, I'm hoping that String is
> implemented as A.of_array Bigarray.char Bigarray.c_layout or
> similar...
> 
> Thanks

A Bigarray is a ocaml block with the dimension, size, proxy object
(for sub arrays) and pointer to the data.

A string on the other hand is a ocaml block with bytes directly in it.

Now if you allocate the memory for a Bigarray you can add a bit extra
in front so you can also access the array as string. But you run into
problems with the GC. Because it might want to free the Bigarrays
while something still holds a reference to the string. So realy not a
good idea.



Since you seem to want to use fuse and bigarrays to implement a
filesystem you might want to take a look at:

- ExtUnix:
type buffer = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

- ExtUnix.BA:
val pread : Unix.file_descr -> int -> ExtUnixSpecific.buffer -> int
val pwrite : Unix.file_descr -> int -> ExtUnixSpecific.buffer -> int
val read : Unix.file_descr -> ExtUnixSpecific.buffer -> int
val write : Unix.file_descr -> ExtUnixSpecific.buffer -> int
val get_substr : ExtUnixSpecific.buffer -> int -> int -> string
val set_substr : ExtUnixSpecific.buffer -> int -> string -> unit

- ExtUnix.BA.BigEndian
- ExtUnix.BA.LittleEndian
- ExtUnix.BA.HostEndian


- libaio-ocaml: http://git.ocamlcore.org/cgi-bin/gitweb.cgi?p=libaio-ocaml/libaio-ocaml.git;a=summary
	pkg-ocaml-maint/packages/libaio-ocaml.git on git.debian.org
	Bindings for Linux async IO library (libaio)

- libfuse-ocaml: http://git.ocamlcore.org/cgi-bin/gitweb.cgi?p=libfuse-ocaml/libfuse-ocaml.git;a=summary
	pkg-ocaml-maint/packages/libfuse-ocaml.git on git.debian.org
	Bindings for libfuse

The fuse bindings are for the lowlevel interface and geared towards
async IO. I already convert filenames to strings. Or the stat
structures to Bigarray on replies and so on. All of those are
comparatively small so copying seems OK. The read callback always uses
Bigarray and the write callback reply with string, string array,
Bigarray or Bigarray array.


I've managed to merge the Bigarray and endian stuff from libaio-ocaml
and libfuse-ocaml in extunix but I haven't yet updated libaio-ocaml
and libfuse-ocaml to make use of that. So if you plan to use either of
those for real let me know and I will find some time to update them. I
plan to do that soon and get them added to Debian proper now that
wheezy is released anyway.

Also if you want to add bindings for the high level fuse interface,
implement a high level interface in ocaml directly or add a
multithreaded main loop I wouldn't mind patches to that affect.

MfG
	Goswin

      parent reply	other threads:[~2013-05-10 23:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-09 13:32 Tom Ridge
2013-05-09 13:44 ` Anil Madhavapeddy
2013-05-09 14:07   ` Tom Ridge
2013-05-09 14:14     ` Tom Ridge
2013-05-09 14:21       ` Anil Madhavapeddy
2013-05-09 14:30         ` Tom Ridge
2013-05-09 16:29         ` ygrek
2013-05-09 14:29       ` Markus Mottl
2013-05-09 14:25 ` Markus Mottl
2013-05-10 23:42 ` Goswin von Brederlow [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130510234219.GB29570@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).