Userdata for memory functions

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Userdata for memory functions

Postby saxman » Mon Dec 15, 2008 3:55 pm

Since callbacks are static, it would really help if they always have access to a userdata point, so they can access the C++ class. In particular, it would be great to have NewtonWorld passed into these functions, as follow...

NewtonAllocMemory( NewtonWorld *world, int sizeInBytes )
NewtonFreeMemory( NewtonWorld *world, void *ptr, int sizeInBytes )
saxman
 
Posts: 12
Joined: Thu Oct 02, 2008 5:37 am

Re: Userdata for memory functions

Postby JernejL » Tue Dec 16, 2008 4:38 am

See: NewtonBodySetUserData and NewtonBodyGetUserData , you can use that to store a pointer to memory or a class that you allocated and use that from within your app.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Userdata for memory functions

Postby saxman » Tue Dec 16, 2008 4:48 am

I'm using those, but I need to access the class from within the memory allocate/free callback functions. Currently they only receive the size of the memory to allocate or the point to the memory to free.
saxman
 
Posts: 12
Joined: Thu Oct 02, 2008 5:37 am

Re: Userdata for memory functions

Postby Dave Gravel » Tue Dec 16, 2008 5:43 am

NewtonWorldSetUserData
NewtonWorldGetUserData

// Class
type
TMybananasManager = class

end;

var
BananasTree: TMybananasManager;

BananasTree:=TMybananasManager(NewtonWorldGetUserData(NewtonWorld));
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Userdata for memory functions

Postby saxman » Wed Dec 17, 2008 1:14 am

Yeah, I know how to get the userdata from the world. The problem is you don't get any world, body, material, etc... from a memory callback.

typedef void* (_cdecl *NewtonAllocMemory) (int sizeInBytes);

NewtonAllocMemory is static, so you have no "this" pointer, and NewtonWorld is not an argument. Only "int sizeInBytes". So in the case of multiple Newton worlds, you can't tell which one belongs with which class instance.
saxman
 
Posts: 12
Joined: Thu Oct 02, 2008 5:37 am

Re: Userdata for memory functions

Postby Dave Gravel » Wed Dec 17, 2008 1:57 am

Normally the command that you use is only a debug purpose resulting only the mem in use by newton something like this.
So in the case of multiple Newton worlds


I don't see really the problem in case that you have many world because normally the class have the world in self too,
and if you have multiple world normally you have multiple class instance for idle your world's.
It depending more from the way that you use for implement your newton world class instance's.

Edited:
If you need to use different class with different world, you can all times create one base class with a sort of ID.
You create all other different class based on this class, and you use the pointer base class and you detect the good class type by the id result...
Or maybe i totally not understand you right, if it is the case sorry.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Userdata for memory functions

Postby Julio Jerez » Wed Dec 17, 2008 11:16 am

Saxman is correct, ther is a problem with memory mananger. It is no a bug but it is a prblem with programming in c and c++.

it is the fact taht a menormy managert can not be local, I can no figure out hwo to make a manage that is local to one world.
the idea of usig a user data sound cool by it is very dificult to implement in practice.
I see what I can do, but you are right, when a wrodl is terminated they is no way to check if all memory is dealocated unless all oeth wordl are also terminated.
so solve this I think I have to creter teh allocator object and each wieth hsi onl call back and pass that to teh engine instead.

Thsi si a problem tah had boetre me for a very long time, many years in fact maybe is tiem to do sometion about it

thanks for the sugestion.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Userdata for memory functions

Postby JernejL » Wed Dec 17, 2008 2:03 pm

saxman wrote:So in the case of multiple Newton worlds, you can't tell which one belongs with which class instance.


If you have a pointer to some custom object in your app you can easily store the newton world variable in the object itself or the object's parent.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Userdata for memory functions

Postby saxman » Fri Dec 19, 2008 5:54 am

If you have a pointer to some custom object in your app you can easily store the newton world variable in the object itself or the object's parent


Yes, you can store a pointer to a class instance in NewtonWorld. The problem, you don't have acces to the NewtonWorld in the static NewtonAllocMemory callback.
saxman
 
Posts: 12
Joined: Thu Oct 02, 2008 5:37 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 20 guests

cron