From e1ada1ac8a50fd814c8868e97f977f3544d4929d Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Wed, 10 May 2017 22:24:03 +0100 Subject: [PATCH] Allow mem region visitors to abort the scan by returning false --- kernel/hal/multiboot/multiboot.go | 9 ++++++--- kernel/hal/multiboot/multiboot_test.go | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/kernel/hal/multiboot/multiboot.go b/kernel/hal/multiboot/multiboot.go index 1dd6e9f..6f7de4a 100644 --- a/kernel/hal/multiboot/multiboot.go +++ b/kernel/hal/multiboot/multiboot.go @@ -119,8 +119,9 @@ var ( ) // MemRegionVisitor defies a visitor function that gets invoked by VisitMemRegions -// for each memory region provided by the boot loader. -type MemRegionVisitor func(entry *MemoryMapEntry) +// for each memory region provided by the boot loader. The visitor must return true +// to continue or false to abort the scan. +type MemRegionVisitor func(entry *MemoryMapEntry) bool // SetInfoPtr updates the internal multiboot information pointer to the given // value. This function must be invoked before invoking any other function @@ -151,7 +152,9 @@ func VisitMemRegions(visitor MemRegionVisitor) { entry.Type = MemReserved } - visitor(entry) + if !visitor(entry) { + return + } curPtr += uintptr(ptrMapHeader.entrySize) } diff --git a/kernel/hal/multiboot/multiboot_test.go b/kernel/hal/multiboot/multiboot_test.go index 62193b3..9b8a963 100644 --- a/kernel/hal/multiboot/multiboot_test.go +++ b/kernel/hal/multiboot/multiboot_test.go @@ -59,8 +59,9 @@ func TestVisitMemRegion(t *testing.T) { var visitCount int SetInfoPtr(uintptr(unsafe.Pointer(&emptyInfoData[0]))) - VisitMemRegions(func(_ *MemoryMapEntry) { + VisitMemRegions(func(_ *MemoryMapEntry) bool { visitCount++ + return true }) if visitCount != 0 { @@ -71,7 +72,7 @@ func TestVisitMemRegion(t *testing.T) { SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0]))) multibootInfoTestData[128] = 0xFF - VisitMemRegions(func(entry *MemoryMapEntry) { + VisitMemRegions(func(entry *MemoryMapEntry) bool { if entry.PhysAddress != specs[visitCount].expPhys { 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) } visitCount++ + return true }) if visitCount != len(specs) { 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) {