1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Allow mem region visitors to abort the scan by returning false

This commit is contained in:
Achilleas Anagnostopoulos 2017-05-10 22:24:03 +01:00
parent 61314a9c33
commit e1ada1ac8a
2 changed files with 21 additions and 5 deletions

View File

@ -119,8 +119,9 @@ var (
) )
// MemRegionVisitor defies a visitor function that gets invoked by VisitMemRegions // MemRegionVisitor defies a visitor function that gets invoked by VisitMemRegions
// for each memory region provided by the boot loader. // for each memory region provided by the boot loader. The visitor must return true
type MemRegionVisitor func(entry *MemoryMapEntry) // to continue or false to abort the scan.
type MemRegionVisitor func(entry *MemoryMapEntry) bool
// SetInfoPtr updates the internal multiboot information pointer to the given // SetInfoPtr updates the internal multiboot information pointer to the given
// value. This function must be invoked before invoking any other function // value. This function must be invoked before invoking any other function
@ -151,7 +152,9 @@ func VisitMemRegions(visitor MemRegionVisitor) {
entry.Type = MemReserved entry.Type = MemReserved
} }
visitor(entry) if !visitor(entry) {
return
}
curPtr += uintptr(ptrMapHeader.entrySize) curPtr += uintptr(ptrMapHeader.entrySize)
} }

View File

@ -59,8 +59,9 @@ func TestVisitMemRegion(t *testing.T) {
var visitCount int var visitCount int
SetInfoPtr(uintptr(unsafe.Pointer(&emptyInfoData[0]))) SetInfoPtr(uintptr(unsafe.Pointer(&emptyInfoData[0])))
VisitMemRegions(func(_ *MemoryMapEntry) { VisitMemRegions(func(_ *MemoryMapEntry) bool {
visitCount++ visitCount++
return true
}) })
if visitCount != 0 { if visitCount != 0 {
@ -71,7 +72,7 @@ func TestVisitMemRegion(t *testing.T) {
SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0]))) SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0])))
multibootInfoTestData[128] = 0xFF multibootInfoTestData[128] = 0xFF
VisitMemRegions(func(entry *MemoryMapEntry) { VisitMemRegions(func(entry *MemoryMapEntry) bool {
if entry.PhysAddress != specs[visitCount].expPhys { if entry.PhysAddress != specs[visitCount].expPhys {
t.Errorf("[visit %d] expected physical address to be %x; got %x", visitCount, specs[visitCount].expPhys, entry.PhysAddress) t.Errorf("[visit %d] expected physical address to be %x; got %x", visitCount, specs[visitCount].expPhys, entry.PhysAddress)
} }
@ -82,11 +83,23 @@ func TestVisitMemRegion(t *testing.T) {
t.Errorf("[visit %d] expected region type to be %d; got %d", visitCount, specs[visitCount].expType, entry.Type) t.Errorf("[visit %d] expected region type to be %d; got %d", visitCount, specs[visitCount].expType, entry.Type)
} }
visitCount++ visitCount++
return true
}) })
if visitCount != len(specs) { if visitCount != len(specs) {
t.Errorf("expected the visitor func to be invoked %d times; got %d", len(specs), visitCount) t.Errorf("expected the visitor func to be invoked %d times; got %d", len(specs), visitCount)
} }
// Test that the visitor function can abort the scan by returning false
visitCount = 0
VisitMemRegions(func(entry *MemoryMapEntry) bool {
visitCount++
return false
})
if visitCount != 1 {
t.Errorf("expected the visitor func to be invoked %d times; got %d", 1, visitCount)
}
} }
func TestGetFramebufferInfo(t *testing.T) { func TestGetFramebufferInfo(t *testing.T) {