[php-src] Issue #7944: api to set memory_limit to "at least" this much
| From: | divinity76 | Date: | Fri, 14 Jan 2022 23:15:36 +0000 |
| Subject: | [php-src] Issue #7944: api to set memory_limit to "at least" this much | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239018@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7944
Author: divinity76
### Description
it's not rare that i see code like
ini_set('memory_limit',
'1G'); , where the intention is not really to set the memory limit to 1G, but to
make sure that the limit is *at least* 1G, if some other code in the same script requires 4G, i
don't want to overwrite that.. would be nice if there was some api kindof like
```php
function memory_limit_set_at_least(int $at_least): void
{
$current = ini_get("memory_limit");
if ($current === "-1") {
// it's already unlimited.
return;
}
$modifier = $current[strlen($current) - 1];
$current = (int) $current;
if ($modifier === "K" || $modifier === "k") {
$current *= 1024;
} elseif ($modifier === "M" || $modifier === "m") {
$current *= 1024 * 1024;
} elseif ($modifier === "G" || $modifier === "g") {
$current *= 1024 * 1024 * 1024;
} else {
// there is no modifier.
}
if ($current < $at_least) {
ini_set("memory_limit", (string) $at_least);
}
}
```