Skip to content

Commit

Permalink
Makes mem.New return a pointer, handles page size 0 edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
jpr98 committed Feb 2, 2020
1 parent 41b1b4b commit 5b9bf5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ func main() {

scanner := bufio.NewScanner(file)

realMemory, err := mem.New(2048, 16)
if err != nil {
errorMessage := fmt.Sprintf("Error creating real memory: %s", err.Error())
panic(errorMessage)
}

swapMemory, err := mem.New(2048, 16)
if err != nil {
errorMessage := fmt.Sprintf("Error creating swap memory: %s", err.Error())
panic(errorMessage)
}
comp = cpu.New(
mem.New(2048, 16),
mem.New(1024, 16),
*realMemory,
*swapMemory,
)

for scanner.Scan() {
Expand Down
11 changes: 8 additions & 3 deletions mem/mem.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mem

import "errors"

type page struct {
pid string
virtualAddress int
Expand All @@ -12,12 +14,15 @@ type Memory struct {
}

// New creates a new Memory
func New(size, pageSize int) Memory {
func New(size, pageSize int) (*Memory, error) {
if pageSize == 0 {
return nil, errors.New("PageSize should not be zero")
}
numOfPages := size / pageSize
return Memory{
return &Memory{
pages: make([]page, numOfPages),
PageSize: pageSize,
}
}, nil
}

// AllocatePage ...
Expand Down

0 comments on commit 5b9bf5d

Please sign in to comment.