Skip to content

Commit

Permalink
fsevents: move free to finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Oct 11, 2022
1 parent 6e5d04e commit d09b4f8
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/fsevents/fsevents_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,24 @@ void dune_fsevents_runloop_finalize(value v_runloop) {
}

static struct custom_operations dune_fsevents_runloop_ops = {
"dune.fsevents.runloop", dune_fsevents_runloop_finalize,
custom_compare_default, custom_hash_default,
custom_serialize_default, custom_deserialize_default,
custom_compare_ext_default, custom_fixed_length_default};
"dune.fsevents.runloop", dune_fsevents_runloop_finalize,
custom_compare_default, custom_hash_default,
custom_serialize_default, custom_deserialize_default,
custom_compare_ext_default, custom_fixed_length_default};

#define Fsevents_val(v) (*(dune_fsevents_t **)Data_custom_val(v))

static void dune_fsevents_t_finalize(value v_t) {
dune_fsevents_t *t = Fsevents_val(v_t);
caml_remove_global_root(&t->v_callback);
caml_stat_free(t);
}

static struct custom_operations dune_fsevents_t_ops = {
"dune.fsevents.fsevents_t", dune_fsevents_t_finalize,
custom_compare_default, custom_hash_default,
custom_serialize_default, custom_deserialize_default,
custom_compare_ext_default, custom_fixed_length_default};

CAMLprim value dune_fsevents_runloop_current(value v_unit) {
CAMLparam1(v_unit);
Expand Down Expand Up @@ -169,13 +183,16 @@ CAMLprim value dune_fsevents_create(value v_paths, value v_latency,
t->v_callback = v_callback;
t->stream = stream;

CAMLreturn(caml_copy_nativeint((intnat)t));
value v_ret = caml_alloc_custom(
&dune_fsevents_t_ops, sizeof(dune_fsevents_t *), 0, 1);
Fsevents_val(v_ret) = t;
CAMLreturn(v_ret);
}

CAMLprim value dune_fsevents_set_exclusion_paths(value v_t, value v_paths) {
CAMLparam2(v_t, v_paths);
CAMLlocal1(path);
dune_fsevents_t *t = (dune_fsevents_t *)Nativeint_val(v_t);
dune_fsevents_t *t = Fsevents_val(v_t);
CFMutableArrayRef paths = paths_of_list(v_paths);

bool ret = FSEventStreamSetExclusionPaths(t->stream, paths);
Expand All @@ -189,7 +206,7 @@ CAMLprim value dune_fsevents_set_exclusion_paths(value v_t, value v_paths) {

CAMLprim value dune_fsevents_start(value v_t, value v_runloop) {
CAMLparam2(v_t, v_runloop);
dune_fsevents_t *t = (dune_fsevents_t *)Nativeint_val(v_t);
dune_fsevents_t *t = Fsevents_val(v_t);
dune_runloop *runloop = Runloop_val(v_runloop);
t->runloop = runloop;
FSEventStreamScheduleWithRunLoop(t->stream, runloop->runloop,
Expand All @@ -204,19 +221,17 @@ CAMLprim value dune_fsevents_start(value v_t, value v_runloop) {

CAMLprim value dune_fsevents_stop(value v_t) {
CAMLparam1(v_t);
dune_fsevents_t *t = (dune_fsevents_t *)Nativeint_val(v_t);
dune_fsevents_t *t = Fsevents_val(v_t);
FSEventStreamStop(t->stream);
FSEventStreamInvalidate(t->stream);
FSEventStreamRelease(t->stream);
caml_remove_global_root(&t->v_callback);
caml_stat_free(t);
CAMLreturn(Val_unit);
}

CAMLprim value dune_fsevents_runloop_get(value v_t) {
CAMLparam1(v_t);
CAMLlocal2(v_some, v_runloop);
dune_fsevents_t *t = (dune_fsevents_t *)Nativeint_val(v_t);
dune_fsevents_t *t = Fsevents_val(v_t);
if (t->runloop == NULL) {
CAMLreturn(Val_int(0));
} else {
Expand All @@ -229,7 +244,7 @@ CAMLprim value dune_fsevents_runloop_get(value v_t) {

CAMLprim value dune_fsevents_flush_async(value v_t) {
CAMLparam1(v_t);
dune_fsevents_t *t = (dune_fsevents_t *)Nativeint_val(v_t);
dune_fsevents_t *t = Fsevents_val(v_t);
CAMLlocal1(v_event);
uint64_t id = FSEventStreamFlushAsync(t->stream);
v_event = caml_copy_int64(id);
Expand All @@ -238,7 +253,7 @@ CAMLprim value dune_fsevents_flush_async(value v_t) {

CAMLprim value dune_fsevents_flush_sync(value v_t) {
CAMLparam1(v_t);
dune_fsevents_t *t = (dune_fsevents_t *)Nativeint_val(v_t);
dune_fsevents_t *t = Fsevents_val(v_t);
caml_release_runtime_system();
FSEventStreamFlushSync(t->stream);
caml_acquire_runtime_system();
Expand Down

0 comments on commit d09b4f8

Please sign in to comment.