Skip to content

Commit 7e73508

Browse files
committed
Fix allocation issue in refc_binary_info
Also add new test based on refc_binary_info authored by @fadushin Signed-off-by: Paul Guyot <[email protected]>
1 parent 427dd48 commit 7e73508

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/libAtomVM/refc_binary.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ term refc_binary_create_binary_info(Context *ctx)
7676
if (len == 0) {
7777
return term_nil();
7878
}
79-
if (memory_ensure_free(ctx, len * TUPLE_SIZE(2)) != MEMORY_GC_OK) {
79+
if (memory_ensure_free(ctx, len * (2 + TUPLE_SIZE(2))) != MEMORY_GC_OK) {
8080
return term_invalid_term();
8181
}
8282
term ret = term_nil();

tests/erlang_tests/test_refc_binaries.erl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ start() ->
3434
ok = run_test(fun() -> test_heap_binary() end),
3535
ok = run_test(fun() -> test_const_binary() end),
3636
ok = run_test(fun() -> test_non_const_binary() end),
37+
ok = run_test(fun() -> test_refc_binaries() end),
3738
ok = run_test(fun() -> test_send() end),
3839
ok = run_test(fun() -> test_spawn() end),
3940
ok = run_test(fun() -> test_spawn_fun() end),
@@ -66,8 +67,22 @@ test_non_const_binary() ->
6667
id(Bin),
6768
ok.
6869

70+
test_refc_binaries() ->
71+
String = create_string(1024),
72+
Bins = [create_binary(String) || _ <- [x, x, x]],
73+
BinsInfo = [{1024, 1} || _ <- [x, x, x]],
74+
ok = verify_refc_binary_info(BinsInfo),
75+
MoreBins = [create_binary(String) || _ <- [x, x, x], _ <- [x, x, x], _ <- [x, x, x]],
76+
MoreBinsInfo = BinsInfo ++ [{1024, 1} || _ <- [x, x, x], _ <- [x, x, x], _ <- [x, x, x]],
77+
ok = verify_refc_binary_info(MoreBinsInfo),
78+
id(String),
79+
id(Bins),
80+
id(MoreBins),
81+
ok.
82+
6983
test_send() ->
7084
Bin = create_binary(1024),
85+
ok = verify_refc_binary_info([{1024, 1}]),
7186
Pid = erlang:spawn(fun() -> loop(#state{}) end),
7287
PidHeapSize0 = get_heap_size(Pid),
7388
%%
@@ -77,6 +92,7 @@ test_send() ->
7792
PidHeapSize1 = get_heap_size(Pid),
7893
true = PidHeapSize0 < PidHeapSize1,
7994
true = PidHeapSize1 < 1024,
95+
ok = verify_refc_binary_info([{1024, 2}]),
8096
%%
8197
%% Make sure we can get what we sent
8298
%%
@@ -160,6 +176,7 @@ loop(State) ->
160176
Pid ! {Ref, ok},
161177
loop(State#state{bin = Bin});
162178
{Pid, Ref, halt} ->
179+
erlang:garbage_collect(),
163180
Pid ! {Ref, ok}
164181
end.
165182

@@ -187,6 +204,7 @@ run_test(Fun) ->
187204
end.
188205

189206
execute(Pid, Fun) ->
207+
erlang:garbage_collect(),
190208
Result =
191209
try
192210
Fun(),
@@ -197,4 +215,13 @@ execute(Pid, Fun) ->
197215
end,
198216
Pid ! Result.
199217

218+
verify_refc_binary_info(Expected) ->
219+
case erlang:system_info(machine) of
220+
"BEAM" ->
221+
ok;
222+
_ ->
223+
Expected = erlang:system_info(refc_binary_info),
224+
ok
225+
end.
226+
200227
id(X) -> X.

0 commit comments

Comments
 (0)