-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmemory.php
128 lines (121 loc) · 4.97 KB
/
memory.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
+---------------------------------------------------------------------------+
| OpenX v${RELEASE_MAJOR_MINOR} |
| =======${RELEASE_MAJOR_MINOR_DOUBLE_UNDERLINE} |
| |
| Copyright (c) 2003-2009 OpenX Limited |
| For contact details, see: http://www.openx.org/ |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation; either version 2 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
+---------------------------------------------------------------------------+
$Id$
*/
/**
* @package OpenX
* @author Andrew Hill <[email protected]>
*
* A file of memory-related functions, that are used as part of the UI system's
* pre-initialisation "pre-check.php" file, and also as part of the delivery
* engine, maintenance engine, etc.
*/
/**
* Returns the minimum required amount of memory required for OpenX operation.
*
* @param strting $limit An optional limitation level, to have a memory limit OTHER than
* the default UI/delivery engine memory limit returned. If used,
* one of:
* - "cache" - The limit used for generating XML caches
* - "plugin" - The limit used for installing pluings
* - "maintenance" - The limit used for the maintenance engine
* @return integer The required minimum amount of memory (in bytes).
*/
function OX_getMinimumRequiredMemory($limit = null)
{
if ($limit == 'maintenance') {
return 134217728; // 128MB in bytes (128 * 1048576)
}
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 OX_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 OX_checkMemoryCanBeSet()
{
$phpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
// Unlimited memory, no need to check if it can be set
if ($phpMemoryLimitInBytes == -1) {
return true;
}
OX_increaseMemoryLimit($phpMemoryLimitInBytes + 1);
$newPhpMemoryLimitInBytes = OX_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 OX_increaseMemoryLimit($setMemory) {
$phpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
if ($phpMemoryLimitInBytes == -1) {
// Memory is unlimited
return true;
}
if ($setMemory > $phpMemoryLimitInBytes) {
if (@ini_set('memory_limit', $setMemory) === false) {
return false;
}
}
return true;
}
?>