kernel/vm: Use 64-bit signed types explicitly for memory computations.

off_t is a signed 64-bit type, but size_t isn't signed or 64-bit
on 32-bit platforms. That meant adding a sign bit caused strange
things to happen there.

Fixes incorrect free-memory computations on 32-bit.
(cherry picked from commit cc565c81afd2dfba34de6ced607199d757080a8d)

Change-Id: I294f92ac1279a6311347b5e25341542d8c862013
Reviewed-on: https://review.haiku-os.org/c/haiku/+/11326
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Augustin Cavalier
2026-07-22 17:37:42 +00:00
committed by waddlesplash
parent 0178ddbc88
commit 9d80b76b46
+7 -7
View File
@@ -250,11 +250,11 @@ static uint32 sPageMappingsMask;
static rw_lock sAreaCacheLock = RW_LOCK_INITIALIZER("area->cache");
static rw_spinlock sAvailableMemoryLock = B_RW_SPINLOCK_INITIALIZER;
static off_t sAvailableMemory;
static int64 sAvailableMemory;
#if ENABLE_SWAP_SUPPORT
static off_t sAvailableMemoryAndSwap;
static int64 sAvailableMemoryAndSwap;
#endif
static off_t sNeededMemory;
static int64 sNeededMemory;
static uint32 sPageFaults;
static VMPhysicalPageMapper* sPhysicalPageMapper;
@@ -4432,20 +4432,20 @@ vm_unreserve_memory_or_swap(size_t amount)
static status_t
vm_try_reserve_internal(off_t& pool, uint32 resource,
size_t amount, int priority, bigtime_t absoluteTimeout)
vm_try_reserve_internal(int64& pool, uint32 resource,
int64 amount, int priority, bigtime_t absoluteTimeout)
{
ASSERT((amount % B_PAGE_SIZE) == 0);
ASSERT(priority >= 0 && priority < (int)B_COUNT_OF(kMemoryReserveForPriority));
TRACE(("try to reserve %lu bytes, %Lu left\n", amount, pool));
const size_t reserve = kMemoryReserveForPriority[priority];
const off_t amountPlusReserve = amount + reserve;
const int64 amountPlusReserve = amount + reserve;
// Try with a read-lock and atomics first, but only if there's more than double
// the amount of memory we're trying to reserve available, to avoid races.
InterruptsReadSpinLocker readLocker(sAvailableMemoryLock);
if (atomic_get64(&pool) > (off_t)(amountPlusReserve + amount)) {
if (atomic_get64(&pool) > (amountPlusReserve + amount)) {
if (atomic_add64(&pool, -amount) >= amountPlusReserve)
return B_OK;