Factor out the kernel loading function. Add kernel_min, kernel_max addresses.
[virt-mem.git] / lib / virt_mem_mmap.ml
index bf048f7..c323c1a 100644 (file)
@@ -309,9 +309,13 @@ let rec get_mapping addr = function
 (* Use the tree to quickly check if an address is mapped (returns false
  * if it's a hole).
  *)
-let is_mapped { tree = tree } addr =
-  let _, mapping = get_mapping addr tree in
-  mapping <> None
+let is_mapped { mappings = mappings; tree = tree } addr =
+  (* NB: No [`HasMapping] in the type so we have to check mappings <> []. *)
+  match mappings with
+  | [] -> false
+  | _ ->
+      let _, mapping = get_mapping addr tree in
+      mapping <> None
 
 (* Get a single byte. *)
 let get_byte { tree = tree } addr =
@@ -410,6 +414,19 @@ let dowhile { tree = tree } addr cond =
   in
   loop addr
 
+let is_mapped_range ({ mappings = mappings } as t) addr size =
+  match mappings with
+  (* NB: No [`HasMapping] in the type so we have to check mappings <> []. *)
+  | [] -> false
+  | _ ->
+      (* Quick and dirty.  It's possible to make a much faster
+       * implementation of this which doesn't call the closure for every
+       * byte.
+       *)
+      let size = ref size in
+      try dowhile t addr (fun _ _ -> decr size; !size > 0); true
+      with Invalid_argument "dowhile" -> false
+
 (* Get a string, ending at ASCII NUL character. *)
 let get_string t addr =
   let chars = ref [] in