Skip to content

Commit

Permalink
Undefine allocation function for C extension class
Browse files Browse the repository at this point in the history
* Undefine allocation function for C extension class

Since Ruby 3.2 a new warning is printed when a Ruby class created in a C
extension does not specify an allocate function or undefine it.

```
/gem/lib/appsignal/transaction.rb:98: warning: undefining the allocator of T_DATA class Appsignal::Extension::Transaction
/gem/lib/appsignal/utils/data.rb:19: warning: undefining the allocator of T_DATA class Appsignal::Extension::Data
```

From my understanding, we only need to define an allocate function if
the class uses a C struct and stores any values on it. Our classes don't
do that in C, that's done in our Rust extension.

Closes #909

## Resources

https://bugs.ruby-lang.org/issues/18007
https://github.com/ruby/ruby/blob/6963f8f743b42f9004a0879cd66c550f18987352/doc/extension.rdoc#label-Write+the+C+Code
https://ruby-doc.org/core-3.1.1/doc/extension_rdoc.html#label-C+struct+to+Ruby+object

rails-sqlserver/tiny_tds#515
https://groups.google.com/g/sequel-talk/c/K0J80s4wGJU/m/BT-6FFhrAgAJ

## Other MR doing similar change
vmg/redcarpet#721
appsignal/appsignal-ruby#917
  • Loading branch information
stoivo authored and joast committed Dec 7, 2023
1 parent f5df69c commit a6a0f2e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

== 0.8.0 [unreleased]

* Remove Warning 'warning: undefining the allocator of T_DATA class FileMagic'

* Required Ruby version >= 3.0.0.

* Separate error messages for missing header and library in extconf.rb
Expand Down
2 changes: 2 additions & 0 deletions ext/filemagic/filemagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ void
Init_ruby_filemagic() {
char version[16] = "0";
cFileMagic = rb_define_class("FileMagic", rb_cObject);
rb_undef_alloc_func(cFileMagic);

#if defined(FILE_VERSION_MAJOR)
RB_MAGIC_SET_VERSION(FILE_VERSION_MAJOR, patchlevel)
Expand Down Expand Up @@ -240,6 +241,7 @@ Init_ruby_filemagic() {
rb_alias(cFileMagic, rb_intern("valid?"), rb_intern("check"));

rb_FileMagicError = rb_define_class_under(cFileMagic, "FileMagicError", rb_eStandardError);
rb_undef_alloc_func(rb_FileMagicError);

#ifdef MAGIC_NONE
rb_define_const(cFileMagic, "MAGIC_NONE", INT2FIX(MAGIC_NONE));
Expand Down
2 changes: 2 additions & 0 deletions test/mahoro.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ mahoro_load(self, path)
void Init_mahoro(void)
{
cMahoro = rb_define_class("Mahoro", rb_cObject);
rb_undef_alloc_func(cMahoro);
eMahoroError = rb_define_class_under(cMahoro, "Error", rb_eStandardError);
rb_undef_alloc_func(eMahoroError);

rb_const_set(cMahoro, rb_intern("NONE"), INT2FIX(MAGIC_NONE));
rb_const_set(cMahoro, rb_intern("DEBUG"), INT2FIX(MAGIC_DEBUG));
Expand Down

0 comments on commit a6a0f2e

Please sign in to comment.