How to return an array in a primitive ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to return an array in a primitive ?

Mariano Martinez Peck
 
Hi folks. I am writing some primitives in Interpreter and now I need to return more than one value. Of course, in a OOP the ideal would be to return an object that represents the result. That object would also have all the instance variables I want.

So..first question, is it possible to create and push on the stack an object of my own class ?   If true, con can I do it ?

now...suppose I cannot do that, the other option is to create an array with the values I want to return, and then return the array. For this particular case, I have 3 or 4 interegers I would like to put in the array, and push it in the stack. Is this possible ?  If true, how ?

Are there other options for what I need ?

Thanks in advance for any help.

Mariano
Reply | Threaded
Open this post in threaded view
|

Re: How to return an array in a primitive ?

Bert Freudenberg

On 27.04.2010, at 11:27, Mariano Martinez Peck wrote:
> Hi folks. I am writing some primitives in Interpreter and now I need to return more than one value. Of course, in a OOP the ideal would be to return an object that represents the result. That object would also have all the instance variables I want.
>
> So..first question, is it possible to create and push on the stack an object of my own class ?   If true, con can I do it ?
>
> now...suppose I cannot do that, the other option is to create an array with the values I want to return, and then return the array. For this particular case, I have 3 or 4 interegers I would like to put in the array, and push it in the stack. Is this possible ?  If true, how ?
>
> Are there other options for what I need ?

It is better to avoid allocations in a primitive. So make a 3 or 4 element array, and pass this as argument to the primitive, which fills in the actual values.

- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: How to return an array in a primitive ?

David T. Lewis
 
On Tue, Apr 27, 2010 at 01:01:33PM +0200, Bert Freudenberg wrote:

>
> On 27.04.2010, at 11:27, Mariano Martinez Peck wrote:
> > Hi folks. I am writing some primitives in Interpreter and now I need to return more than one value. Of course, in a OOP the ideal would be to return an object that represents the result. That object would also have all the instance variables I want.
> >
> > So..first question, is it possible to create and push on the stack an object of my own class ?   If true, con can I do it ?
> >
> > now...suppose I cannot do that, the other option is to create an array with the values I want to return, and then return the array. For this particular case, I have 3 or 4 interegers I would like to put in the array, and push it in the stack. Is this possible ?  If true, how ?
> >
> > Are there other options for what I need ?
>
> It is better to avoid allocations in a primitive. So make a 3 or 4 element array, and pass this as argument to the primitive, which fills in the actual values.

As Bert says, it is safer to avoid allacations in the primitive. But
for an example of how to do it properly, see
  FilePlugin>>makeDirEntryName:size:createDate:modDate:isDir:fileSize:

Note the use of #pushRemappableOop: and popRemappableOop, which is required
to prevent the object pointers from changing due to garbage collection.
Allocating an object in the primitive can and will invoke garbage
collection, which leads to nasty intermittent bugs if you do not
use #pushRemappableOop:.

Dave
 
Reply | Threaded
Open this post in threaded view
|

Re: How to return an array in a primitive ?

Mariano Martinez Peck
 


As Bert says, it is safer to avoid allacations in the primitive. But
for an example of how to do it properly, see
 FilePlugin>>makeDirEntryName:size:createDate:modDate:isDir:fileSize:

Note the use of #pushRemappableOop: and popRemappableOop, which is required
to prevent the object pointers from changing due to garbage collection.
Allocating an object in the primitive can and will invoke garbage
collection, which leads to nasty intermittent bugs if you do not
use #pushRemappableOop:.



Thanks Dave. I was just going to ask why it was "unsafe" but you already answered. I was going to ask an example too, but I got it also :)

So...perfect. I just write my primitive with something similar to the FilePlugin example.

Thank you very much to both of you.

mariano
 

Reply | Threaded
Open this post in threaded view
|

Re: How to return an array in a primitive ?

Bert Freudenberg

On 27.04.2010, at 16:18, Mariano Martinez Peck wrote:

>
>> As Bert says, it is safer to avoid allacations in the primitive. But
>> for an example of how to do it properly, see
>>  FilePlugin>>makeDirEntryName:size:createDate:modDate:isDir:fileSize:
>>
>> Note the use of #pushRemappableOop: and popRemappableOop, which is required
>> to prevent the object pointers from changing due to garbage collection.
>> Allocating an object in the primitive can and will invoke garbage
>> collection, which leads to nasty intermittent bugs if you do not
>> use #pushRemappableOop:.
>>
> Thanks Dave. I was just going to ask why it was "unsafe" but you already answered.

Not quite. There is more to it than just remapping oops. Try to imagine what happens if the allocation fails because there is not enough memory, even after GC.

For small allocations there should be enough of a safe margin (hopefully the low-space watcher kicks in early enough). But at least for big allocations you also need to check if they failed.

- Bert -

> I was going to ask an example too, but I got it also :)
>
> So...perfect. I just write my primitive with something similar to the FilePlugin example.
>
> Thank you very much to both of you.
>
> mariano
>  
>


Reply | Threaded
Open this post in threaded view
|

Re: How to return an array in a primitive ?

Mariano Martinez Peck
 


On Tue, Apr 27, 2010 at 5:04 PM, Bert Freudenberg <[hidden email]> wrote:

On 27.04.2010, at 16:18, Mariano Martinez Peck wrote:
>
>> As Bert says, it is safer to avoid allacations in the primitive. But
>> for an example of how to do it properly, see
>>  FilePlugin>>makeDirEntryName:size:createDate:modDate:isDir:fileSize:
>>
>> Note the use of #pushRemappableOop: and popRemappableOop, which is required
>> to prevent the object pointers from changing due to garbage collection.
>> Allocating an object in the primitive can and will invoke garbage
>> collection, which leads to nasty intermittent bugs if you do not
>> use #pushRemappableOop:.
>>
> Thanks Dave. I was just going to ask why it was "unsafe" but you already answered.

Not quite. There is more to it than just remapping oops. Try to imagine what happens if the allocation fails because there is not enough memory, even after GC.


Bert, forgive my ignorance, but I don't understand the difference between allocating object in this place (inside a VM primitive)  and allocating objects in the image side. Why in the VM side I should take care about this problems but not when do it from image side ?
 
For small allocations there should be enough of a safe margin (hopefully the low-space watcher kicks in early enough). But at least for big allocations you also need to check if they failed.


Ok, thanks. In my particular case, it is an array with just 4 numbers and to get statistics.

Thanks

Mariano
 
- Bert -

> I was going to ask an example too, but I got it also :)
>
> So...perfect. I just write my primitive with something similar to the FilePlugin example.
>
> Thank you very much to both of you.
>
> mariano
>
>