Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php	(revision 6668)
+++ wp-includes/functions.php	(working copy)
@@ -1676,4 +1676,67 @@
 			trigger_error( printf( __("%s is <strong>deprecated</strong> since version %s with no alternative available."), $file, $version ) );
 	}
 }
+
+/**
+ * _set_memory_limit() - Try to set PHP memory limit to amount
+ *
+ * If the amount is not given, then the default is to set the amount
+ * to '32MB', however the parameter available will allow setting the
+ * amount to something other than '32MB'.
+ *
+ * Failure might mean that the memory limit is already higher than
+ * either the default or the amount given. It could mean that ini_set
+ * and/or ini_get has been disabled and either getting and/or setting
+ * could not be accomplished.
+ *
+ * @since 2.5
+ * @access private
+ *
+ * @param string $mib MiB amount as a string with MiB amount followed by 'M'. Default is 32M.
+ * @return bool True if the amount was set by this function or already 32MB, false on failure.
+ */
+function _set_memory_limit($mib='32M') {
+	if( !function_exists('memory_get_usage') )
+		return null;
+
+	if( (int) @ini_get('memory_limit') < absint($mib) )
+		@ini_set('memory_limit', $mib);
+
+	if( @ini_get('memory_limit') == $mib )
+		return true;
+
+	return false;
+}
+
+/**
+ * wp_get_memory_limit() - Return the PHP memory limit in MiB
+ *
+ * If null is returned, then --enable-memory-limit directive was
+ * not enabled during PHP install (< 5.2.1). Prior to PHP 5.2.1
+ * if --enable-memory-limit was not used during the install of
+ * PHP, then memory_get_usage() would not exist in PHP.
+ *
+ * So Failure (false return value) might also mean that
+ * memory_limit is unavailable on PHP version greater than PHP
+ * 5.2.1.
+ *
+ * @todo Complete edge cases which have the memory limit set to something
+ *		other than 'M'. The Memory limit can be an integer
+ *
+ * @since 2.5
+ *
+ * @return bool|int False if memory limit is not already in MiB or the MiB amount
+ */
+function wp_get_memory_limit() {
+	if( !function_exists('memory_get_usage') )
+		return null;
+
+	$iMemoryLimit = @ini_get('memory_limit');
+
+	if( false !== stripos($iMemoryLimit, 'm') )
+		return absint($iMemoryLimit);
+	
+	return false;
+}
+
 ?>
Index: wp-includes/pluggable.php
===================================================================
--- wp-includes/pluggable.php	(revision 6668)
+++ wp-includes/pluggable.php	(working copy)
@@ -1260,4 +1260,39 @@
 }
 endif;
 
+if ( !function_exists('wp_increase_memory_limit') ) :
+/**
+ * wp_increase_memory_limit() - Increase PHP memory limit when needed
+ *
+ * This function can be applied to memory intensive loops to slowly increase
+ * the memory when needed instead of just failing.
+ *
+ * Null return value means that the function to get the current memory
+ * usage is unavailable. False return value can mean that the increase is
+ * not needed at the moment or that the memory limit could not be increased.
+ *
+ * @todo Return WP_Error instead of false or instead apply this todo to
+ *		the wp_set_memory_limit() function instead to return a WP_Error.
+ * @since 2.5
+ *
+ * @return null|bool True on success. Null if can't increase memory
+ */
+function wp_increase_memory_limit() {
+	if( !function_exists('memory_get_usage') )
+		return null;
+
+	$iMemoryLimit = (int) @ini_get('memory_limit');
+	$iWhenIncrease = (int) $iMemoryLimit * 0.75;
+
+	$iCurrentUsage = memory_get_usage() / 1048576; // Divide by Megabytes
+
+	if( $iCurrentUsage >= $iWhenIncrease ) {
+		$iIncreaseToAmount = ($iMemoryLimit+$iWhenIncrease).'MB';
+		return _set_memory_limit( $iIncreaseToAmount );
+	}
+
+	return false;
+}
+endif;
+
 ?>
Index: wp-settings.php
===================================================================
--- wp-settings.php	(revision 6668)
+++ wp-settings.php	(working copy)
@@ -194,6 +194,11 @@
 require (ABSPATH . WPINC . '/functions.php');
 require (ABSPATH . WPINC . '/classes.php');
 
+if( defined('WP_MEMORY_LIMIT') )
+	_set_memory_limit( WP_MEMORY_LIMIT );
+else
+	_set_memory_limit();
+
 require_wp_db();
 
 if ( !empty($wpdb->error) )
