-
Notifications
You must be signed in to change notification settings - Fork 9
/
MemoryCounter.php
executable file
·76 lines (63 loc) · 1.93 KB
/
MemoryCounter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace axios\tools;
class MemoryCounter
{
private $pathname;
private $type;
private $size;
public function __construct($pathname, $type, $size = 8)
{
$this->pathname = $pathname;
$this->type = $type;
$this->size = $size;
}
/**
* @param array $config
*/
public function config($config = [])
{
foreach ($config as $key => $val) {
if (isset($this->{$key})) {
$this->{$key} = $val;
}
}
}
public function create($ini = 0)
{
$shm = ftok($this->pathname, $this->type);
$shm_id = shmop_open($shm, 'c', 0644, $this->size);
$curr = $ini;
$curr = str_pad($curr, $this->size, '0', STR_PAD_LEFT);
shmop_write($shm_id, $curr, 0);
return $curr;
}
public function increase($step = 1)
{
$shm = ftok($this->pathname, $this->type);
$shm_id = shmop_open($shm, 'c', 0644, $this->size);
$curr = shmop_read($shm_id, $this->size, $this->size);
$curr = empty($curr) ? 1 : (int) $curr;
$curr = $curr + $step;
$curr = str_pad($curr, $this->size, '0', STR_PAD_LEFT);
shmop_write($shm_id, $curr, 0);
return $curr;
}
public function decrease($step = 1)
{
$shm = ftok($this->pathname, $this->type);
$shm_id = shmop_open($shm, 'c', 0644, $this->size);
$curr = shmop_read($shm_id, 0, $this->size);
$curr = $curr - $step;
$curr = str_pad($curr, $this->size, '0', STR_PAD_LEFT);
shmop_write($shm_id, $curr, 0);
return $curr;
}
public function current()
{
$shm = ftok($this->pathname, $this->type);
$shm_id = shmop_open($shm, 'c', 0644, $this->size);
$current = shmop_read($shm_id, 0, $this->size);
return empty($current) ? 0 : (int) $current;
}
}