PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/memory.php

https://github.com/orchestra-io/sample-openx
PHP | 128 lines | 55 code | 7 blank | 66 comment | 14 complexity | af8ef6c5faea284ea96fabbf9c0c486e MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v${RELEASE_MAJOR_MINOR} |
  5. | =======${RELEASE_MAJOR_MINOR_DOUBLE_UNDERLINE} |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id: memory.php 34714 2009-04-02 05:57:05Z matthieu.aubry $
  25. */
  26. /**
  27. * @package OpenX
  28. * @author Andrew Hill <andrew.hill@openx.org>
  29. *
  30. * A file of memory-related functions, that are used as part of the UI system's
  31. * pre-initialisation "pre-check.php" file, and also as part of the delivery
  32. * engine, maintenance engine, etc.
  33. */
  34. /**
  35. * Returns the minimum required amount of memory required for OpenX operation.
  36. *
  37. * @param strting $limit An optional limitation level, to have a memory limit OTHER than
  38. * the default UI/delivery engine memory limit returned. If used,
  39. * one of:
  40. * - "cache" - The limit used for generating XML caches
  41. * - "plugin" - The limit used for installing pluings
  42. * - "maintenance" - The limit used for the maintenance engine
  43. * @return integer The required minimum amount of memory (in bytes).
  44. */
  45. function OX_getMinimumRequiredMemory($limit = null)
  46. {
  47. if ($limit == 'maintenance') {
  48. return 134217728; // 128MB in bytes (128 * 1048576)
  49. }
  50. return 134217728; // 128MB in bytes (128 * 1048576)
  51. }
  52. /**
  53. * Get the PHP memory_limit value in bytes.
  54. *
  55. * @return integer The memory_limit value set in PHP, in bytes
  56. * (or -1, if no limit).
  57. */
  58. function OX_getMemoryLimitSizeInBytes() {
  59. $phpMemoryLimit = ini_get('memory_limit');
  60. if (empty($phpMemoryLimit) || $phpMemoryLimit == -1) {
  61. // No memory limit
  62. return -1;
  63. }
  64. $aSize = array(
  65. 'G' => 1073741824,
  66. 'M' => 1048576,
  67. 'K' => 1024
  68. );
  69. $phpMemoryLimitInBytes = $phpMemoryLimit;
  70. foreach($aSize as $type => $multiplier) {
  71. $pos = strpos($phpMemoryLimit, $type);
  72. if (!$pos) {
  73. $pos = strpos($phpMemoryLimit, strtolower($type));
  74. }
  75. if ($pos) {
  76. $phpMemoryLimitInBytes = substr($phpMemoryLimit, 0, $pos) * $multiplier;
  77. }
  78. }
  79. return $phpMemoryLimitInBytes;
  80. }
  81. /**
  82. * Test if the memory_limit can be changed.
  83. *
  84. * @return boolean True if the memory_limit can be changed, false otherwise.
  85. */
  86. function OX_checkMemoryCanBeSet()
  87. {
  88. $phpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
  89. // Unlimited memory, no need to check if it can be set
  90. if ($phpMemoryLimitInBytes == -1) {
  91. return true;
  92. }
  93. OX_increaseMemoryLimit($phpMemoryLimitInBytes + 1);
  94. $newPhpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
  95. $memoryCanBeSet = ($phpMemoryLimitInBytes != $newPhpMemoryLimitInBytes);
  96. // Restore previous limit
  97. @ini_set('memory_limit', $phpMemoryLimitInBytes);
  98. return $memoryCanBeSet;
  99. }
  100. /**
  101. * Increase the PHP memory_limit value to the supplied size, if required.
  102. *
  103. * @param integer $setMemory The memory_limit that should be set (in bytes).
  104. * @return boolean True if the memory_limit was already greater than the value
  105. * supplied, or if the attempt to set a larger memory_limit was
  106. * successful; false otherwise.
  107. */
  108. function OX_increaseMemoryLimit($setMemory) {
  109. $phpMemoryLimitInBytes = OX_getMemoryLimitSizeInBytes();
  110. if ($phpMemoryLimitInBytes == -1) {
  111. // Memory is unlimited
  112. return true;
  113. }
  114. if ($setMemory > $phpMemoryLimitInBytes) {
  115. if (@ini_set('memory_limit', $setMemory) === false) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. ?>