Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class HandleMemorySegments {
- public function createMemory($size = 100) {
- $strReturn = '';
- while (true) {
- $strMemID = "0x" . $this->generateMemoryID();
- $strReturn = shmop_open($strMemID, "n", 0777, $size);
- if ($strReturn != false) {
- break;
- }
- }
- return $strReturn;
- }
- public function readMemory($id ,$amount ,$offsetStart = 0) {
- return shmop_read ($id, $offsetStart, $amount);
- }
- public function writeMemory($id, $data, $offset = 0) {
- $id = intval($id);
- $semid=sem_get($id,1,0777);
- sem_acquire($semid);
- shmop_write($id, $data, $offset);
- sem_release($semid);
- }
- public function deleteMemory($id) {
- shmop_delete($id);
- }
- public function generateMemoryID() {
- $x = '';
- $hex='';
- for ($i = 0; $i<3; $i++)
- {
- $x .= dechex(rand(0,15));
- }
- for ($i=0; $i < strlen($x); $i++)
- {
- $hex .= dechex(ord($x[$i]));
- }
- return $hex;
- }
- public function _count($shmopId){
- $size = (int) shmop_size($shmopId);
- $dataInSH = shmop_read($shmopId, 0, $size);
- $dataSizeInSH = 0;
- for ($i = 0; $i < $size; $i++) {
- if ($this->strToHex($dataInSH[$i]) != 0) {
- $dataSizeInSH++;
- }
- }
- return $dataSizeInSH;
- }
- protected function strToHex($string) {
- $hex='';
- for ($i=0; $i < strlen($string); $i++)
- {
- $hex .= dechex(ord($string[$i]));
- }
- return $hex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement