/******************************************************************************
 * $Id: gfx_debug.cc,v 1.1 2006/10/10 04:22:05 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Debug support (unused because my code is always perfect).
 *//*
 * LEGAL:	COPYRIGHT (C) 2006 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 ******************************************************************************/

#if DEBUG

#define GFX_DEBUG_CC 1
#include "gfx.hh"
#include "gfx_stream.hh"
#include "gfx_gfxsys.hh"
#include <sstream>
#include <sys/time.h>
#include <time.h>
#if __GLIBC__
#include <malloc.h>
#endif

namespace gfx {

/*****************************************************************************
 * Return total amount of bytes allocated.
 *****************************************************************************/
uint
MemUsed( void )
{
#if __GLIBC__
	struct mallinfo meminfo;
	meminfo = mallinfo();
	return (uint)meminfo.arena;
#else
    return 0;  // yeh right
#endif
}

/*****************************************************************************
 * Measure the amount of time a function takes.
 *****************************************************************************/
void
MeasureTime( void(*func)(long), long funcArg, const string& funcName )
{
	// ANSI C code that measures the time the process consumed in user-mode.
	// Excludes kernel-mode (doesn't count time spent in video driver).
	int clocks1 = clock();
	func( funcArg );  // call function to be measured
	int clocks2		 = clock();
	int clocksPerSec = CLOCKS_PER_SEC;
	fp  secUser		 = fp(clocks2 - clocks1) / fp(clocksPerSec);
	ostringstream oss;
	oss << "------------------------------------\n"
        << "function: " << funcName << "\n"
	    << "secs (user) = " << secUser << "\n"
        << "------------------------------------\n";
	string result = oss.str();
	*gStreamOut << result.c_str();
}

} // namespace gfx

#endif // DEBUG
