Re: Is there anyway to share NIF resource type among NIF modules?
The reasonable way would be to use a ref-counted pointer to struct which will be shared by both NIF modules. I believe it must also be allocated somewhere in VM memory in case both NIF modules get unloaded. As C is not particularly famous for its smart refc pointers, you will have to find a library or do it yourself, and hook the refc check onto both NIF resource free functions.
I have a C struct, as a NIF resource type, and want it to be shared between several NIF modules, is it possible?
There is no offical documented support to use the same resource type from more than one NIF module.
But if you want to experiment, there are two problems I can think of.
1. How to get the other NIF modules access to the resource type pointer.
One way to solve this is to put the resource type in a separate dynamic shared library, dynamically linked by all NIF modules using the resource type.
2. Prevent usage of an unloaded resource type.
You must make sure yourself that other modules do not try to use the pointer to an unloaded resource type (as argument to enif_get_resource or enif_make_resource for example). That will probably lead to VM crash or other unpleasantries.
A resource type will exists as long as the NIF module that called enif_open_resource_type is loaded. If the resource type has a destructor function then the NIF module will not be unloaded until the last resource object is garbage collected.