(** ** Semantics of dynamic memory allocation (malloc) *)

Inductive extcall_malloc_sem (ge: Senv.t):
              list val -> mem -> trace -> val -> mem -> Prop :=
  | extcall_malloc_sem_intro: forall sz m m' b m'',
      Mem.alloc m (- size_chunk Mptr) (Ptrofs.unsigned sz) = (m', b) ->
      Mem.store Mptr m' b (- size_chunk Mptr) (Vptrofs sz) = Some m'' ->
      extcall_malloc_sem ge (Vptrofs sz :: nil) m E0 (Vptr b Ptrofs.zero) m''.(** ** Semantics of dynamic memory allocation (malloc) *)

Inductive extcall_malloc_sem (ge: Senv.t):
              list val -> mem -> trace -> val -> mem -> Prop :=
  | extcall_malloc_sem_intro: forall sz m m' b m'',
      Mem.alloc m (- size_chunk Mptr) (Ptrofs.unsigned sz) = (m', b) ->
      Mem.store Mptr m' b (- size_chunk Mptr) (Vptrofs sz) = Some m'' ->
      extcall_malloc_sem ge (Vptrofs sz :: nil) m E0 (Vptr b Ptrofs.zero) m''.

(* Specifications: *)
Lemma extcall_malloc_ok: extcall_properties extcall_malloc_sem (mksignature (Tptr :: nil) (Some Tptr) cc_default).
Lemma alloc_parallel_inject' : forall (f : meminj) (m1 m2 : mem) (lo1 hi1 : Z) (m1' : Mem.mem') (b1 : block) (lo2 hi2 : Z), Mem.inject f m1 m2 -> Mem.alloc m1 lo1 hi1 = (m1', b1) -> injection_full f m1 -> lo2 <= lo1 -> hi1 <= hi2 -> exists (f' : meminj) (m2' : Mem.mem') (b2 : block), Mem.alloc m2 lo2 hi2 = (m2', b2) /\ Mem.inject f' m1' m2' /\ inject_incr f f' /\ f' b1 = Some (b2, 0) /\ (forall b : block, b <> b1 -> f' b = f b) /\ injection_full f' m1'.
Theorem alloc_left_mapped_inject': forall f m1 m2 lo hi m1' b1 b2 delta, Mem.inject f m1 m2 -> injection_full f m1 -> Mem.alloc m1 lo hi = (m1', b1) -> Mem.valid_block m2 b2 -> 0 <= delta <= Ptrofs.max_unsigned -> (forall ofs k p, Mem.perm m2 b2 ofs k p -> delta = 0 \/ 0 <= ofs < Ptrofs.max_unsigned) -> (forall ofs k p, lo <= ofs < hi -> Mem.perm m2 b2 (ofs + delta) k p) -> Mem.inj_offset_aligned delta (hi-lo) -> (forall b delta' ofs k p, f b = Some (b2, delta') -> Mem.perm m1 b ofs k p -> lo + delta <= ofs + delta' < hi + delta -> False) -> exists f', Mem.inject f' m1' m2 /\ inject_incr f f' /\ f' b1 = Some(b2, delta) /\ (forall b, b <> b1 -> f' b = f b) /\ injection_full f' m1'.
Lemma extcall_malloc_ok: extcall_properties extcall_malloc_sem (mksignature (Tptr :: nil) (Some Tptr) cc_default).