Skip to content

Commit 6b80ab7

Browse files
authored
Merge pull request #1003 from jhawthorn/typed_data
Use TypedData APIs exclusively
2 parents 0772a8e + 64e4b6f commit 6b80ab7

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

ext/rugged/rugged.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,16 @@ static void cleanup_cb(void *unused)
336336
git_libgit2_shutdown();
337337
}
338338

339+
static const rb_data_type_t rugged_shutdown_hook_type = {
340+
.wrap_struct_name = "Rugged::ShutdownHook",
341+
.function = {
342+
.dmark = NULL,
343+
.dfree = cleanup_cb,
344+
.dsize = NULL,
345+
},
346+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
347+
};
348+
339349
void rugged_exception_raise(void)
340350
{
341351
VALUE err_klass, err_obj;
@@ -687,6 +697,7 @@ void Init_rugged(void)
687697

688698
/* Hook a global object to cleanup the library
689699
* on shutdown */
690-
rb_mShutdownHook = Data_Wrap_Struct(rb_cObject, NULL, &cleanup_cb, NULL);
700+
/* Non-NULL data so the GC actually invokes cleanup_cb on shutdown. */
701+
rb_mShutdownHook = TypedData_Wrap_Struct(rb_cObject, &rugged_shutdown_hook_type, (void *)1);
691702
rb_global_variable(&rb_mShutdownHook);
692703
}

ext/rugged/rugged_repo.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ static void rugged_repo_new_with_backend(git_repository **repo, VALUE rb_path, V
225225
rb_raise(rb_eRuggedError, "Backend must be an instance of Rugged::Backend");
226226
}
227227

228-
Data_Get_Struct(rb_backend, rugged_backend, backend);
228+
/* Backends are wrapped by external code with a type descriptor we don't
229+
* own, so we can't TypedData_Get_Struct against a known type. */
230+
backend = (rugged_backend *)RTYPEDDATA_DATA(rb_backend);
229231

230232
error = git_odb_new(&odb);
231233
if (error) goto cleanup;

0 commit comments

Comments
 (0)