Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2. class HandleMemorySegments {
  3.    
  4.     public function createMemory($size = 100) {
  5.         $strReturn = '';
  6.         while (true) {
  7.             $strMemID = "0x" . $this->generateMemoryID();
  8.             $strReturn = shmop_open($strMemID, "n", 0777, $size);
  9.             if ($strReturn != false) {
  10.                 break;
  11.             }
  12.         }
  13.         return $strReturn;
  14.     }
  15.     public function readMemory($id ,$amount ,$offsetStart = 0) {
  16.         return shmop_read ($id, $offsetStart, $amount);
  17.     }
  18.     public function writeMemory($id, $data, $offset = 0) {
  19.         $id = intval($id);
  20.         $semid=sem_get($id,1,0777);
  21.         sem_acquire($semid);
  22.         shmop_write($id, $data, $offset);
  23.         sem_release($semid);
  24.     }
  25.     public function deleteMemory($id) {
  26.         shmop_delete($id);
  27.     }
  28.     public function generateMemoryID() {
  29.         $x = '';
  30.         $hex='';
  31.         for ($i = 0; $i<3; $i++)
  32.         {
  33.             $x .=  dechex(rand(0,15));
  34.         }
  35.        
  36.         for ($i=0; $i < strlen($x); $i++)
  37.         {
  38.             $hex .= dechex(ord($x[$i]));
  39.         }
  40.        
  41.         return $hex;
  42.     }
  43.    
  44.     public function _count($shmopId){
  45.         $size = (int) shmop_size($shmopId);
  46.        
  47.         $dataInSH = shmop_read($shmopId, 0, $size);
  48.         $dataSizeInSH = 0;
  49.        
  50.         for ($i = 0; $i < $size; $i++) {
  51.             if ($this->strToHex($dataInSH[$i]) != 0) {
  52.                 $dataSizeInSH++;
  53.             }
  54.         }
  55.        
  56.         return $dataSizeInSH;
  57.     }
  58.    
  59.     protected function strToHex($string) {
  60.         $hex='';
  61.         for ($i=0; $i < strlen($string); $i++)
  62.         {
  63.             $hex .= dechex(ord($string[$i]));
  64.         }
  65.         return $hex;
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement