Reference Counting Problem

Started by
2 comments, last by Madcap 6 years, 6 months ago

Hi,

I wish to register my math classes with Angelscript and use reference counting to manage the memory.  However, the problem I have with this is the need to add an integer to the class to keep track of the number of references to the object used (addref/release methods).  The reason for this is that my math classes work with OpenGL, so when I pass an array (pointer) of matrices or vectors, they will work directly with opengl shader programs.  Adding an integer would break this and was wondering if there's a way to solve this problem without using external reference counters which would need a lookup and slow the whole thing down.  I'm hoping somebody can help me overcome this problem.

Thanks.

C++

Advertisement

Do you really need the vector and matrix types to be reference types? Couldn't they be value types? In my own math library I treat them as value types.

Unfortunately safety and performance do not always go hand-in-hand. Perhaps you should not expose the opengl shader programs directly to the script, and instead have proxy functions that transform the data prepared by the script into the format expected by the opengl shaders.

If you do not really bother with safety you can relax the controls in the script compiler by setting the engine property asEP_ALLOW_UNSAFE_REFERENCES to true. That might help allow you to work a little closer to the way C++ does without requiring the matrix and vector types to be reference types.

Along this way of thinking you can also provide functions to allow the scripts to manually allocate and free memory for matrices and vectors. You could register a matrix_ptr type to represent a pointer to a matrix allocated on the heap, then have implicit cast operators to cast to a matrix type.

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I just felt it was easier to share the math classes and pass objects to and from the scripts as pointers, whilst storing some of them in the scripts.  The problem I was having is the added 4 bytes from the counter.

Considering the openGL function takes a float pointer and that I would normally do the selection to build this array of data in the rendering loop, I decided I could probably just use a Union.  Something like this:


union
{
	struct
	{
		float x, y, z;
	};

	struct
	{
		float array[3];
	};
};

I can then just use obj.array and push that into a float array to then pass to openGL.  I'll look into some of your suggestions (I've only had a few days with AS).   

This topic is closed to new replies.

Advertisement