Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread David Roundy
See the Data.ByteString.Internal docs:

http://www.haskell.org/ghc/docs/latest/html/libraries/bytestring/Data-ByteString-Internal.html#v%3AtoForeignPtr

Of course, you'd better not write to the contents of that pointer, or
bad things could happen...

David

On Mon, Jun 23, 2008 at 08:18:23PM -0500, Galchin, Vasili wrote:
> e.g. on Word8 .
> 
>  let aiocb = AIOCB{
>aioFd=fd,
>aioLioOpcode=0,
>aioReqPrio=0,
>aioOffset=0,
>aioBuf=??,<   Ptr Word8
>aioBytes=128,
>aioSigevent=event}
> 
> ???
> 
> Kind regards, Vasili
> 
> On Mon, Jun 23, 2008 at 8:13 PM, Galchin, Vasili <[EMAIL PROTECTED]>
> wrote:
> 
> > ok .
> >
> > 1) how do I marshall from ByteString to char * (poke)??
> >
> > 2) how do I write
> >
> >  let x =??? :: Word8
> >
> > 3) how do I write
> >
> >  let y = ??? ::ByteString
> >
> > Kind regards, Vasili
> >
> >
> >
> > On Mon, Jun 23, 2008 at 6:13 PM, Adam Langley <[EMAIL PROTECTED]>
> > wrote:
> >
> >> On Mon, Jun 23, 2008 at 2:27 PM, Don Stewart <[EMAIL PROTECTED]> wrote:
> >> > So heap allocated and collected, but not moved.
> >>
> >> My bad. In that case, you might want to work with ByteStrings all the
> >> way since it might make building the visible interface (which probably
> >> should use ByteStrings) easier.
> >>
> >>
> >> AGL
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Galchin, Vasili
e.g. on Word8 .

 let aiocb = AIOCB{
   aioFd=fd,
   aioLioOpcode=0,
   aioReqPrio=0,
   aioOffset=0,
   aioBuf=??,<   Ptr Word8
   aioBytes=128,
   aioSigevent=event}

???

Kind regards, Vasili

On Mon, Jun 23, 2008 at 8:13 PM, Galchin, Vasili <[EMAIL PROTECTED]>
wrote:

> ok .
>
> 1) how do I marshall from ByteString to char * (poke)??
>
> 2) how do I write
>
>  let x =??? :: Word8
>
> 3) how do I write
>
>  let y = ??? ::ByteString
>
> Kind regards, Vasili
>
>
>
> On Mon, Jun 23, 2008 at 6:13 PM, Adam Langley <[EMAIL PROTECTED]>
> wrote:
>
>> On Mon, Jun 23, 2008 at 2:27 PM, Don Stewart <[EMAIL PROTECTED]> wrote:
>> > So heap allocated and collected, but not moved.
>>
>> My bad. In that case, you might want to work with ByteStrings all the
>> way since it might make building the visible interface (which probably
>> should use ByteStrings) easier.
>>
>>
>> AGL
>>
>> --
>> Adam Langley [EMAIL PROTECTED] http://www.imperialviolet.org
>>
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Galchin, Vasili
ok .

1) how do I marshall from ByteString to char * (poke)??

2) how do I write

 let x =??? :: Word8

3) how do I write

 let y = ??? ::ByteString

Kind regards, Vasili


On Mon, Jun 23, 2008 at 6:13 PM, Adam Langley <[EMAIL PROTECTED]>
wrote:

> On Mon, Jun 23, 2008 at 2:27 PM, Don Stewart <[EMAIL PROTECTED]> wrote:
> > So heap allocated and collected, but not moved.
>
> My bad. In that case, you might want to work with ByteStrings all the
> way since it might make building the visible interface (which probably
> should use ByteStrings) easier.
>
>
> AGL
>
> --
> Adam Langley [EMAIL PROTECTED] http://www.imperialviolet.org
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Adam Langley
On Mon, Jun 23, 2008 at 2:27 PM, Don Stewart <[EMAIL PROTECTED]> wrote:
> So heap allocated and collected, but not moved.

My bad. In that case, you might want to work with ByteStrings all the
way since it might make building the visible interface (which probably
should use ByteStrings) easier.


AGL

