-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemManager.php
More file actions
101 lines (86 loc) · 1.96 KB
/
Copy pathMemManager.php
File metadata and controls
101 lines (86 loc) · 1.96 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
class MemoryManager{
private $buffer = '';
private $size = 0;
private $free = 0;
private $index = -1;
private $alloc = Array();
function __construct($bytes)
{
$this->buffer = str_repeat(" ", $bytes);
$this->size = strlen($this->buffer);
$this->free = strlen($this->buffer);
}
function Alloc($size)
{
if($size <= $this->free) {
array_push($this->alloc, $size);
$this->index++;
$item = $this->alloc[$this->index];
$this->free -= $size;
return $this->index;
} else {
return false;
}
}
function Position($index)
{
if($index <= $this->index)
{
$result = 0;
for($i=0; $i<$index; $i++) {
$result += $this->alloc[$i];
}
return $result;
} else {
return false;
}
}
function Store($index, $data)
{
foreach (str_split($data) as $key => $value) {
$this->buffer[$this->Position($index) + $key] = $value;
}
}
function Free($index)
{
$pos = $this->Position($index);
$space = $this->alloc[$index];
$new_buffer = substr($this->buffer, 0, $pos).substr($this->buffer, $pos+$space).str_repeat(' ', $space);
$this->alloc[$index] = 0;
$this->buffer = $new_buffer;
$this->free += $space;
}
function ShowIndex($index)
{
$pos = $this->Position($index);
$space = $this->alloc[$index];
if($space>0) {
print_r(substr($this->buffer, $pos, $space)."|\n");
}
}
function ShowIndexes()
{
for($i=0; $i<=$this->index; $i++)
{
$this->ShowIndex($i);
}
}
function ShowDiagnostic()
{
print_r($this->buffer."\n");
print_r($this->size."\n");
print_r($this->free."\n");
}
}
$memory = new MemoryManager(100);
$n0 = $memory->Alloc(strlen('Yonsy'));
$memory->Store($n0, 'Yonsy');
$n1 = $memory->Alloc(10);
$memory->Store($n1, 'Solis');
$n2 = $memory->Alloc(10);
$memory->Store($n2, 'Ponce');
$memory->ShowIndexes();
$memory->Free($n1);
$memory->ShowIndexes();
?>