Custom memory management

Sly
Posts: 16
Joined: Wed Apr 23, 2008 9:57 am

Custom memory management

Post by Sly »

Is there a proper way to override the btAlignedAlloc/btAlignedFree with our own memory management? I've seen mentioned in other posts about providing our own stack and pool allocators, but those classes just use btAlignedAlloc anyway.
Sly
Posts: 16
Joined: Wed Apr 23, 2008 9:57 am

Re: Custom memory management

Post by Sly »

I've now implemented functionality to allow the game to optionally provide their own custom memory allocation functions. It retains previous behaviour unless btAlignedAllocSetCustom() is called to provide new functions. You will want to call btAlignedAllocSetCustom() before any other Bullet code is called so that all allocations and frees go through your own functions.

New files attached.
You do not have the required permissions to view the files attached to this post.
Sly
Posts: 16
Joined: Wed Apr 23, 2008 9:57 am

Re: Custom memory management

Post by Sly »

I also have another fix. We override new and delete in our game code, so including btAlignedObjectArray.h (via btDynamicsCommon.h) causes a redefinition error of the new operator. Commenting out BT_USE_PLACEMENT_NEW causes compile errors in Bullet (placement new is required), so I have the following solution.

Replace

Code: Select all

#define BT_USE_PLACEMENT_NEW 1
with

Code: Select all

#ifndef BT_USE_PLACEMENT_NEW
#define BT_USE_PLACEMENT_NEW 1
#endif  // BT_USE_PLACEMENT_NEW
and the use of the macro

Code: Select all

#ifdef BT_USE_PLACEMENT_NEW
with

Code: Select all

#if BT_USE_PLACEMENT_NEW
The macro will not be declared in builds of the Bullet libraries, so the placement new will function as normal. In game code, declare BT_USE_PLACEMENT_NEW=0 in the project file or before btDynamicsCommon.h is included to allow the game's placement new operator to be used. No more compile errors.

Update: Note that this only affected PS3 and the GCC compiler. Win32 and Xbox 360 were ok, but they are still happy with this fix as well.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Custom memory management

Post by Erwin Coumans »

Is there a proper way to override the btAlignedAlloc/btAlignedFree with our own memory management?
Best way it to integrate your memory manager in Bullet/src/LinearMath/btAlignedAllocator.cpp.

Does your patch add generic registration for memory managers? That might come in handy.

The issue with redefinition of new should be sorted too, so here is a new issue to track progress of this:
http://code.google.com/p/bullet/issues/detail?id=57

Thanks a lot for the help,
Erwin
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Custom memory management

Post by Erwin Coumans »

There are a few issues with your patch that needs discussion:

The user might have a general memory allocator that doesn't have support for alignment. So it would be better to provide replacement for malloc/free as used inside void* btAlignedAllocInternal.

If we want to support custom aligned allocators (that already deal with arbitrary alignment), then we need to provide two options for custom allocators: an aligned custom allocator and un-aligned custom allocator.

Also, the patch changes some behaviour: by default, Bullet doesn't track down line number and file of each allocation, unless BT_DEBUG_MEMORY_ALLOCATIONS is defined. Can your btAlignedAllocSetCustom be modified so it checks for BT_DEBUG_MEMORY_ALLOCATIONS define?

What do you think?
Thanks,
Erwin
Sly
Posts: 16
Joined: Wed Apr 23, 2008 9:57 am

Re: Custom memory management

Post by Sly »

I will revisit the changes taking your comments into account.
Sly
Posts: 16
Joined: Wed Apr 23, 2008 9:57 am

Re: Custom memory management

Post by Sly »

I have revised the custom memory management callbacks to provide two sets of callbacks: aligned and unaligned.

I also had to disable profiling (btQuickprof.h) in my local copy. There is a static declaration of CProfileNode that has two pointers to memory that get allocated during runtime using the custom memory allocation routines. This memory is not cleaned up before the program exits, leaving the destructor to free the memory. By this time, the custom memory allocators no longer work because the application-controlled heap has usually been destroyed. This would primarily affect PC that requires a clean exit.

Code: Select all

CProfileNode CProfileManager::Root is constructed
WinMain() is called
  Application-controlled heap is created
  Set custom allocation callbacks for Bullet
  Create a physics simulation world and related objects
     Do useful things with it.  This allocates memory and sets CProfileNode::Child and CProfileNode::Sibling
  Destroy physics simulation world and related objects
  Remove custom allocation callbacks for Bullet
  Application-controlled heap is destroyed
WinMain() returns
CProfileNode CProfileManager::Root is destroyed, which calls delete on Child and Sibling
The heap that contained this memory is no longer present
*boom*
You do not have the required permissions to view the files attached to this post.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Custom memory management

Post by Erwin Coumans »

Your latest patch has been applied. Can you verify the upcoming Bullet 2.69 beta1 or later?

Can you try to call the new CProfileManager::CleanupMemory(), to cleanup btQuickprof memory?

Thanks a lot for the contribution,
Erwin
Sly
Posts: 16
Joined: Wed Apr 23, 2008 9:57 am

Re: Custom memory management

Post by Sly »

Thanks for that. Will the SPU-optimized version be available as well, or will that wait for the final release of 2.69?