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:
parent
006add310a
commit
a77769a549
@ -10,6 +10,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <KernelExport.h>
|
#include <KernelExport.h>
|
||||||
#include <SupportDefs.h>
|
#include <SupportDefs.h>
|
||||||
|
#include <util/AutoLock.h>
|
||||||
#include <util/kernel_cpp.h>
|
#include <util/kernel_cpp.h>
|
||||||
|
|
||||||
#include "PhysicalMemoryAllocator.h"
|
#include "PhysicalMemoryAllocator.h"
|
||||||
@ -97,7 +98,7 @@ PhysicalMemoryAllocator::PhysicalMemoryAllocator(const char *name,
|
|||||||
|
|
||||||
PhysicalMemoryAllocator::~PhysicalMemoryAllocator()
|
PhysicalMemoryAllocator::~PhysicalMemoryAllocator()
|
||||||
{
|
{
|
||||||
_Lock();
|
mutex_lock(&fLock);
|
||||||
|
|
||||||
for (int32 i = 0; i < fArrayCount; i++)
|
for (int32 i = 0; i < fArrayCount; i++)
|
||||||
free(fArray[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
|
status_t
|
||||||
PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
|
PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
|
||||||
phys_addr_t *physicalAddress)
|
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;
|
return B_ERROR;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -203,7 +191,6 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
|
|||||||
arrayIndex >>= 1;
|
arrayIndex >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_Unlock();
|
|
||||||
size_t offset = fBlockSize[arrayToUse] * i;
|
size_t offset = fBlockSize[arrayToUse] * i;
|
||||||
*logicalAddress = (void *)((uint8 *)fLogicalBase + offset);
|
*logicalAddress = (void *)((uint8 *)fLogicalBase + offset);
|
||||||
*physicalAddress = (phys_addr_t)(fPhysicalBase + offset);
|
*physicalAddress = (phys_addr_t)(fPhysicalBase + offset);
|
||||||
@ -217,7 +204,7 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
|
|||||||
fNoMemoryCondition.Add(&entry);
|
fNoMemoryCondition.Add(&entry);
|
||||||
fMemoryWaitersCount++;
|
fMemoryWaitersCount++;
|
||||||
|
|
||||||
_Unlock();
|
locker.Unlock();
|
||||||
|
|
||||||
TRACE_ERROR(("PMA: found no free slot to store %ld bytes, waiting\n",
|
TRACE_ERROR(("PMA: found no free slot to store %ld bytes, waiting\n",
|
||||||
size));
|
size));
|
||||||
@ -227,7 +214,7 @@ PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_Lock())
|
if (!locker.Lock())
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
|
|
||||||
fMemoryWaitersCount--;
|
fMemoryWaitersCount--;
|
||||||
@ -283,7 +270,8 @@ PhysicalMemoryAllocator::Deallocate(size_t size, void *logicalAddress,
|
|||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_Lock())
|
MutexLocker _(&fLock);
|
||||||
|
if (!_.IsLocked())
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
|
|
||||||
// clear upwards to the smallest block
|
// clear upwards to the smallest block
|
||||||
@ -308,7 +296,6 @@ PhysicalMemoryAllocator::Deallocate(size_t size, void *logicalAddress,
|
|||||||
if (fMemoryWaitersCount > 0)
|
if (fMemoryWaitersCount > 0)
|
||||||
fNoMemoryCondition.NotifyAll();
|
fNoMemoryCondition.NotifyAll();
|
||||||
|
|
||||||
_Unlock();
|
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,9 +39,6 @@ public:
|
|||||||
void DumpFreeSlots();
|
void DumpFreeSlots();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _Lock();
|
|
||||||
void _Unlock();
|
|
||||||
|
|
||||||
char *fName;
|
char *fName;
|
||||||
|
|
||||||
size_t fOverhead;
|
size_t fOverhead;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user