Cubism SDK‎ > ‎Live2D Library‎ > ‎

Custom Memory Allocator【C++】

With C++, you can customize how Live2D reserves the memory.

It inherits LDAllocator class and pass the overridden each function when initializing.

Specify the memory allocator when initializing Live2D as below;
If you do not specify, a standard allocator (like new or delete) will be used.

#include "memory/LDAllocator.h"

MyLive2DAllocator myAllocator ;// Custom memory allocator
Live2D::init( &myAllocator );

Refer to the following code to customize the allocator.
/************************************************************
* Customize the order to secure the memory for Live2D
* Override the virtual function of live2d::LDAllocator
************************************************************/
class MyLive2DAllocator : public live2d::LDAllocator{
public:

MyLive2DAllocator() {
}

virtual ~MyLive2DAllocator() {
}

//---------------------------------------------------------------------------
// Process of initializing
//
// Process of initializing the call from Live2D::init().Do initializing as needed.
//---------------------------------------------------------------------------
virtual void init(){ }

//---------------------------------------------------------------------------
// Process of finishing
//
// Process of finishing the call in the last of Live2D::dispose(). Do initializing as needed.
//---------------------------------------------------------------------------
virtual void dispose(){ }


//---------------------------------------------------------------------------
// malloc
//
// ※It is called in certain amount size (more than 1024) except initializing.
//---------------------------------------------------------------------------
virtual void* pageAlloc( unsigned int size , LDAllocator::Type allocType ){
void* ptr ;
switch( allocType ){
case LDAllocator::MAIN: //normal memory
ptr = ::malloc(size) ;
break ;

case LDAllocator::GPU: //Point, index, UV to pass GPU
ptr = ::malloc(size) ;
break ;

default: //Others (will possibly be added)
ptr = ::malloc(size) ;
break ;
}
return ptr ;
}

//---------------------------------------------------------------------------
// free
//---------------------------------------------------------------------------
virtual void pageFree( void* ptr , LDAllocator::Type allocType ){
::free(ptr);
}

} ;


Comments