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

Change boot allocator signature so it returns a kernel error

This commit is contained in:
Achilleas Anagnostopoulos 2017-05-31 14:48:07 +01:00
parent d7eb2547dd
commit 7156b09656
2 changed files with 19 additions and 17 deletions

View File

@ -1,6 +1,7 @@
package pmm package pmm
import ( import (
"github.com/achilleasa/gopher-os/kernel"
"github.com/achilleasa/gopher-os/kernel/hal/multiboot" "github.com/achilleasa/gopher-os/kernel/hal/multiboot"
"github.com/achilleasa/gopher-os/kernel/kfmt/early" "github.com/achilleasa/gopher-os/kernel/kfmt/early"
"github.com/achilleasa/gopher-os/kernel/mem" "github.com/achilleasa/gopher-os/kernel/mem"
@ -11,6 +12,9 @@ var (
// which is used to bootstrap the kernel before initializing a more // which is used to bootstrap the kernel before initializing a more
// advanced memory allocator. // advanced memory allocator.
EarlyAllocator BootMemAllocator EarlyAllocator BootMemAllocator
errBootAllocUnsupportedPageSize = &kernel.Error{Module: "pmm.BootMemAllocator", Message: "allocator only support allocation requests of order(0)"}
errBootAllocOutOfMemory = &kernel.Error{Module: "pmm.BootMemAllocator", Message: "out of memory"}
) )
// BootMemAllocator implements a rudimentary physical memory allocator which is used // BootMemAllocator implements a rudimentary physical memory allocator which is used
@ -56,18 +60,13 @@ func (alloc *BootMemAllocator) Init() {
} }
// AllocFrame scans the system memory regions reported by the bootloader and // AllocFrame scans the system memory regions reported by the bootloader and
// reseves the next available free frame. AllocFrame returns false if no more // reserves the next available free frame.
// memory can be allocated.
// //
// The allocator only supports allocating blocks equal to the page size. // AllocFrame returns an error if no more memory can be allocated or when the
// Requests for a page order > 0 will cause the allocator to return false. // requested page order is > 0.
// func (alloc *BootMemAllocator) AllocFrame(order mem.PageOrder) (Frame, *kernel.Error) {
// The use of a bool return value is intentional; if this method returned an
// error then the compiler would call runtime.convT2I which in turn invokes the
// yet uninitialized Go allocator.
func (alloc *BootMemAllocator) AllocFrame(order mem.PageOrder) (Frame, bool) {
if order > 0 { if order > 0 {
return InvalidFrame, false return InvalidFrame, errBootAllocUnsupportedPageSize
} }
var ( var (
@ -103,11 +102,11 @@ func (alloc *BootMemAllocator) AllocFrame(order mem.PageOrder) (Frame, bool) {
}) })
if foundPageIndex == -1 { if foundPageIndex == -1 {
return InvalidFrame, false return InvalidFrame, errBootAllocOutOfMemory
} }
alloc.allocCount++ alloc.allocCount++
alloc.lastAllocIndex = foundPageIndex alloc.lastAllocIndex = foundPageIndex
return Frame(foundPageIndex), true return Frame(foundPageIndex), nil
} }

View File

@ -36,9 +36,12 @@ func TestBootMemoryAllocator(t *testing.T) {
allocFrameCount uint64 allocFrameCount uint64
) )
for alloc.Init(); ; allocFrameCount++ { for alloc.Init(); ; allocFrameCount++ {
frame, ok := alloc.AllocFrame(mem.PageOrder(0)) frame, err := alloc.AllocFrame(mem.PageOrder(0))
if !ok { if err != nil {
break if err == errBootAllocOutOfMemory {
break
}
t.Fatalf("[frame %d] unexpected allocator error: %v", allocFrameCount, err)
} }
expAddress := uintptr(uint64(alloc.lastAllocIndex) * uint64(mem.PageSize)) expAddress := uintptr(uint64(alloc.lastAllocIndex) * uint64(mem.PageSize))
@ -56,8 +59,8 @@ func TestBootMemoryAllocator(t *testing.T) {
} }
// This allocator only works with order(0) blocks // This allocator only works with order(0) blocks
if frame, ok := alloc.AllocFrame(mem.PageOrder(1)); ok || frame.IsValid() { if frame, err := alloc.AllocFrame(mem.PageOrder(1)); err != errBootAllocUnsupportedPageSize || frame.IsValid() {
t.Fatalf("expected allocator to return false and an invalid frame when requested to allocate a block with order > 0; got %t, %v", ok, frame) t.Fatalf("expected allocator to return errBootAllocUnsupportedPageSize and an invalid frame when requested to allocate a block with order > 0; got %v, %v", err, frame)
} }
} }