-- 
Adam Langley [EMAIL PROTECTED] http://www.imperialviolet.org
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Don Stewart
agl:
> 2008/6/23 Galchin, Vasili <[EMAIL PROTECTED]>:
> > Basically I want to model POSIX async io "aiocb"(
> > http://uw714doc.sco.com/en/man/html.5/aiocb.5.html) in Haskell .. in
> > particular the aiocb field "aio_buf"!
> 
> That's a mutable buffer, so this has to be pretty low level in your
> wrapping of the AIO interface. I would keep it as a Ptr Word8 at that
> point. ByteStrings are probably the correct type for the user visible
> interface, but I would guess not here.
> 
> Plus, ByteString data lives in the Haskell heap so could get moved
> about by the collector. Since an AIO interface is probably going to

ByteStrings are allocated into pinned memory, so they're easy to pass to
C and back. It uses the fast 'mallocPlainForeignPtr' api,

-- GHC notes: 'mallocPlainForeignPtr' has a heavily optimised
-- implementation in GHC.  It uses pinned memory in the garbage
-- collected heap, as for mallocForeignPtr. 

So heap allocated and collected, but not moved.

> return while the IO is still in progress, you don't want to stop the
> collector, but nor do you want the data moving because the kernel's
> pointer isn't going to move with it.

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Adam Langley
On Mon, Jun 23, 2008 at 1:15 PM, Galchin, Vasili <[EMAIL PROTECTED]> wrote:
>  I tried to write a test value of Word8 without success. Can you give an 
> example?

I'm not quite sure what you would want in such an example. See [1] for
the malloc function. Then you take the result and wrap it with [2]:
 fp <- mallocBytes nbytes >>= newForeignPtr finalizerFree

When you want to use it, use [3] to unwrap fp and get a raw Ptr Word8.
You can pass these to foreign functions any place they expect a
uint8_t *, char * etc.


[1] 
http://www.haskell.org/hoogle/hoodoc.cgi?module=Foreign.Marshal.Alloc&name=mallocBytes&mode=func
[2] 
http://www.haskell.org/hoogle/hoodoc.cgi?module=Foreign.ForeignPtr&name=newForeignPtr&mode=func
[3] 
http://www.haskell.org/hoogle/hoodoc.cgi?module=Foreign.ForeignPtr&name=withForeignPtr&mode=func

-- 
Adam Langley [EMAIL PROTECTED] http://www.imperialviolet.org
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Galchin, Vasili
On Mon, Jun 23, 2008 at 12:07 PM, Adam Langley <[EMAIL PROTECTED]>
wrote:

> 2008/6/23 Galchin, Vasili <[EMAIL PROTECTED]>:
> > Basically I want to model POSIX async io "aiocb"(
> > http://uw714doc.sco.com/en/man/html.5/aiocb.5.html) in Haskell .. in
> > particular the aiocb field "aio_buf"!
>
> That's a mutable buffer, so this has to be pretty low level in your
> wrapping of the AIO interface. I would keep it as a Ptr Word8 at that
> point. ByteStrings are probably the correct type for the user visible
> interface, but I would guess not here.

 I tried to write a test value of Word8 without success. Can you give an
example?

>
>
> Plus, ByteString data lives in the Haskell heap so could get moved
> about by the collector. Since an AIO interface is probably going to
> return while the IO is still in progress, you don't want to stop the
> collector, but nor do you want the data moving because the kernel's
> pointer isn't going to move with it.

   ^^ Interesting about moving around in the heap. I cannot use it for
AIO.

>
>
> AGL
>
> --
> Adam Langley [EMAIL PROTECTED] http://www.imperialviolet.org
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Adam Langley
2008/6/23 Galchin, Vasili <[EMAIL PROTECTED]>:
> Basically I want to model POSIX async io "aiocb"(
> http://uw714doc.sco.com/en/man/html.5/aiocb.5.html) in Haskell .. in
> particular the aiocb field "aio_buf"!

That's a mutable buffer, so this has to be pretty low level in your
wrapping of the AIO interface. I would keep it as a Ptr Word8 at that
point. ByteStrings are probably the correct type for the user visible
interface, but I would guess not here.

Plus, ByteString data lives in the Haskell heap so could get moved
about by the collector. Since an AIO interface is probably going to
return while the IO is still in progress, you don't want to stop the
collector, but nor do you want the data moving because the kernel's
pointer isn't going to move with it.

AGL

-- 
Adam Langley [EMAIL PROTECTED] http://www.imperialviolet.org
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: "C" buffer suggestions??

2008-06-23 Thread Galchin, Vasili
Basically I want to model POSIX async io "aiocb"(
http://uw714doc.sco.com/en/man/html.5/aiocb.5.html) in Haskell .. in
particular the aiocb field "aio_buf"!

Kind regards, Vasili

On Sun, Jun 22, 2008 at 1:47 AM, Galchin, Vasili <[EMAIL PROTECTED]>
wrote:

> Hello,
>
> I want to model an I/O read/write buffer in Haskell and marshall down
> into ANSI C.
>
> 1) ByteString? Or Ptr Word8?
>
> 2) What are example values of the types in 1)?
>
>
> Kind regards, Vasili
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe