Rewrite critical search inner loop in C for speed.
[virt-mem.git] / lib / virt_mem_mmap.ml
index 98dc84a..963e10d 100644 (file)
@@ -82,7 +82,7 @@ let add_string ({ mappings = mappings } as t) str addr =
   if addr &^ 7L <> 0L then
     invalid_arg "add_file: mapping address must be aligned to 8 bytes";
   let size = String.length str in
-  (* Use Bigarray.Array1. XXX We should just use the string. *)
+  (* Copy the string data to a Bigarray. *)
   let arr = Array1.create char c_layout size in
   for i = 0 to size-1 do
     Array1.set arr i (String.unsafe_get str i)
@@ -108,6 +108,15 @@ let _find_map { mappings = mappings } pred =
   in
   loop mappings
 
+(* The following functions are actually written in C
+ * because memmem(3) is likely to be much faster than anything
+ * we could write in OCaml.
+ *
+ * Also OCaml bigarrays are specifically designed to be accessed
+ * easily from C:
+ *   http://caml.inria.fr/pub/docs/manual-ocaml/manual043.html
+ *)
+(*
 (* Array+offset = string? *)
 let string_at arr offset str strlen =
   let j = ref offset in
@@ -144,6 +153,10 @@ let _find_in start align str arr =
     loop ()
   )
   else Some start
+*)
+external _find_in :
+  int -> int -> string -> (char,int8_unsigned_elt,c_layout) Array1.t ->
+  int option = "virt_mem_mmap_find_in"
 
 (* Generic find function. *)
 let _find t start align str =