[squeak-dev] FFI: FFI-Kernel-nice.118.mcz

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Mon Jun 22 23:38:07 UTC 2020


Le lun. 22 juin 2020 à 15:20, Nicolas Cellier <
nicolas.cellier.aka.nice at gmail.com> a écrit :

>
>
> Le lun. 22 juin 2020 à 12:11, Marcel Taeumel <marcel.taeumel at hpi.de> a
> écrit :
>
>> > For the generalization to other atomic types, we indeed need to go
>> through the ExternalType and implement allocate: there too.
>>
>> Not needed. You can access ExternalType >> #byteSize from within
>> ExternalData. It's public.
>>
>> Best,
>> Marcel
>>
>
> Hi Marcel,
> you constantly bring me to a lower level API ...
> I know that byteSize is public, but I don't want to bother with that.
> I want to handle an array of N things, not an ExternalData withHandle:
> (ByteArray new: thing byteSize * N) type: thing.
> I want (thing allocate: N), that's all I need...
> I've used similar constructs provided by VW DLLCC, and believe me, it's
> convenient!
>
> Beside, think of it, an ExternalData has to provide both memory AND type
> right?
>     ExternalData fromHandle: (ByteArray new: type byteSize * anInteger)
> type: type.
> Who knows better about its own size than the type itself?
> Why scatter the responsibilities that could be gathered in the type?
> Where would this snippet go?
>
> The funny thing is that I refactored some dusty parts today, so we can
flesh out this discussion:

Name: HDF5-Interface-nice.10
Time: 22 June 2020, 6:13:15.137449 pm

The old version using byteSize was:

dimensions
     "Answer an array of integers representing the dimensions of this
dataspace.
     Note that a dimension -1 means unlimited."
     | ndims effective hdims maxDims bsize method |
     ndims := self ndims.
     bsize := Hsize_t byteSize.
    method := #(unsignedByteAt: unsignedShortAt: unsignedLongAt:
unsignedLongLongAt:) at: bsize highBit.
    hdims := ByteArray new: ndims * bsize.
    maxDims := ByteArray new: ndims * bsize.
    effective := API H5Sget_simple_extent_dims: handle with: hdims with:
maxDims.
    effective = ndims ifFalse: [self error: 'unexpected effective
dimensionality'].
    ^(0 to: ndims - 1) collect: [:i | hdims perform: method with: i * bsize
+ 1]

I don't remember if ByteArray did work without using ExternalData wrapper
around it, this could only be worse!
The new one is using allocate: which create an ExternalData and deal with
all the low level ByteArray container, type byteSize etc...
It also uses the collection-like protocol on the external data (hdims at:)

dimensions
     "Answer an array of integers representing the dimensions of this
dataspace.
     Note that a dimension -1 means unlimited."
     | ndims effective hdims maxDims  |
     ndims := self ndims.
     hdims := Hsize_t allocate: ndims.
     maxDims := Hsize_t allocate: ndims.
     effective := API H5Sget_simple_extent_dims: handle with: hdims with:
maxDims.
     effective = ndims ifFalse: [self error: 'unexpected effective
dimensionality'].
     ^(1 to: ndims) collect: [:i | (hdims at: i) value]

We can all agree that the later is better.
I have the ExternalData playing its role in the best of the manners:
Tansparently! Exactly like ExternalFunction.
A great economy of verbiage - unlike this thread ;)
So thank you Marcel for enhancing those API, let me just add my grain of
salt!

The only annoying thing is the value which unwraps the Hsize_t
ExternalTypeAlias value.
If I would replace Hsize_t with a simple ExternalType hsize_t, then
1) I would omit the value
2) I would like to write
     hdims := ExternalType hsize_t allocate: ndims.
Because it's conceptually the same, allocate an array (ExternalData) of
given type.
That's why I implement allocate: in ExternalType if ever you are still not
convinced.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200623/9e9b62a6/attachment.html>


More information about the Squeak-dev mailing list