[Vm-dev] Objects may move while reading a file

Guillermo Polito guillermopolito at gmail.com
Mon May 13 10:04:09 UTC 2013

I was looking at the following primitiveFileRead because I wanted to
extract some code. I do not see why objects should move during the
primitive execution... And I do not see why is this check only made in this
primitive (and the threadedFFI ones...).

Does someone have a clue?


<export: true>
| retryCount count startIndex array file elementSize bytesRead |
<var: 'file' type: #'SQFile *'>
<var: 'count' type: #'size_t'>
<var: 'startIndex' type: #'size_t'>
<var: 'elementSize' type: #'size_t'>

retryCount := 0.
count := interpreterProxy positive32BitValueOf: (interpreterProxy
stackValue: 0).
startIndex := interpreterProxy positive32BitValueOf: (interpreterProxy
stackValue: 1).

[array := interpreterProxy stackValue: 2.
 file := self fileValueOf: (interpreterProxy stackValue: 3).

 (interpreterProxy failed
 "buffer can be any indexable words or bytes object except CompiledMethod"
 or: [(interpreterProxy isWordsOrBytes: array) not]) ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadArgument].

 elementSize := (interpreterProxy isWords: array) ifTrue: [4] ifFalse: [1].
 (startIndex >= 1
  and: [(startIndex + count - 1) <= (interpreterProxy slotSizeOf: array)])
[^interpreterProxy primitiveFailFor: PrimErrBadIndex].

 "Note: adjust startIndex for zero-origin indexing"
 bytesRead := self
sqFile: file
Read: count * elementSize
Into: (self cCoerce: (interpreterProxy firstIndexableField: array) to:
#'char *')
At: (startIndex - 1) * elementSize.
 *interpreterProxy primitiveFailureCode = PrimErrObjectMayMove*
* and: [(retryCount := retryCount + 1) <= 2] "Two objects, the file and the
array can move"*] whileTrue:
primitiveFailFor: PrimNoErr].
interpreterProxy failed ifFalse:
pop: 5 "pop rcvr, file, array, startIndex, count"
thenPush:(interpreterProxy integerObjectOf: bytesRead // elementSize)
 "push # of elements read"]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20130513/96a05935/attachment.htm

More information about the Vm-dev mailing list