Skip to content

Commit

Permalink
Remove all callback global roots.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Dec 29, 2024
1 parent a6093f5 commit 626297a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
14 changes: 9 additions & 5 deletions examples/decode.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let () = Printexc.record_backtrace true

let output_int chan n =
output_char chan (char_of_int ((n lsr 0) land 0xff));
output_char chan (char_of_int ((n lsr 8) land 0xff));
Expand Down Expand Up @@ -100,11 +102,13 @@ let process () =
try
Flac.Decoder.process dec;
Flac.Decoder.state dec
with Ogg.Not_enough_data -> (
try
fill ();
process ()
with Ogg.End_of_stream | Ogg.Not_enough_data -> `End_of_stream)
with
| Ogg.End_of_stream -> `End_of_stream
| Ogg.Not_enough_data -> (
try
fill ();
process ()
with Ogg.End_of_stream | Ogg.Not_enough_data -> `End_of_stream)
in
(process, info, meta)
in
Expand Down
33 changes: 16 additions & 17 deletions src/flac_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ CAMLprim value ocaml_flac_cleanup_decoder(value e) {
caml_remove_generational_global_root(&dec->callbacks.length_cb);
caml_remove_generational_global_root(&dec->callbacks.write_cb);
caml_remove_generational_global_root(&dec->callbacks.buffer);
caml_remove_generational_global_root(&dec->callbacks.output);

return Val_unit;
}
Expand Down Expand Up @@ -328,7 +329,10 @@ static FLAC__bool dec_eof_callback(const FLAC__StreamDecoder *decoder,
value ret = caml_callback(callbacks->eof_cb, Val_unit);
caml_release_runtime_system();

return ret == Val_true;
if (ret == Val_true)
return true;

return false;
}

FLAC__StreamDecoderReadStatus static dec_read_callback(
Expand Down Expand Up @@ -383,27 +387,16 @@ dec_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
ocaml_flac_register_thread();
caml_acquire_runtime_system();

value data = caml_alloc_tuple(channels);
caml_register_generational_global_root(&data);

int c, i;
for (c = 0; c < channels; c++) {
Store_field(data, c, caml_alloc(samples * Double_wosize, Double_array_tag));
Store_field(callbacks->output, c,
caml_alloc(samples * Double_wosize, Double_array_tag));
for (i = 0; i < samples; i++)
Store_double_field(Field(data, c), i,
Store_double_field(Field(callbacks->output, c), i,
sample_to_double(buffer[c][i], bps));
}

value ret = caml_callback_exn(callbacks->write_cb, data);
caml_remove_generational_global_root(&data);

if (Is_exception_result(ret)) {
ret = Extract_exception(ret);
caml_remove_generational_global_root(&data);
caml_raise(ret);
}

caml_remove_generational_global_root(&data);
caml_callback(callbacks->write_cb, callbacks->output);

caml_release_runtime_system();

Expand Down Expand Up @@ -449,6 +442,9 @@ CAMLprim value ocaml_flac_decoder_alloc_native(value seek, value tell,
dec->callbacks.buffer = caml_alloc_string(dec->callbacks.buflen);
caml_register_generational_global_root(&dec->callbacks.buffer);

dec->callbacks.output = Val_none;
caml_register_generational_global_root(&dec->callbacks.output);

dec->callbacks.info = NULL;
dec->callbacks.meta = NULL;

Expand All @@ -471,7 +467,7 @@ CAMLprim value ocaml_flac_decoder_alloc_bytecode(value *argv, int argn) {
CAMLprim value ocaml_flac_decoder_init(value _dec) {
CAMLparam1(_dec);

ocaml_flac_decoder *dec = Decoder_val(dec);
ocaml_flac_decoder *dec = Decoder_val(_dec);

// Intialize decoder
caml_release_runtime_system();
Expand All @@ -482,6 +478,9 @@ CAMLprim value ocaml_flac_decoder_init(value _dec) {
FLAC__stream_decoder_process_until_end_of_metadata(dec->decoder);
caml_acquire_runtime_system();

caml_modify_generational_global_root(
&dec->callbacks.output, caml_alloc_tuple(dec->callbacks.info->channels));

CAMLreturn(Val_unit);
}

Expand Down
1 change: 1 addition & 0 deletions src/flac_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct ocaml_flac_decoder_callbacks {
value length_cb;
value eof_cb;
value write_cb;
value output;
value buffer;
int buflen;
FLAC__StreamMetadata_StreamInfo *info;
Expand Down

0 comments on commit 626297a

Please sign in to comment.