<?php

namespace Core;

/**
 * Memory related functions
 */
class Memory
{
	/**
	 * Format memory size string
	 *
	 * @param int $size Memory size
	 * @param int $precision Decimal precision
	 */
	static function format($size, $precision = 1)
	{
		$p = array('', 'K', 'M', 'G', 'T', 'P');
		$i = (int)(log($size, 1024));
		return @round($size / pow(1024, $i), $precision) . ' ' . $p[$i] . 'B';
	}
	
	/**
	 * Get memory usage
	 *
	 * @param bool $real Get full memory usage, allocated size otherwise
	 * @param int $precision Decimal precision
	 */
	static function getUsage($real = false, $precision = 1)
	{
		return self::format(memory_get_usage($real), $precision);
	}
}
