Sign in to follow this  

Support for application types w/ custom alignment

This topic is 2662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was attempting to add support for wrappers over Eigen types when I ran into the alignment roadblock-- is there a way for me to specify to the memory manager that I need a particular application type to reside on a specific memory alignment? While constructors may handle initialization, the Angelscript runtime actually handles the allocation itself.

I can see control over something like this being very useful, so I'm hoping there's a method I overlooked somewhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
For reference types you use the asBEHAVE_FACTORY behavior to allocate memory for a type.

The unfortunate part of this is that the objects in question are value types-- any way I can extend factories to cover situations like this?

Share this post


Link to post
Share on other sites
I'm currently experimenting w/ the scoped reference type right now, but I'm not sure how this will interact with using objects of this type as class properties. Any insight?

EDIT: Said class properties would be members of a native C++ class. Specifically, things like position and orientation.

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
Let me know if it doesn't work. If it doesn't work already I'll make it work.


Will do. If/while you're around, is there any documentation on the contract(s) for the factory functions? Am I responsible for calling/wrapping the appropriate type constructor in the factory or will Angelscript call any registered constructors?

Share this post


Link to post
Share on other sites
You will need to register the factory functions for the scoped type, just like for other reference types. The factory function is responsible for allocating and initializing the memory.

The scoped reference type doesn't use reference counting as it is supposed to be destroyed when the variable goes out of scope. For this reason the AddRef behaviour can't be registered for this type. The Release behaviour is still necessary though, as it is the one responsible for destroying the object and freeing the memory.

Share this post


Link to post
Share on other sites
Progress update: The more I look into this, the more I think I may need to add some form of intrinsic support for aligned types to the language backend. From what I've been able to determine, this should actually be a (fairly) trivial extension-- I would need to add

A) Some method to specify to the runtime that the object type needs to be allocated along a certain boundary. A flag or additional integer designating which kind of alignment is required seems a good choice here. Alternately, something like RegisterAlignmentSensitiveObjectProperty() and a recursive 'alignment sensitivity' propagation check/flag might make things easier for the user. Additionally, this should be non-breaking with regards to bytecode, etc.

B) A means to guarantee that the allocated memory is on the needed boundary. Making a secondary allocation function type (something along the lines of asALIGNEDALLOCFUNC/asALIGNEDFREEFUNC and the corresponding debug logging functions) should be simple enough.

Thoughts? I'll try and put something together over the next few days and ship it to you if I end up with a sensible implementation.

Share this post


Link to post
Share on other sites
I'm interested in having this support natively, but I need to weigh this against complexity and performance impact. Especially in the support for native calling conventions. Is it even possible to pass this type by value?

Currently AngelScript doesn't allocate registered value types on the stack, but that is an optimization I plan on doing in a not too distant future. If there are special alignment requirements for a type, this may start to get complicated. Especially since the saved bytecode has to be platform independent, which means the alignment would have to be adjustable during the loading of the bytecode.

There is also the array type. Currently it also allocates the type on the heap, but in a future version it should probably allocate them in-row as well.

As I was typing this I came to think that AngelScript would probably have to have the ability to decide which value types should be allocated on the stack or on the heap. A type with special alignment requirements could always be allocated on the heap.

Share this post


Link to post
Share on other sites
OK, put together the basics for the aligned type system. I think having reference types be responsible for their own alignment is a fairly sound policy and haven't taken any steps in that domain. What I have added, however, is an additional parameter to the RegisterObjectType method to specify property block alignment, (and a new enumerated/default value for this parameter, asALIGNMENT_GENERIC, which falls back gracefully to stock allocation behavior) two new memory management functions, and the basic setup code for the feature to compile correctly on MSVC/GCC. Value types are the only kind of objects that will use the new behavior, reference types will simply fall back to the default.

Also, it appears this may interact with the template subtype code in the object type registration function. Currently I have things set up so that they just use the generic alignment behavior.

Tl;dr edition: I think I have a solution now. Let me test this for a bit to make sure it works, but if it's robust I'll submit the code changes to you ASAP.

EDIT: For my use, this was created against revision 680.

Share this post


Link to post
Share on other sites

This topic is 2662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this