Previously, the rbtree comparator converted the difference between two
addresses to intptr_t. On 32-bit systems, addresses that differ by at
least 2 GiB could produce an incorrect ordering and prevent a later block
lookup from finding its cluster.
The fix is to compare the addresses directly and return -1, 0, or 1.
This fixes a pool that remained non-empty after all allocations were freed
in the x86 memory pool test.
static intptr_t
njs_mp_rbtree_compare(njs_rbtree_node_t *node1, njs_rbtree_node_t *node2)
{
+ uintptr_t start1, start2;
njs_mp_block_t *block1, *block2;
block1 = (njs_mp_block_t *) node1;
block2 = (njs_mp_block_t *) node2;
- return (uintptr_t) block1->start - (uintptr_t) block2->start;
+ start1 = (uintptr_t) block1->start;
+ start2 = (uintptr_t) block2->start;
+
+ return (start1 > start2) - (start1 < start2);
}