Last active
April 27, 2019 04:58
-
-
Save Aphoh/4461a989dfb7ad2bca83bbefe29d11d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Feast your eyes on the worst pointer chasing logic I have ever seen. | |
static simulation_context* ctx_from_term(ErlNifEnv *env, ERL_NIF_TERM term) { | |
/* This may seem crazy, but here's what's going on here. | |
* I'm trying to get a simulation_context*, which is a resource | |
* my application is using. The function enif_get_resource accepts a void** ptr such that *ptr | |
* will point to my resource, so **ptr will be my simulation_context* | |
*/ | |
// We can't make a simulation_context*** as then it will try to write the pointer-pointer to that location | |
// and it will segfault in our face, so first we need to alloc the pointer to the simulation_context* | |
// which enif_get_resource will write to: | |
simulation_context** context_res_ptr_ptr; | |
// Then we make our call | |
enif_get_resource(env, term, SIM_CONTEXT_TYPE, (void**) &context_res_ptr_ptr); // | |
// context_res_ptr_ptr now points to our simulation_context* | |
simulation_context *context = *(context_res_ptr_ptr); | |
return context; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment