|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sj-i/php-profiler package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace PhpProfiler\Lib\Process\Pointer; |
|
15
|
|
|
|
|
16
|
|
|
use FFI\CData; |
|
17
|
|
|
use FFI\CInteger; |
|
18
|
|
|
use FFI\CPointer; |
|
19
|
|
|
use FFI\CType; |
|
20
|
|
|
use PhpProfiler\Lib\PhpInternals\CastedCData; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @template T of \PhpProfiler\Lib\Process\Pointer\Dereferencable |
|
24
|
|
|
*/ |
|
25
|
|
|
class Pointer |
|
26
|
|
|
{ |
|
27
|
|
|
/** @param class-string<T> $type */ |
|
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
public string $type, |
|
30
|
|
|
public int $address, |
|
31
|
|
|
public int $size, |
|
32
|
|
|
) { |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function indexedAt(int $n): Pointer |
|
36
|
|
|
{ |
|
37
|
|
|
return new Pointer( |
|
38
|
|
|
$this->type, |
|
39
|
|
|
$this->address + $n * $this->size, |
|
40
|
|
|
$this->size, |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getCTypeName(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->type::getCTypeName(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param CastedCData<CData> $casted_cdata |
|
51
|
|
|
* @param Pointer<T> $pointer |
|
52
|
|
|
* @return T |
|
|
|
|
|
|
53
|
|
|
*/ |
|
54
|
|
|
public function fromCastedCData( |
|
55
|
|
|
CastedCData $casted_cdata, |
|
56
|
|
|
Pointer $pointer |
|
57
|
|
|
): mixed { |
|
58
|
|
|
return $this->type::fromCastedCData($casted_cdata, $pointer); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @template TType of \PhpProfiler\Lib\Process\Pointer\Dereferencable |
|
63
|
|
|
* @param class-string<TType> $type |
|
|
|
|
|
|
64
|
|
|
* @param CPointer $c_pointer |
|
65
|
|
|
* @return Pointer<TType> |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function fromCData( |
|
68
|
|
|
string $type, |
|
69
|
|
|
CData $c_pointer, |
|
70
|
|
|
): self { |
|
71
|
|
|
/** @var CInteger $addr */ |
|
72
|
|
|
$addr = \FFI::cast('long', $c_pointer); |
|
73
|
|
|
/** |
|
74
|
|
|
* @psalm-suppress InaccessibleMethod |
|
75
|
|
|
* @var CData $element |
|
76
|
|
|
*/ |
|
77
|
|
|
$element = $c_pointer[0]; |
|
78
|
|
|
/** @param CType $ctype */ |
|
79
|
|
|
$ctype = \FFI::typeof($element); |
|
80
|
|
|
return new self( |
|
81
|
|
|
$type, |
|
82
|
|
|
$addr->cdata, |
|
83
|
|
|
\FFI::sizeof($ctype), |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|