Help with FFI - another try
nicolas cellier
ncellier at ifrance.com
Mon Jul 24 12:02:23 UTC 2006
I retry sending because it failed...
---------- Message transmis ----------
Subject: Re: Help with FFI - another try
Date: Vendredi 21 Juillet 2006 23:13
From: nicolas cellier <ncellier at ifrance.com>
To: Squeak-dev developers list general-purpose Squeak
<squeak-dev at lists.squeakfoundation.org>
Le Vendredi 21 Juillet 2006 22:49, nicolas cellier a écrit :
> Le Vendredi 21 Juillet 2006 13:15, Eugene Beschastnov a écrit :
> > I digged a little into C-code and found that hbeStream is pointer to
> > some LAME data (some global flags are stored there). I suppose that
> > root of problem is that this data is destroyed or moved between
> > apicalls. Is there any way to avoid this?
>
> One solution is to allocate this data on external heap.
> (See ExternalAddress class>>allocate:) instead of using a ByteArray in
> Smalltalk memory.
>
> Of course, you'll have to free data yourself as in C.
> Maybe the trick explained at http://bugs.impara.de/view.php?id=3692 that
> should be in latest image can help...
>
> Nicolas
My last post was too short: i remember it is not that obvious to guess the
right object which should be passed as FFI call argument.
It is an ExternalData, a combination of an ExternalAddress and an
ExternalType.
And one more thing: don't expect ExternalAddress to survive across image
snapshots. If you want to survive, you must transfer data into a ByteArray
before saving the image, and transfer to a newly allocated ExternalAddress
before next use... But maybe this is not a problem.
in Smallapack-Collection, i have a class ExternalArray doing this kind of
tirck:
ExternalArray>>resetExternalData
"Reset the ExternalData object used as FFI argument.
This is cached in an inst-var instead of being recreated at each call.
However, this must be reset whenever handle is changed"
| cType |
cType := self class type.
data := cType isStructureType
ifTrue: [
"Arrays of structure cannot carry type checking in FFI"
ExternalData fromHandle: handle type: ExternalType void]
ifFalse: [
"Atomic types"
ExternalData fromHandle: handle type: cType]
where handle is sometimes a ByteArray in Smalltalk memory, sometimes an
ExternalAddress according to my needs.
ExternalArray>>copyInCSpace
"Answer a copy of self with data in C space"
^self shallowCopy postCopyInCSpace
ExternalArray>>postCopyInCSpace
"Copy the data into a new array"
| newHandle bs |
handle isNil
ifFalse: [bs := self byteSize.
newHandle := ExternalAddress gcallocate: bs.
self
memcpyDest: newHandle
fromSource: handle
nBytes: bs.
self setHandle: newHandle]
Maybe you have enough elements or you can browse more at squeak source
http://www.squeaksource.com/@ZGaVjYGiHOshVjSM/LGmmIzpi
Nicolas
-------------------------------------------------------
More information about the Squeak-dev
mailing list
|