[ocaml-ctypes] Bigarray typing problem

Jeremy Yallop yallop at gmail.com
Tue Dec 17 22:17:03 GMT 2013


On 15 December 2013 23:51, Daniel Bünzli <daniel.buenzli at erratique.ch> wrote:
> However I now have another problem which is the other way round. I would like to be able to see a chunk of (void *) memory as a Bigarray.Array1.t of a given, client-defined, kind (failing if there are size alignment issue).
>
> Here's an example:
>
>   val get_pixels : t -> ('a, 'b) kind -> ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t
>
> For that I need to use Ctypes.bigarray_of_ptr, however to remain generic I need to coerce my (void *) pointer to the right type according to the bigarray kind. For that I wrote this function, just in order to match the access types (is that ok or should I match the storage aswell ?):

Yes, you need to consider the storage type as well.  The following
should be about right:

  let access_ptr_typ_of_ba_kind : ('a, 'b) Bigarray.kind -> 'a ptr typ
= fun k ->
    let open Bigarray in
    match Obj.magic k with
    | k when k = float32 -> Obj.magic (ptr Ctypes.float)
    | k when k = float64 -> Obj.magic (ptr Ctypes.double)
    | k when k = complex32 -> Obj.magic (ptr Ctypes.complex32)
    | k when k = complex64 -> Obj.magic (ptr Ctypes.complex64)
    | k when k = int8_signed -> Obj.magic (ptr Ctypes.int8_t)
    | k when k = int8_unsigned -> Obj.magic (ptr Ctypes.uint8_t)
    | k when k = int16_signed -> Obj.magic (ptr Ctypes.int16_t)
    | k when k = int16_unsigned -> Obj.magic (ptr Ctypes.uint16_t)
    | k when k = int -> Obj.magic (ptr Ctypes.camlint)
    | k when k = int32 -> Obj.magic (ptr Ctypes.int32_t)
    | k when k = int64 -> Obj.magic (ptr Ctypes.int64_t)
    | k when k = nativeint -> Obj.magic (ptr Ctypes.nativeint)
    | k when k = char -> Obj.magic (ptr Ctypes.char)
    | _ -> assert false

> Is that correct ?

It looks reasonable to me.

> If it is and there's no other way I think it would be nice to have the above function in ctypes.

Agreed: it's both needed in the interface for completeness, and much
simpler to implement internally:

   https://github.com/ocamllabs/ocaml-ctypes/pull/114/files

It should be in the next release.

Jeremy.


More information about the Ctypes mailing list