From 9437d8d73b31dd25504c3d652f0ad77ccd1239a3 Mon Sep 17 00:00:00 2001
From: Sverker Eriksson <sverker@erlang.org>
Date: Tue, 18 Apr 2017 19:28:06 +0200
Subject: tools: Fix more assumptions in lcnt about external format

Also tried to prepare for large creation.
---
 lib/tools/src/lcnt.erl | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)

diff --git a/lib/tools/src/lcnt.erl b/lib/tools/src/lcnt.erl
index 22db947e7a..864d62f6c5 100644
--- a/lib/tools/src/lcnt.erl
+++ b/lib/tools/src/lcnt.erl
@@ -698,19 +698,47 @@ stats2record([{{File,Line},{Tries,Colls,{S,Ns,N}}}|Stats]) ->
 	    nt    = N} | stats2record(Stats)];
 stats2record([]) -> [].
 
+
 clean_id_creation(Id) when is_pid(Id) ->
     Bin = term_to_binary(Id),
-    <<H:3/binary, L:16, Node:L/binary, Ids:8/binary, _Creation/binary>> = Bin,
-    Bin2 = list_to_binary([H, bytes16(L), Node, Ids, 0]),
+    <<H:3/binary, Rest/binary>> = Bin,
+    <<131, PidTag, AtomTag>> = H,
+    LL = atomlen_bits(AtomTag),
+    CL = creation_bits(PidTag),
+    <<L:LL, Node:L/binary, Ids:8/binary, _Creation/binary>> = Rest,
+    Bin2 = list_to_binary([H, <<L:LL>>, Node, Ids, <<0:CL>>]),
     binary_to_term(Bin2);
 clean_id_creation(Id) when is_port(Id) ->
     Bin = term_to_binary(Id),
-    <<H:3/binary, L:16, Node:L/binary, Ids:4/binary, _Creation/binary>> = Bin,
-    Bin2 = list_to_binary([H, bytes16(L), Node, Ids, 0]),
+    <<H:3/binary, Rest/binary>> = Bin,
+    <<131, PortTag, AtomTag>> = H,
+    LL = atomlen_bits(AtomTag),
+    CL = creation_bits(PortTag),
+    <<L:LL, Node:L/binary, Ids:4/binary, _Creation/binary>> = Rest,
+    Bin2 = list_to_binary([H, <<L:LL>>, Node, Ids, <<0:CL>>]),
     binary_to_term(Bin2);
 clean_id_creation(Id) ->
     Id.
 
+-define(PID_EXT, $g).
+-define(NEW_PID_EXT, $X).
+-define(PORT_EXT, $f).
+-define(NEW_PORT_EXT, $Y).
+-define(ATOM_EXT, $d).
+-define(SMALL_ATOM_EXT, $s).
+-define(ATOM_UTF8_EXT, $v).
+-define(SMALL_ATOM_UTF8_EXT, $w).
+
+atomlen_bits(?ATOM_EXT) -> 16;
+atomlen_bits(?SMALL_ATOM_EXT) -> 8;
+atomlen_bits(?ATOM_UTF8_EXT) -> 16;
+atomlen_bits(?SMALL_ATOM_UTF8_EXT) -> 8.
+
+creation_bits(?PID_EXT) -> 8;
+creation_bits(?NEW_PID_EXT) -> 32;
+creation_bits(?PORT_EXT) -> 8;
+creation_bits(?NEW_PORT_EXT) -> 32.
+
 %% serializer
 
 state_default(Field) -> proplists:get_value(Field, state2list(#state{})).
@@ -932,9 +960,6 @@ strings([{space,  N,      S} | Ss], Out) -> strings(Ss, Out ++ term2string(term2
 strings([{left,   N,      S} | Ss], Out) -> strings(Ss, Out ++ term2string(term2string(" ~~s~~~ws", [N]), [S,""]));
 strings([S|Ss], Out) -> strings(Ss, Out ++ term2string("~ts", [S])).
 
--define(SMALL_ATOM_UTF8_EXT, $w).
--define(ATOM_UTF8_EXT, $v).
--define(ATOM_EXT, $d).
 
 term2string({M,F,A}) when is_atom(M), is_atom(F), is_integer(A) -> term2string("~p:~p/~p", [M,F,A]);
 term2string(Term) when is_port(Term) ->
-- 
cgit v1.2.3