> On 27 Apr 2015, at 16:14, Henrik Johansen <[email protected]> > wrote: > > >> On 26 Apr 2015, at 7:29 , Max Leske <[email protected]> wrote: >> >> Hi guys >> >> I have a problem with NBExternalArray. AFAICT the generated assembly should >> be able to handle arbitrary elements in the array (as long as they are all >> of the same type). The operations required to read / write an element are >> given by the external type specified for the collection (e.g ‘char’ or >> ‘int’). All of this works wonderfully for char, int, NBExternalAddress etc. >> But I’m trying to get it working for strings. So here is the code that I >> would expect to work: >> >> arrayClass := NBExternalArray anonymousSubclassInitElementType: 'String'. >> array := arrayClass new: 1. >> array at: 1 put: ‘foo'. >> array at: 1. “—> should procude ‘foo’, but produces arbitrary strings” >> >> I can make it work with NBExternalAddress of course but I don’t want to if I >> can avoid it. >> >> I’d appreciate it if someone (Nicolai?) could take a look at >> NBExternalArray>>emitRead / emitWrite and see if there’s an easy solution. >> Especially for strings I don’t think it should be too hard since all that’s >> needed is already present in the string handling facilities. >> >> Cheers, >> Max > > There's a solution, but I doubt you could call it simple. > NBExternalArray is meant to hold const sized objects, so is allocated with > the proper amount of slots up front. > If you want it to hold pointers to variably sized objects (like strings), > since we can't pin the objects, they need to be copied (well, would have to > do that anyways for Strings, which need it'd need to do dynamic allocation > (and freeing when appropriate), so emitWrite would become quite a bit more > complicated, + necessary object finalization would have to occur.
Right. So space is allocated for the constant sized objects or the pointers but not for the variable sized objects… That explains what I’m seeing with the memory being used sometimes. I guess allocation and finalization can easily be done in a dedicated subclass, so I’ll try that. Thanks a lot Henry! Cheers, Max > > You *can* work around it, by using an element type that is externally > allocated, but it adds quite a bit of boilerplate, here's the workspace PoC > code: > > string := 'herru€'. > bytes := ZnUTF8Encoder new encodeString: string. > mem := NativeBoost allocate: bytes size + 1. > NativeBoost memCopy: bytes to: mem size: bytes size. > > arrClass := NBExternalArray anonymousSubclassInitElementType: 'char *'. > arr := arrClass new: 1. > arr at: 1 put: mem. > ZnUTF8Encoder new decodeBytes: (arr at:1) readString asByteArray > > Cheers, > Henry
