NAME

ExtUtils::Typemaps::MagicExt - Typemap for storing objects in magic

VERSION

version 0.004

SYNOPSIS

use ExtUtils::Typemaps::MagicExt;
# First, read my own type maps:
my $private_map = ExtUtils::Typemaps->new(file => 'my.map');

# Then, get the Magic set and merge it into my maps
my $map = ExtUtils::Typemaps::MagicExt->new;
$private_map->merge(typemap => $map);

# Now, write the combined map to an output file
$private_map->write(file => 'typemap');

DESCRIPTION

ExtUtils::Typemaps::MagicExt is an ExtUtils::Typemaps subclass that stores the object just like T_MAGIC does, but additionally attaches a magic vtable (type MGVTBL) with the name ${type}_magic (e.g. Foo__Bar_magic for a value of type Foo::Bar) to the value. This is mainly useful for adding free (destruction) and dup (thread cloning) callbacks. The details of how these work is explained in perlguts, but it might look something like this:

static int object_dup(pTHX_ MAGIC* magic, CLONE_PARAMS* params) {
    object_refcount_increment((struct Object*)magic->mg_ptr);
    return 0;
}

static int object_free(pTHX_ SV* sv, MAGIC* magic) {
    object_refcount_decrement((struct Object*)magic->mg_ptr);
    return 0;
}

static const MGVTBL My__Object_magic = { NULL, NULL, NULL, NULL, object_free, NULL, object_dup, NULL };

This is useful to create objects that handle thread cloning correctly and effectively. If the object may be destructed by another thread, it should be allocated with the PerlSharedMem_malloc family of allocators.

DEPENDENCIES

This typemap requires ExtUtils::ParseXS 3.50 or higher as a build dependency.

On perls older than 5.14, this will require ppport.h to provide mg_findext.

AUTHOR

Leon Timmermans <leont@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.