| | 1679 | |
| | 1680 | /** |
| | 1681 | * _set_memory_limit() - Try to set PHP memory limit to amount |
| | 1682 | * |
| | 1683 | * If the amount is not given, then the default is to set the amount |
| | 1684 | * to '32MB', however the parameter available will allow setting the |
| | 1685 | * amount to something other than '32MB'. |
| | 1686 | * |
| | 1687 | * Failure might mean that the memory limit is already higher than |
| | 1688 | * either the default or the amount given. It could mean that ini_set |
| | 1689 | * and/or ini_get has been disabled and either getting and/or setting |
| | 1690 | * could not be accomplished. |
| | 1691 | * |
| | 1692 | * @since 2.5 |
| | 1693 | * @access private |
| | 1694 | * |
| | 1695 | * @param string $mib MiB amount as a string with MiB amount followed by 'M'. Default is 32M. |
| | 1696 | * @return bool True if the amount was set by this function or already 32MB, false on failure. |
| | 1697 | */ |
| | 1698 | function _set_memory_limit($mib='32M') { |
| | 1699 | if( !function_exists('memory_get_usage') ) |
| | 1700 | return null; |
| | 1701 | |
| | 1702 | if( (int) @ini_get('memory_limit') < absint($mib) ) |
| | 1703 | @ini_set('memory_limit', $mib); |
| | 1704 | |
| | 1705 | if( @ini_get('memory_limit') == $mib ) |
| | 1706 | return true; |
| | 1707 | |
| | 1708 | return false; |
| | 1709 | } |
| | 1710 | |
| | 1711 | /** |
| | 1712 | * wp_increase_memory_limit() - Increase PHP memory limit when needed |
| | 1713 | * |
| | 1714 | * This function can be applied to memory intensive loops to slowly increase |
| | 1715 | * the memory when needed instead of just failing. |
| | 1716 | * |
| | 1717 | * Null return value means that the function to get the current memory |
| | 1718 | * usage is unavailable. False return value can mean that the increase is |
| | 1719 | * not needed at the moment or that the memory limit could not be increased. |
| | 1720 | * |
| | 1721 | * @todo Return WP_Error instead of false or instead apply this todo to |
| | 1722 | * the wp_set_memory_limit() function instead to return a WP_Error. |
| | 1723 | * @since 2.5 |
| | 1724 | * |
| | 1725 | * @return null|bool True on success. Null if can't increase memory |
| | 1726 | */ |
| | 1727 | function wp_increase_memory_limit() { |
| | 1728 | if( !function_exists('memory_get_usage') ) |
| | 1729 | return null; |
| | 1730 | |
| | 1731 | $iMemoryLimit = (int) @ini_get('memory_limit'); |
| | 1732 | $iWhenIncrease = (int) $iMemoryLimit * 0.75; |
| | 1733 | |
| | 1734 | $iCurrentUsage = memory_get_usage() / 1048576; // Divide by Megabytes |
| | 1735 | |
| | 1736 | if( $iCurrentUsage >= $iWhenIncrease ) { |
| | 1737 | $iIncreaseToAmount = ($iMemoryLimit+$iWhenIncrease).'MB'; |
| | 1738 | return _set_memory_limit( $iIncreaseToAmount ); |
| | 1739 | } |
| | 1740 | |
| | 1741 | return false; |
| | 1742 | } |
| | 1743 | |
| | 1744 | /** |
| | 1745 | * wp_get_memory_limit() - Return the PHP memory limit in MiB |
| | 1746 | * |
| | 1747 | * If null is returned, then --enable-memory-limit directive was |
| | 1748 | * not enabled during PHP install (< 5.2.1). Prior to PHP 5.2.1 |
| | 1749 | * if --enable-memory-limit was not used during the install of |
| | 1750 | * PHP, then memory_get_usage() would not exist in PHP. |
| | 1751 | * |
| | 1752 | * So Failure (false return value) might also mean that |
| | 1753 | * memory_limit is unavailable on PHP version greater than PHP |
| | 1754 | * 5.2.1. |
| | 1755 | * |
| | 1756 | * @todo Complete edge cases which have the memory limit set to something |
| | 1757 | * other than 'M'. The Memory limit can be an integer |
| | 1758 | * |
| | 1759 | * @since 2.5 |
| | 1760 | * |
| | 1761 | * @return bool|int False if memory limit is not already in MiB or the MiB amount |
| | 1762 | */ |
| | 1763 | function wp_get_memory_limit() { |
| | 1764 | if( !function_exists('memory_get_usage') ) |
| | 1765 | return null; |
| | 1766 | |
| | 1767 | $iMemoryLimit = @ini_get('memory_limit'); |
| | 1768 | |
| | 1769 | if( false !== stripos($iMemoryLimit, 'm') ) |
| | 1770 | return absint($iMemoryLimit); |
| | 1771 | |
| | 1772 | return false; |
| | 1773 | } |
| | 1774 | |