source file REDMemoryAllocator.h

Go to the documentation of this file.

#ifndef RED_MEMORYALLOCATOR_H
#define RED_MEMORYALLOCATOR_H

//************************************************************************************************************
// Copyright (c) 2005 Redway3D. All rights reserved. Redway3D is a registered trademark. Any reproduction and /
// or utilization of any material from this document without Redway3D?s express authorization are prohibited.
//************************************************************************************************************

#include "RED.h"


//******************************************************************************
//*** MemoryAllocator class ****************************************************
//******************************************************************************

namespace RED
{
  class MemoryLeakTracker;
  
  
  // Memory allocator class:
  // -----------------------
  
  class REDFullExp MemoryAllocator
  {
    
    public:
    
    // Memory allocator types:
    // -----------------------
    
    typedef void* (*RED_CUSTOM_NEW) ( size_t iSize );
        
    typedef void  (*RED_CUSTOM_DELETE) ( void* iMemory );

    typedef void* (*RED_CUSTOM_REALLOC) ( void* iMemory, size_t iSize );
    
    
    // Singleton access:
    // -----------------
    
    static MemoryAllocator& Get();
    
    
    // Enabling / disabling memory leak tracking:
    // ------------------------------------------
    
    void SetMemoryTracking( bool iTracking );
    
    
    inline RED::MemoryLeakTracker* GetMemoryLeakTracker() const;
    
    
    // Customizing memory allocation:
    // ------------------------------
    
    RED_CUSTOM_NEW SetCustomNew( RED_CUSTOM_NEW iCustomNew );
    
    
    RED_CUSTOM_NEW SetCustomNewArray( RED_CUSTOM_NEW iCustomNewArray );
    
    
    RED_CUSTOM_DELETE SetCustomDelete( RED_CUSTOM_DELETE iCustomDelete );
    
    
    RED_CUSTOM_DELETE SetCustomDeleteArray( RED_CUSTOM_DELETE iCustomDeleteArray );
    
    
    RED_CUSTOM_REALLOC SetCustomRealloc( RED_CUSTOM_REALLOC iCustomRealloc );


    void* Allocate( size_t iSize, int iCaller );
    

    void* Allocate( size_t iSize );
    
    
    void* Reallocate( void* iSource, size_t iSize, int iCaller );
    
    
    void* Reallocate( void* iSource, size_t iSize );
    
    
    void Free( void* iAddress );
    
    
  protected:
    
    // Construction and destruction methods:
    // -------------------------------------
    
    MemoryAllocator();
    
    
    ~MemoryAllocator();
    
    
    // Allocator's parameters:
    // -----------------------
    
    RED_CUSTOM_NEW _new;
    
    RED_CUSTOM_NEW _newarray;
    
    RED_CUSTOM_DELETE _delete;
    
    RED_CUSTOM_DELETE _deletearray;

    RED_CUSTOM_REALLOC _realloc;
    
    
    // The memory leak tracker:
    // ------------------------
    
    RED::MemoryLeakTracker* _mlt;
  };
};


//******************************************************************************
//*** RED::MemoryAllocator inlined methods *************************************
//******************************************************************************

inline RED::MemoryLeakTracker *RED::MemoryAllocator::GetMemoryLeakTracker () const
{
  return _mlt;
}


//******************************************************************************
//*** REDsdk's known memory allocation sources *********************************
//******************************************************************************

#define RED_MLT_UNKNOWN                           0
#define RED_MLT_RAYTRACER                         1
#define RED_MLT_IMAGE_2D                          2
#define RED_MLT_IMAGE_3D                          3
#define RED_MLT_IMAGE_CUBE                        4
#define RED_MLT_IMAGE_COMPOSITE                   5
#define RED_MLT_IMAGE_READBACK                    6
#define RED_MLT_SHAPE_TRANSFORM                   7
#define RED_MLT_SHAPE_DATA                        8
#define RED_MLT_MATERIAL                          9
#define RED_MLT_SHADER                           10
#define RED_MLT_CLUSTER                          11
#define RED_MLT_OPENGL_DRIVER                    12
#define RED_MLT_STRING                           13
#define RED_MLT_MATERIAL_CONTROLLER_COMPILER     14
#define RED_MLT_STL                              15
#define RED_MLT_LAST_MLT                         16


//******************************************************************************
//*** REDsdk's Miscellaneous allocation defines ********************************
//******************************************************************************

#if defined(_WIN64) || defined(_LIN64) || defined(_MAC64)

#define RED_MLT_DYN_ALLOC_SIZE      48

#elif defined(_WIN32) || defined(_LIN32) || defined(_MAC32)

#define RED_MLT_DYN_ALLOC_SIZE      24

#else

#define RED_MLT_DYN_ALLOC_SIZE       0

#endif


#define RED_MLT_VEC_SIZE( vector, type ) ( RED_MLT_DYN_ALLOC_SIZE + (vector).size() * sizeof( type ) )


//******************************************************************************
//*** Custom REDsdk allocation entry points ************************************
//******************************************************************************

#ifndef __RED_MLT_CALLER__
#define __RED_MLT_CALLER__   RED_MLT_UNKNOWN
#endif

#define rnew                  new( __RED_MLT_CALLER__ )
#define rnewc( caller )       new( caller )
#define rdelete               delete
#define rmalloc( size )       RED::MemoryAllocator::Get().Allocate( size, __RED_MLT_CALLER__ )
#define rrealloc( ptr, size ) RED::MemoryAllocator::Get().Reallocate( ptr, size, __RED_MLT_CALLER__ )
#define rfree                 RED::MemoryAllocator::Get().Free


#endif