-
Notifications
You must be signed in to change notification settings - Fork 1
/
mem_block.ml
66 lines (51 loc) · 1.42 KB
/
mem_block.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open Lwt
open Back
open Block
module MemBlock = (struct
open Lwt
module StringMap = Map.Make(String)
type t = { mutable blocks : string StringMap.t }
let block_size t = 0x00001000
let device_size t = 6 * 1024 * 1024 * 1024
let zeros t = String.make (block_size t) '\x00'
let make_key lba = Printf.sprintf "%0x16x" lba
let create =
(* to be able to reconnect *)
let ts = ref (Hashtbl.create 5) in
(fun uri ->
let t =
try Hashtbl.find !ts uri
with Not_found ->
let t = {blocks = StringMap.empty} in
let () = Hashtbl.add !ts uri t in
t
in
Lwt.return t)
let read_block t lba =
let key = make_key lba in
let block =
try
StringMap.find key t.blocks
with
| Not_found -> zeros t
in
Lwt.return block
let read_blocks t lbas =
Lwt_list.map_s
(fun lba -> read_block t lba >>= fun block -> Lwt.return (lba,block))
lbas
let write_blocks t writes =
let map = t.blocks in
let add_one map (lba,block) =
let key = make_key lba in
StringMap.add key block map
in
let map' = List.fold_left add_one map writes in
let () = t.blocks <- map' in
Lwt.return ()
let flush t = Lwt.return ()
let trim t off dlen =
log_f "mem : trim 0x%0x16x 0x%08x" off dlen >>= fun () ->
Lwt.return ()
let disconnect t = Lwt.return ()
end : BLOCK)