shared: fix unit tests for nm_utils_get_next_realloc_size()

The change broke unit tests on 32 bit systems.

Change the code again to make it more similar to what it was
before. Now only on 64 bit systems there is any difference compared
to before. That makes it easier about reasoning for how the unit test
should be (in most cases, it is unchanged).

Fixes: 040c86f15c ('shared: avoid compiler warning for nm_utils_get_next_realloc_size() returning huge sizes')
This commit is contained in:
Thomas Haller
2021-01-12 09:49:57 +01:00
parent 4686e9baef
commit 478d5bdafe
2 changed files with 12 additions and 8 deletions

View File

@@ -798,7 +798,9 @@ test_nm_utils_get_next_realloc_size(void)
}
/* reserved_false is generally the next power of two - 24. */
if (!truncated_false) {
if (reserved_false == G_MAXSIZE)
g_assert_cmpuint(requested, >, G_MAXSIZE / 2u - 24u);
else if (!reserved_false) {
g_assert_cmpuint(reserved_false, <=, G_MAXSIZE - 24u);
if (reserved_false >= 40) {
const gsize _pow2 = reserved_false + 24u;
@@ -816,7 +818,9 @@ test_nm_utils_get_next_realloc_size(void)
}
/* reserved_true is generally the next 4k border - 24. */
if (!truncated_true) {
if (reserved_true == G_MAXSIZE)
g_assert_cmpuint(requested, >, G_MAXSIZE - 0x1000u - 24u);
else if (!truncated_true) {
g_assert_cmpuint(reserved_true, <=, G_MAXSIZE - 24u);
if (reserved_true > 8168u) {
const gsize page_border = reserved_true + 24u;

View File

@@ -65,16 +65,16 @@ nm_utils_get_next_realloc_size(bool true_realloc, size_t requested)
return n - 24u;
}
/* For large allocations (with !true_realloc) we allocate memory in chunks of
* 4K (- 24 bytes extra), assuming that the memory gets mmapped and thus
* realloc() is efficient by just reordering pages. */
n = ((requested + (0x0FFFu + 24u)) & ~((size_t) 0x0FFFu)) - 24u;
if (NM_UNLIKELY(n < requested)) {
if (NM_UNLIKELY(requested > SIZE_MAX - 0x1000u - 24u)) {
/* overflow happened. */
goto out_huge;
}
/* For large allocations (with !true_realloc) we allocate memory in chunks of
* 4K (- 24 bytes extra), assuming that the memory gets mmapped and thus
* realloc() is efficient by just reordering pages. */
n = ((requested + (0x0FFFu + 24u)) & ~((size_t) 0x0FFFu)) - 24u;
nm_assert(n >= requested);
return n;
out_huge: