forked from danmorton/madserve_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_f.php
80 lines (75 loc) · 2.33 KB
/
mem_f.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
77
78
79
80
<?php
function MAD_getMinimumRequiredMemory($limit = null)
{
return 134217728; // 128MB in bytes (128 * 1048576)
}
/**
* Get the PHP memory_limit value in bytes.
*
* @return integer The memory_limit value set in PHP, in bytes
* (or -1, if no limit).
*/
function MAD_getMemoryLimitSizeInBytes() {
$phpMemoryLimit = ini_get('memory_limit');
if (empty($phpMemoryLimit) || $phpMemoryLimit == -1) {
// No memory limit
return -1;
}
$aSize = array(
'G' => 1073741824,
'M' => 1048576,
'K' => 1024
);
$phpMemoryLimitInBytes = $phpMemoryLimit;
foreach($aSize as $type => $multiplier) {
$pos = strpos($phpMemoryLimit, $type);
if (!$pos) {
$pos = strpos($phpMemoryLimit, strtolower($type));
}
if ($pos) {
$phpMemoryLimitInBytes = substr($phpMemoryLimit, 0, $pos) * $multiplier;
}
}
return $phpMemoryLimitInBytes;
}
/**
* Test if the memory_limit can be changed.
*
* @return boolean True if the memory_limit can be changed, false otherwise.
*/
function MAD_checkMemoryCanBeSet()
{
$phpMemoryLimitInBytes = MAD_getMemoryLimitSizeInBytes();
// Unlimited memory, no need to check if it can be set
if ($phpMemoryLimitInBytes == -1) {
return true;
}
MAD_increaseMemoryLimit($phpMemoryLimitInBytes + 1);
$newPhpMemoryLimitInBytes = MAD_getMemoryLimitSizeInBytes();
$memoryCanBeSet = ($phpMemoryLimitInBytes != $newPhpMemoryLimitInBytes);
// Restore previous limit
@ini_set('memory_limit', $phpMemoryLimitInBytes);
return $memoryCanBeSet;
}
/**
* Increase the PHP memory_limit value to the supplied size, if required.
*
* @param integer $setMemory The memory_limit that should be set (in bytes).
* @return boolean True if the memory_limit was already greater than the value
* supplied, or if the attempt to set a larger memory_limit was
* successful; false otherwise.
*/
function MAD_increaseMemoryLimit($setMemory) {
$phpMemoryLimitInBytes = MAD_getMemoryLimitSizeInBytes();
if ($phpMemoryLimitInBytes == -1) {
// Memory is unlimited
return true;
}
if ($setMemory > $phpMemoryLimitInBytes) {
if (@ini_set('memory_limit', $setMemory) === false) {
return false;
}
}
return true;
}
?>