Passed
Pull Request — 0.6.x (#205)
by
unknown
02:28 queued 36s
created

ProcessModuleMemoryMap::getInodeNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof 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 Reli\Lib\Process\MemoryMap;
15
16
final class ProcessModuleMemoryMap implements ProcessModuleMemoryMapInterface
17
{
18
    private ?int $base_address = null;
19
    /** @var array<int,int>|null  */
20
    private ?array $sorted_offset_to_memory_map = null;
21
22
    /** @param ProcessMemoryArea[] $memory_areas */
23
    public function __construct(
24
        private array $memory_areas
25
    ) {
26
    }
27
28
    public function getBaseAddress(): int
29
    {
30
        if (!isset($this->base_address)) {
31
            $base_address = PHP_INT_MAX;
32
            foreach ($this->memory_areas as $memory_area) {
33
                $base_address = min($base_address, hexdec($memory_area->begin));
34
            }
35
            $this->base_address = $base_address;
36
        }
37
        return $this->base_address;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->base_address could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
40
    public function getMemoryAddressFromOffset(int $offset): int
41
    {
42
        $ranges = $this->getSortedOffsetToMemoryAreaMap();
43
        $file_offset_decided = 0;
44
        foreach ($ranges as $file_offset => $_memory_begin) {
45
            if ($file_offset <= $offset) {
46
                $file_offset_decided = $file_offset;
47
            }
48
        }
49
        return $ranges[$file_offset_decided] + ($offset - $file_offset_decided);
50
    }
51
52
    public function isInRange(int $address): bool
53
    {
54
        foreach ($this->memory_areas as $memory_area) {
55
            if ($memory_area->isInRange($address)) {
56
                return true;
57
            }
58
        }
59
        return false;
60
    }
61
62
    /** @return array<int, int> */
63
    private function getSortedOffsetToMemoryAreaMap(): array
64
    {
65
        if (!isset($this->sorted_offset_to_memory_map)) {
66
            $ranges = [];
67
            foreach ($this->memory_areas as $memory_area) {
68
                $ranges[hexdec($memory_area->file_offset)] = hexdec($memory_area->begin);
69
            }
70
            ksort($ranges);
71
            $this->sorted_offset_to_memory_map = $ranges;
72
        }
73
        return $this->sorted_offset_to_memory_map;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sorted_offset_to_memory_map could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    public function getDeviceId(): string
77
    {
78
        return $this->memory_areas[0]->device_id;
79
    }
80
81
    public function getInodeNumber(): int
82
    {
83
        return $this->memory_areas[0]->inode_num;
84
    }
85
86
    public function getModuleName(): string
87
    {
88
        return $this->memory_areas[0]->name;
89
    }
90
}
91