Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Update from Echo Stream Server (currently used code) #6

Merged
merged 1 commit into from over 1 year ago

2 participants

Yuri Lukyanov Lev Walkin
Yuri Lukyanov
Owner

No description provided.

Lev Walkin vlm merged commit 6707f92 into from August 22, 2012
Lev Walkin vlm closed this August 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 15, 2012
Yuri Lukyanov Sync code with Echo Stream Server aa91baf
This page is out of date. Refresh to see the latest.
50  ErlangTerm.ml
@@ -475,11 +475,11 @@ let binary_to_term_buf2 off buf =
475 475
 			incr off;
476 476
 			byte in
477 477
 	let iint () = Int32.(
478  
-      to_int (
479  
-        List.fold_left (fun a e -> add a (shift_left (of_int (ibyte ())) e))
480  
-		  zero [24; 16; 8; 0]
481  
-      )
482  
-    ) in
  478
+		to_int (
  479
+			List.fold_left (fun a e -> add a (shift_left (of_int (ibyte ())) e))
  480
+			zero [24; 16; 8; 0]
  481
+			)
  482
+		) in
483 483
 	let istr len = let s = Buffer.sub buf !off len in
484 484
 			off := !off + len;
485 485
 			s in
@@ -522,18 +522,50 @@ let term_to_binary_out out_channel term =
522 522
 let term_to_binary_buf buffer term =
523 523
 	let abyte x = Buffer.add_char buffer (char_of_int x) in
524 524
 	let aint x =
525  
-      let x32 = Int32.of_int x in
526  
-	  List.iter (fun n ->
527  
-        abyte Int32.(to_int (logand (shift_right_logical x32 n) 0xFFl))
528  
-      ) [24; 16; 8; 0] in
  525
+		let x32 = Int32.of_int x in
  526
+		List.iter (fun n ->
  527
+			abyte Int32.(to_int (logand (shift_right_logical x32 n) 0xFFl))
  528
+			) [24; 16; 8; 0] in
529 529
 	let astr = Buffer.add_string buffer in
530 530
 	let abuf = Buffer.add_buffer buffer in
531 531
 	abyte 131;
532 532
 	erlang_term_encode abyte aint astr abuf term;;
533 533
 
  534
+let term_to_binary_bufs term =
  535
+    let aref = ref [] in
  536
+	let new_buffer size = Buffer.create size in
  537
+	let current_size = ref (10000) in
  538
+	let buffer = ref (new_buffer !current_size) in
  539
+	let wrapper f x =
  540
+		let wr f0 x0 =
  541
+			aref := !buffer :: !aref;
  542
+			if !current_size < 246000 then
  543
+				current_size := !current_size * 2;
  544
+			buffer := new_buffer !current_size;
  545
+			f !buffer x
  546
+		in
  547
+		if (Buffer.length !buffer + 1000 > !current_size) then
  548
+			wr f x
  549
+		else
  550
+			try f !buffer x with _ -> wr f x
  551
+	in
  552
+	let abyte x = wrapper Buffer.add_char (char_of_int x) in
  553
+	let aint x =
  554
+		let x32 = Int32.of_int x in
  555
+		List.iter (fun n ->
  556
+			abyte Int32.(to_int (logand (shift_right_logical x32 n) 0xFFl))
  557
+			) [24; 16; 8; 0] in
  558
+	let astr = wrapper Buffer.add_string in
  559
+	let abuf = wrapper Buffer.add_buffer in
  560
+	abyte 131;
  561
+	erlang_term_encode abyte aint astr abuf term;
  562
+	List.rev (!buffer :: !aref);;
  563
+
534 564
 (* Return a fresh Buffer containing the serialized Erlang term *)
535 565
 let term_to_binary term =
536 566
 	let b = Buffer.create 1024 in
537 567
 	let () = term_to_binary_buf b term in
538 568
 	b;;
539 569
 
  570
+exception ExceptionTerm of erlang_term
  571
+
2  ErlangTerm.mli
@@ -54,4 +54,6 @@ val binary_to_term_buf : Buffer.t -> erlang_term
54 54
 val binaries_to_terms_buf : Buffer.t -> erlang_term list
55 55
 val term_to_binary_out : out_channel -> erlang_term -> unit
56 56
 val term_to_binary_buf : Buffer.t -> erlang_term -> unit
  57
+val term_to_binary_bufs : erlang_term -> Buffer.t list
57 58
 val term_to_binary : erlang_term -> Buffer.t
  59
+exception ExceptionTerm of erlang_term
22  ErlangTerm_Check.ml
@@ -68,6 +68,14 @@ let bignum_check_negative () =
68 68
 		| _ -> failwith "Bignum test failed"
69 69
 	;;
70 70
 
  71
+let bigbuffer_check () =
  72
+	let a = ref [] in
  73
+	for v = 1 to 100000 do
  74
+		a := complexTerm :: !a
  75
+	done;
  76
+	ET_List !a;;
  77
+    
  78
+
71 79
 (* Check that the given Erlang term passes the round-trip encode/decode test *)
72 80
 let check_round_trip op term =
73 81
 	let rewrittenTerm = binary_to_term_buf (term_to_binary term) in
@@ -115,6 +123,20 @@ let selfcheck () =
115 123
 	print_erlang_term complexTerm;
116 124
 	print_newline ();
117 125
 	check_round_trip (=) complexTerm;
  126
+
  127
+	ignore(term_to_binary_bufs (bigbuffer_check ()));
  128
+
  129
+    (* do not check it on 64bit system as
  130
+     * Sys.max_string_length = 144115188075855863
  131
+     * it's too long to generate assertion
  132
+     *)
  133
+    if Sys.word_size == 32 then
  134
+        let b = Buffer.create 1024 in
  135
+        try
  136
+            term_to_binary_buf b (bigbuffer_check ());
  137
+            assert(false)
  138
+        with Failure "Buffer.add: cannot grow buffer" -> ();
  139
+
118 140
 	print_string "Selfcheck OK\n";;
119 141
 
120 142
 let _ =
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.