WordPress.org

Make WordPress Core

Ticket #3141: 3141.prototype.r6528.patch

File 3141.prototype.r6528.patch, 3.2 KB (added by darkdragon, 6 years ago)

Prototype of increasing memory limit API

  • wp-includes/functions.php

     
    16741674                        trigger_error( printf( __("%s is <strong>deprecated</strong since version %s with no alternative available."), $file, $version ) ); 
    16751675        } 
    16761676} 
     1677 
     1678/** 
     1679 * _set_memory_limit() - Try to set PHP memory limit to amount 
     1680 * 
     1681 * If the amount is not given, then the default is to set the amount 
     1682 * to '32MB', however the parameter available will allow setting the 
     1683 * amount to something other than '32MB'. 
     1684 * 
     1685 * Failure might mean that the memory limit is already higher than 
     1686 * either the default or the amount given. It could mean that ini_set 
     1687 * and/or ini_get has been disabled and either getting and/or setting 
     1688 * could not be accomplished. 
     1689 * 
     1690 * @since 2.4 
     1691 * @access private 
     1692 * 
     1693 * @param string $mib MiB amount as a string with MiB followed by 'MB'. Default is 32MB. 
     1694 * @return bool True if the amount was set by this function or already 32MB, false on failure. 
     1695 */ 
     1696function _set_memory_limit($mib='32MB') { 
     1697        if( (int) @ini_get('memory_limit') < absint($mib) ) 
     1698                @ini_set('memory_limit', $mib); 
     1699 
     1700        if( @ini_get('memory_limit') == $mib ) 
     1701                return true; 
     1702 
     1703        return false; 
     1704} 
     1705 
     1706/** 
     1707 * _increase_memory_limit() - Increase PHP memory limit when needed 
     1708 * 
     1709 * This function can be applied to memory intensive loops to slowly increase 
     1710 * the memory when needed instead of just failing. 
     1711 * 
     1712 * Null return value means that the function to get the current memory 
     1713 * usage is unavailable. False return value can mean that the increase is 
     1714 * not needed at the moment or that the memory limit could not be increased. 
     1715 * 
     1716 * @todo Return WP_Error instead of false or instead apply this todo to 
     1717 *              the _set_memory_limit() function instead to return a WP_Error. 
     1718 * @since 2.4 
     1719 * 
     1720 * @return null|bool True on success 
     1721 */ 
     1722function wp_increase_memory_limit() { 
     1723        if( !function_exists('memory_get_usage') ) 
     1724                return null; 
     1725 
     1726        $iMemoryLimit = (int) @ini_get('memory_limit'); 
     1727        $iWhenIncrease = (int) $iMemoryLimit * 0.75; 
     1728 
     1729        $iCurrentUsage = memory_get_usage() / 1048576; // Divide by Megabytes 
     1730 
     1731        if( $iCurrentUsage >= $iWhenIncrease ) { 
     1732                $iIncreaseToAmount = ($iMemoryLimit+$iWhenIncrease).'MB'; 
     1733                return _set_memory_limit( $iIncreaseToAmount ); 
     1734        } 
     1735 
     1736        return false; 
     1737} 
     1738 
     1739/** 
     1740 * wp_get_memory_limit() - Return the PHP memory limit in MiB 
     1741 * 
     1742 * @todo Complete edge cases which have the memory limit set to something 
     1743 *              other than 'MB' 
     1744 * 
     1745 * @return bool|int False if memory limit is not already in MiB or the MiB amount 
     1746 */ 
     1747function wp_get_memory_limit() { 
     1748        $iMemoryLimit = @ini_get('memory_limit'); 
     1749 
     1750        if( false !== stripos($iMemoryLimit, 'm') ) 
     1751                return absint($iMemoryLimit); 
     1752         
     1753        return false; 
     1754} 
     1755 
    16771756?> 
  • wp-settings.php

     
    195195require (ABSPATH . WPINC . '/functions.php'); 
    196196require (ABSPATH . WPINC . '/classes.php'); 
    197197 
     198if( defined('WP_MEMORY_LIMIT') ) 
     199        _set_memory_limit( WP_MEMORY_LIMIT ); 
     200else 
     201        _set_memory_limit(); 
     202 
    198203require_wp_db(); 
    199204 
    200205if ( !empty($wpdb->error) )