<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Shared Memory - Essais02</title>
  <style type="text/css">
  </style>
  </head>
<body>
 
<h1>Shared Memory - Essais02</h1>
<ul>
  <li><a href="essais02.php?test=SEARCHMAXMEM">SEARCHMAXMEM</a></li>
</ul>
 
<?php
 
error_reporting(-1);
 
$action = (isset($_GET['test']) && preg_match('/[a-z]{1,20}/i',$_GET['test']) ) ? $_GET['test'] : null ;
switch($action)
{
  case 'SEARCHMAXMEM':
    action_searchmaxmem();
    break;
}
 
function getShmKey()
{
  return ftok(__FILE__, 'A' );
}
 
function action_searchmaxmem()
{
  echo 'SEARCHMAXMEM:';
  echo '<pre>';
  $SHM_KEY = getShmKey();
  $size = 1024*1024 ;
  for( $i=0; $i<60; $i++)
  {
    $size=intval($size);
    if( $size==0 )
      break;
    $data =  shm_attach( $SHM_KEY, $size, 0600);
    if( $data == null || $data === FALSE)
    {
      echo 'FAILED for size='.formatBytesSize($size)."\n";
      break;
    }
    else
    {
      echo 'OK for size='.formatBytesSize($size)."\n";
      shm_remove($data);
      shm_detach($data);
    }
    $size += 1024*1024 ;
  }
  echo '</pre>';
}
 
/**
 * @parame int $sizeInBytes
 * @return string 
 */
function formatBytesSize( $sizeInBytes, $afterColon=2)
{
    // less than 1kB
    if($sizeInBytes < 1024)
    {
        return $sizeInBytes . ' B';
    }
    // between 1kB and 1MB
    if ($sizeInBytes >= 1024 && $sizeInBytes < 1048576)
    {
        return number_format( $sizeInBytes / 1024, $afterColon) . ' KB';
    }
    // between 1MB and 1GB
    if ($sizeInBytes >= 1048576 && $sizeInBytes < 1073741824)
    {
        return number_format( $sizeInBytes / 1048576, $afterColon) . ' MB';
    }
    // more than 1GB
    return number_format( $sizeInBytes / 1073741824, $afterColon) . ' GB';
}
 
?>
 
</body>
</html>