USB: Rework the PhysicalMemoryAllocator to use MutexLocker.

No functional change, but will make some subsequent
changes easier and more fail-proof.
This commit is contained in:
Augustin Cavalier 2019-12-23 15:29:43 -05:00
parent 006add310a
commit a77769a549
2 changed files with 8 additions and 24 deletions

View File

@ -10,6 +10,7 @@
#include <string.h>
#include <KernelExport.h>
#include <SupportDefs.h>
#include <util/AutoLock.h>
#include <util/kernel_cpp.h>
#include "PhysicalMemoryAllocator.h"
@ -97,7 +98,7 @@ PhysicalMemoryAllocator::PhysicalMemoryAllocator(const char *name,
PhysicalMemoryAllocator::~PhysicalMemoryAllocator()
{
_Lock();
mutex_lock(&fLock);
for (int32 i = 0; i < fArrayCount; i++)
free(fArray[i]);
@ -113,20 +114,6 @@ PhysicalMemoryAllocator::~PhysicalMemoryAllocator()
}
bool
PhysicalMemoryAllocator::_Lock()
{
return (mutex_lock(&fLock) == B_OK);
}
void
PhysicalMemoryAllocator::_Unlock()
{
mutex_unlock(&fLock);
}
status_t
PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
phys_addr_t *physicalAddress)
@ -169,7 +156,8 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
}
}
if (!_Lock())
MutexLocker locker(&fLock);
if (!locker.IsLocked())
return B_ERROR;
while (true) {
@ -203,7 +191,6 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
arrayIndex >>= 1;
}
_Unlock();
size_t offset = fBlockSize[arrayToUse] * i;
*logicalAddress = (void *)((uint8 *)fLogicalBase + offset);
*physicalAddress = (phys_addr_t)(fPhysicalBase + offset);
@ -217,7 +204,7 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
fNoMemoryCondition.Add(&entry);
fMemoryWaitersCount++;
_Unlock();
locker.Unlock();
TRACE_ERROR(("PMA: found no free slot to store %ld bytes, waiting\n",
size));
@ -227,7 +214,7 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
break;
}
if (!_Lock())
if (!locker.Lock())
return B_ERROR;
fMemoryWaitersCount--;
@ -283,7 +270,8 @@ PhysicalMemoryAllocator::Deallocate(size_t size, void *logicalAddress,
return B_BAD_VALUE;
}
if (!_Lock())
MutexLocker _(&fLock);
if (!_.IsLocked())
return B_ERROR;
// clear upwards to the smallest block
@ -308,7 +296,6 @@ PhysicalMemoryAllocator::Deallocate(size_t size, void *logicalAddress,
if (fMemoryWaitersCount > 0)
fNoMemoryCondition.NotifyAll();
_Unlock();
return B_OK;
}

View File

@ -39,9 +39,6 @@ public:
void DumpFreeSlots();
private:
bool _Lock();
void _Unlock();
char *fName;
size_t fOverhead;