/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */

#if KEOS_DEBUG_MEMORY_MANAGER && KEOS_DEBUG_MODE

#ifndef KEOS_MEMORYMACROS_H
#define KEOS_MEMORYMACROS_H

/** Overload of the operator new.
 *  @param nSize Size to be allocated.
 *  @param strFile Source file.
 *  @param nLine Line in source file.
 *  @return Pointer on the allocated zone.
 */
inline void* operator new(size_t nSize, const char* strFile, int nLine)
{
    return Keos::CMemoryManager::Instance().Allocate(nSize, strFile, nLine, false);
}

/** Overload of the operator new[].
 *  @param nSize Size to be allocated.
 *  @param strFile Source file.
 *  @param nLine Line in source file.
 *  @return Pointer on the allocated zone.
 */
inline void* operator new[](size_t nSize, const char* strFile, int nLine)
{
    return Keos::CMemoryManager::Instance().Allocate(nSize, strFile, nLine, true);
}

/** Overload of the operator delete.
 *  @param pPtr Pointer on the zone to be released.
 */
inline void operator delete(void* pPtr)
{
    Keos::CMemoryManager::Instance().Free(pPtr, false);
}

/** Overload of the operator delete.
 *  @param pPtr Pointer on the zone to be released.
 *  @param strFile Source file.
 *  @param nLine Line in source file.
 */
inline void operator delete(void* pPtr, const char* strFile, int nLine)
{
    Keos::CMemoryManager::Instance().NextDelete(strFile, nLine);
    Keos::CMemoryManager::Instance().Free(pPtr, false);
}


/** Overload of the operator delete[].
 *  @param pPtr Pointer on the zone to be released.
 */
inline void operator delete[](void* pPtr)
{
    Keos::CMemoryManager::Instance().Free(pPtr, true);
}

/** Overload of the operator delete[].
 *  @param pPtr Pointer on the zone to be released.
 *  @param strFile Source file.
 *  @param nLine Line in source file.
 */
inline void operator delete[](void* pPtr, const char* strFile, int nLine)
{
    Keos::CMemoryManager::Instance().NextDelete(strFile, nLine);
    Keos::CMemoryManager::Instance().Free(pPtr, true);
}

#endif // KEOS_MEMORYMACROS_H

// Macros new and delete
#ifndef new
    #define new    new(__FILE__, __LINE__)
    #define delete Keos::CMemoryManager::Instance().NextDelete(__FILE__, __LINE__), delete
#endif

#endif // KEOS_DEBUG_MEMORY_MANAGER
