Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php	(revision 6528)
+++ wp-includes/functions.php	(working copy)
@@ -1674,4 +1674,83 @@
 			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.4
+ * @access private
+ *
+ * @param string $mib MiB amount as a string with MiB followed by 'MB'. Default is 32MB.
+ * @return bool True if the amount was set by this function or already 32MB, false on failure.
+ */
+function _set_memory_limit($mib='32MB') {
+	if( (int) @ini_get('memory_limit') < absint($mib) )
+		@ini_set('memory_limit', $mib);
+
+	if( @ini_get('memory_limit') == $mib )
+		return true;
+
+	return false;
+}
+
+/**
+ * _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 _set_memory_limit() function instead to return a WP_Error.
+ * @since 2.4
+ *
+ * @return null|bool True on success
+ */
+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;
+}
+
+/**
+ * wp_get_memory_limit() - Return the PHP memory limit in MiB
+ *
+ * @todo Complete edge cases which have the memory limit set to something
+ *		other than 'MB'
+ *
+ * @return bool|int False if memory limit is not already in MiB or the MiB amount
+ */
+function wp_get_memory_limit() {
+	$iMemoryLimit = @ini_get('memory_limit');
+
+	if( false !== stripos($iMemoryLimit, 'm') )
+		return absint($iMemoryLimit);
+	
+	return false;
+}
+
 ?>
Index: wp-settings.php
===================================================================
--- wp-settings.php	(revision 6528)
+++ wp-settings.php	(working copy)
@@ -195,6 +195,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) )
