On 23 September 2013 16:23, Camillo Bruni <[email protected]> wrote:
> Hi Jan, > > I think I will add the ByteArray accessor to NBExternalAddress today > or tomorrow since I need it as well for another project. > > hmm, reading from memory into bytearray can be done with memory copy: inputs: address , offset , size to read newAddress := NBExternalAddress value: address value + offset. buffer := ByteArray new: size. NativeBoost memCopy: newAddress to: buffer size: size. same way, writing, just swap the source and destination: newAddress := NBExternalAddress value: address value + offset. buffer "is given from somewhere". NativeBoost memCopy: buffer to: newAddress size: size. but as Jan noted, you cannot tell to write starting at specified offset from/to bytearray, e.g.: copy from: address to: buffer + someOffset neither: copy from: buffer + someOffset to: someAddress this where we need to introduce special 'field address' type, so you can construct it like this: offsetAddress := buffer nbAddressAt: offset. so then you can use it to pass to any function, which expects address, like memory copy or any foreign function. Since objects are moving in memory, we cannot calculate address of field before hand: address := NBExternalAddress value: someObject address + offset. because if GC will happen, after computing such address and its actual use, you will read/write to wrong location. Thus we should keep oop + offset up to the point of passing it to external function, under controllable conditions, that guarantee there's no GC is possible. > Concerning the Streams, I think it is the easiest solution to wrap around > a ByteArray. Otherwise you could subclass one of the main stream classes > and implement your own primitive methods with FFI to read and write bytes. > In the worst case you will have to implement all primitive methods with FFI > that you find in the StandardFileStream. > > > On 2013-09-22, at 10:12, Jan van de Sandt <[email protected]> wrote: > > Hello, > > > > I'm trying to implement a binary ReadStream that gets its data not from a > > Smalltalk collection but from some external memory address. I got this > > address from a library call using NativeBoost. > > > > The NBExternalAddress has methods to access bytes, int's and longs but > > there is no method to get a ByteArray starting at a specified offset and > > with a specified length. I need a method like this to implement the > > ReadStream>>next: method efficiently. > > > > Is it possible ti implement such a method or are there other ways to > > implement a ReadStream efficiently? > > > > In [1] I read that there was already an idea to implement a nbAddressAt: > > method. This would also be using in combination with NativeBoost > > class>>#memCopy:to:size > > > > Regrads, > > Jan. > > > > [1] > > > http://forum.world.st/Pharo-dev-NB-Review-amp-fixes-amp-ideas-td4698514.html > > -- Best regards, Igor Stasenko.
