Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions cli/lib/isla/allocator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@

(** Page-oriented address allocator. *)

type t = {mutable current : int}
type t =
{ mutable current : int;
limit : int option
}

let default_base = 0x1000

Expand All @@ -56,17 +59,27 @@ let align_up addr alignment =

let page_after addr = align_up (addr + 1) page_size

let make ?(base = default_base) ?(reserved = []) () =
let make ?(base = default_base) ?limit ?(reserved = []) () =
let current =
List.fold_left
(fun current addr -> max current (page_after addr))
base reserved
in
{current}
( match limit with
| Some limit when current > limit ->
Litmus.Error.failwith "allocator: initial address exceeds limit"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth doing a module Error = Litmus.Error at the top to make those calls shorter

| _ -> ()
);
{current; limit}

let alloc_aligned allocator ~size ~alignment =
let addr = align_up allocator.current alignment in
allocator.current <- addr + size;
let next = addr + size in
( match allocator.limit with
| Some limit when next > limit ->
Litmus.Error.failwith "allocator: limit exceeded"
| _ -> allocator.current <- next
);
addr

let alloc_page allocator =
Expand Down
6 changes: 3 additions & 3 deletions cli/lib/isla/allocator.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ val page_size : int
(** Size of one block (2MB) allocated by [alloc_big]. *)
val big_size : int

(** Make an allocator, optionally with reserved addresses. Each reserved
address blocks the page containing it. *)
val make : ?base:int -> ?reserved:int list -> unit -> t
(** Make an allocator, optionally with an exclusive upper limit and reserved
addresses. Each reserved address blocks the page containing it. *)
val make : ?base:int -> ?limit:int -> ?reserved:int list -> unit -> t

(** Allocate [size] bytes at an address aligned to [alignment]. *)
val alloc_aligned : t -> size:int -> alignment:int -> int
Expand Down
Loading