aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/src/asn1ct_func.erl
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2013-09-04 13:20:15 +0200
committerBjörn Gustavsson <[email protected]>2013-09-04 13:20:48 +0200
commit93fe50a6e3f685e77a48921273559b6f03b89fb8 (patch)
tree09e5b7e0deb680ee2347f5a0c96be9c7b00d5e74 /lib/asn1/src/asn1ct_func.erl
parent053b721841efb06d3339c0376783a6dd09e625b5 (diff)
parenta2792ebf8b46903bd05b05288539482722adfa51 (diff)
downloadotp-93fe50a6e3f685e77a48921273559b6f03b89fb8.tar.gz
otp-93fe50a6e3f685e77a48921273559b6f03b89fb8.tar.bz2
otp-93fe50a6e3f685e77a48921273559b6f03b89fb8.zip
Merge branch 'bjorn/asn1/optimize-per-encoding' into maint
OTP-11300 OTP-11262 * bjorn/asn1/optimize-per-encoding: (25 commits) asn1ct_constucted_per: Directly call asn1ct_gen_per Clean up handling of .asn1db files PER, UPER: Fix encoding/decoding of open types greater than 16K PER, UPER: Optimize table constraints PER, UPER: Optimize encoding using an intermediate format Refactor decoding of components of SEQUENCE OF / SET OF PER,UPER: Get rid of unused 'telltype' argument in decoding functions Optimize the generated encode/2 function UPER: Optimize complete/1 Clean up checking of objects Improve tests of deep table constraints BER: Handle multiple optional SEQUENCE fields with table constraints Test OPTIONAL and DEFAULT for open types PER/UPER: Fix encoding of an object set with multiple inlined constructs Remove broken support for multiple UNIQUE Extend the test for parameterized information objects asn1_SUITE: Remove off-topic (and slow) smp/1 test case SeqOf: Add more tricky SEQUENCE OF tests Clean up handling of extension addition groups Refactor encoding of REAL ...
Diffstat (limited to 'lib/asn1/src/asn1ct_func.erl')
-rw-r--r--lib/asn1/src/asn1ct_func.erl65
1 files changed, 59 insertions, 6 deletions
diff --git a/lib/asn1/src/asn1ct_func.erl b/lib/asn1/src/asn1ct_func.erl
index ab0dbcce8f..dbadedb683 100644
--- a/lib/asn1/src/asn1ct_func.erl
+++ b/lib/asn1/src/asn1ct_func.erl
@@ -19,7 +19,7 @@
%%
-module(asn1ct_func).
--export([start_link/0,need/1,call/3,generate/1]).
+-export([start_link/0,need/1,call/3,call_gen/3,call_gen/4,generate/1]).
-export([init/1,handle_call/3,handle_cast/2,terminate/2]).
start_link() ->
@@ -28,15 +28,33 @@ start_link() ->
ok.
call(M, F, Args) ->
- MFA = {M,F,length(Args)},
+ A = length(Args),
+ MFA = {M,F,A},
need(MFA),
- asn1ct_gen:emit([F,"(",call_args(Args, ""),")"]).
+ case M of
+ binary ->
+ asn1ct_gen:emit(["binary:",F,"(",call_args(Args, ""),")"]);
+ _ ->
+ asn1ct_gen:emit([F,"(",call_args(Args, ""),")"])
+ end.
+need({binary,_,_}) ->
+ ok;
+need({erlang,_,_}) ->
+ ok;
need(MFA) ->
asn1ct_rtt:assert_defined(MFA),
cast({need,MFA}).
+call_gen(Prefix, Key, Gen, Args) when is_function(Gen, 2) ->
+ F = req({gen_func,Prefix,Key,Gen}),
+ asn1ct_gen:emit([F,"(",call_args(Args, ""),")"]).
+
+call_gen(Prefix, Key, Gen) when is_function(Gen, 2) ->
+ req({gen_func,Prefix,Key,Gen}).
+
generate(Fd) ->
+ do_generate(Fd),
Used0 = req(get_used),
erase(?MODULE),
Used = sofs:set(Used0, [mfa]),
@@ -53,10 +71,13 @@ cast(Req) ->
%%% Internal functions.
--record(st, {used}).
+-record(st, {used, %Used functions
+ gen, %Dynamically generated functions
+ gc=1 %Counter for generated functions
+ }).
init([]) ->
- St = #st{used=gb_sets:empty()},
+ St = #st{used=gb_sets:empty(),gen=gb_trees:empty()},
{ok,St}.
handle_cast({need,MFA}, #st{used=Used0}=St) ->
@@ -69,7 +90,20 @@ handle_cast({need,MFA}, #st{used=Used0}=St) ->
end.
handle_call(get_used, _From, #st{used=Used}=St) ->
- {stop,normal,gb_sets:to_list(Used),St}.
+ {stop,normal,gb_sets:to_list(Used),St};
+handle_call(get_gen, _From, #st{gen=G0}=St) ->
+ {L,G} = do_get_gen(gb_trees:to_list(G0), [], []),
+ {reply,L,St#st{gen=gb_trees:from_orddict(G)}};
+handle_call({gen_func,Prefix,Key,GenFun}, _From, #st{gen=G0,gc=Gc0}=St) ->
+ case gb_trees:lookup(Key, G0) of
+ none ->
+ Name = list_to_atom(Prefix ++ integer_to_list(Gc0)),
+ Gc = Gc0 + 1,
+ G = gb_trees:insert(Key, {Name,GenFun}, G0),
+ {reply,Name,St#st{gen=G,gc=Gc}};
+ {value,{Name,_}} ->
+ {reply,Name,St}
+ end.
terminate(_, _) ->
ok.
@@ -98,3 +132,22 @@ update_worklist([H|T], Used, Ws) ->
update_worklist(T, Used, Ws)
end;
update_worklist([], _, Ws) -> Ws.
+
+do_get_gen([{_,{_,done}}=Keep|T], Gacc, Kacc) ->
+ do_get_gen(T, Gacc, [Keep|Kacc]);
+do_get_gen([{K,{Name,_}=V}|T], Gacc, Kacc) ->
+ do_get_gen(T, [V|Gacc], [{K,{Name,done}}|Kacc]);
+do_get_gen([], Gacc, Kacc) ->
+ {lists:sort(Gacc),lists:reverse(Kacc)}.
+
+do_generate(Fd) ->
+ case req(get_gen) of
+ [] ->
+ ok;
+ [_|_]=Gen ->
+ _ = [begin
+ ok = file:write(Fd, "\n"),
+ GenFun(Fd, Name)
+ end || {Name,GenFun} <- Gen],
+ do_generate(Fd)
+ end.