Overwrite deleted elements with a dummy element to allow them to be GCed

This commit is contained in:
Fabian 2018-11-07 17:06:22 -06:00
parent 0b3c19fa65
commit f8008d79fa
2 changed files with 25 additions and 27 deletions

View file

@ -345,20 +345,6 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
to_buf = s && len = Bytes.length s) to_buf = s && len = Bytes.length s)
*) *)
let clear b =
b.stop <- 0;
b.start <- 0;
()
(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
Byte.clear b; \
Byte.length b = 0)
*)
let is_empty b = b.start = b.stop let is_empty b = b.start = b.stop
(*$Q (*$Q
@ -373,6 +359,7 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
let take_front_exn b = let take_front_exn b =
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
let c = A.get b.buf b.start in let c = A.get b.buf b.start in
A.set b.buf b.start A.dummy;
b.start <- next_ b b.start; b.start <- next_ b b.start;
c c
@ -392,7 +379,9 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
if b.stop = 0 if b.stop = 0
then b.stop <- A.length b.buf - 1 then b.stop <- A.length b.buf - 1
else b.stop <- b.stop - 1; else b.stop <- b.stop - 1;
A.get b.buf b.stop let c = A.get b.buf b.stop in
A.set b.buf b.stop A.dummy;
c
let take_back b = try Some (take_back_exn b) with Empty -> None let take_back b = try Some (take_back_exn b) with Empty -> None
@ -408,6 +397,7 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
let junk_front b = let junk_front b =
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
A.set b.buf b.start A.dummy;
if b.start + 1 = A.length b.buf if b.start + 1 = A.length b.buf
then b.start <- 0 then b.start <- 0
else b.start <- b.start + 1 else b.start <- b.start + 1
@ -425,7 +415,8 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
if b.start = b.stop then raise Empty; if b.start = b.stop then raise Empty;
if b.stop = 0 if b.stop = 0
then b.stop <- A.length b.buf - 1 then b.stop <- A.length b.buf - 1
else b.stop <- b.stop - 1 else b.stop <- b.stop - 1;
A.set b.buf b.stop A.dummy
(*$Q (*$Q
a_str (fun s -> let s = Bytes.of_string s in \ a_str (fun s -> let s = Bytes.of_string s in \
@ -440,15 +431,9 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
if len > length b then ( if len > length b then (
invalid_arg "CCRingBuffer.skip"; invalid_arg "CCRingBuffer.skip";
); );
if b.stop >= b.start then ( for _ = 1 to len do
b.start <- b.start + len; junk_front b
assert (b.stop >= b.start); done
) else (
let len_end = A.length b.buf - b.start in
if len >= len_end
then b.start <- len-len_end (* wrap to the beginning *)
else b.start <- b.start + len
)
(*$Q (*$Q
(Q.pair a_str a_str) (fun (s,s') -> \ (Q.pair a_str a_str) (fun (s,s') -> \
@ -462,6 +447,19 @@ module MakeFromArray(A:Array.S) : S with module Array = A = struct
Byte.length b + l' = l) Byte.length b + l' = l)
*) *)
let clear b =
skip b (length b)
(*$Q
a_str (fun s -> let s = Bytes.of_string s in \
let s_len = Bytes.length s in \
let b = Byte.create (max s_len 64) in \
Byte.blit_from b s 0 s_len; \
Byte.clear b; \
Byte.length b = 0)
*)
let iter b ~f = let iter b ~f =
if b.stop >= b.start if b.stop >= b.start
then for i = b.start to b.stop - 1 do f (A.get b.buf i) done then for i = b.start to b.stop - 1 do f (A.get b.buf i) done

View file

@ -121,7 +121,7 @@ module type S = sig
(** Extract the current content into a list. *) (** Extract the current content into a list. *)
val clear : t -> unit val clear : t -> unit
(** Clear the content of the buffer. Doesn't actually destroy the content. *) (** Clear the content of the buffer *)
val is_empty :t -> bool val is_empty :t -> bool
(** Is the buffer empty (i.e. contains no elements)? *) (** Is the buffer empty (i.e. contains no elements)? *)