NIF Query

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

NIF Query

John Duffy
Hi

I'm trying to follow the Interoperability Tutorial Users Guide (Version 9.0) to create a module containing some simple NIFS. I have managed to do this, however I have needed to digress from the documentation as follows...

1.)  if my module name doesn't match the filename in the call to erlang:load_nif("filename", 0) I get a compile error. This is not as per the example in the documentation, i.e. -module(complex6) & erlang:load_nif("complex6_nif, 0).

2.)  The appears to be a "flags" parameter missing in the {"foo", 1, foo_nif} structure in the documentation which generates a compile error...

static ErlNifFunc nif_funcs[] = {
    {"foo", 1, foo_nif},
    {"bar", 1, bar_nif}
};

I have changed this to that below, which compiles, but what is the nature/value of "flags"?

static ErlNifFunc nif_funcs[] = {
    {"foo", 1, foo_nif, 0},
    {"bar", 1, bar_nif, 0}
};


I may be missing something, or the Tutorial may need updating?

Kind regards

John

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: NIF Query

Steve Vinoski-2


On Fri, Sep 1, 2017 at 7:13 PM, John Duffy <[hidden email]> wrote:
Hi

I'm trying to follow the Interoperability Tutorial Users Guide (Version 9.0) to create a module containing some simple NIFS. I have managed to do this, however I have needed to digress from the documentation as follows...

1.)  if my module name doesn't match the filename in the call to erlang:load_nif("filename", 0) I get a compile error. This is not as per the example in the documentation, i.e. -module(complex6) & erlang:load_nif("complex6_nif, 0).

Can you supply the details of the error you're getting? I followed the tutorial on my Mac and had no problems.

2.)  The appears to be a "flags" parameter missing in the {"foo", 1, foo_nif} structure in the documentation which generates a compile error...

static ErlNifFunc nif_funcs[] = {
    {"foo", 1, foo_nif},
    {"bar", 1, bar_nif}
};

I have changed this to that below, which compiles, but what is the nature/value of "flags"?

static ErlNifFunc nif_funcs[] = {
    {"foo", 1, foo_nif, 0},
    {"bar", 1, bar_nif, 0}
};


I may be missing something, or the Tutorial may need updating?

The flags field is described in http://erlang.org/doc/man/erl_nif.html -- it's currently used for values specific to dirty NIFs, which are also described on that page. For a regular NIF like the one in the tutorial, flags should be 0. Since the nif_funcs variable in the tutorial is defined as static, the C compiler is guaranteed to initialize flags to 0 because an initial value for it isn't specified.

Whether you get a problem from the flags field or not depends on your compiler and the options you pass to it. For example, copying and pasting the complex6_nif.c from the tutorial into a file and compiling it as follows on my Mac produced no errors or warnings:

clang -o complex6_nif.so complex.c complex6_nif.c -I/usr/local/lib/erlang/usr/include -bundle -flat_namespace -undefined suppress

But adding the -Wmissing-field-initializers option results in warnings:

complex6_nif.c:27:23: warning: missing field 'flags' initializer [-Wmissing-field-initializers]
    {"foo", 1, foo_nif},
                      ^
complex6_nif.c:28:23: warning: missing field 'flags' initializer [-Wmissing-field-initializers]
    {"bar", 1, bar_nif}
                      ^
2 warnings generated.

--steve

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: NIF Query

Roger Lipscombe-2
In reply to this post by John Duffy
On 2 September 2017 at 00:13, John Duffy <[hidden email]> wrote:
> 1.)  if my module name doesn't match the filename in the call to
> erlang:load_nif("filename", 0) I get a compile error. This is not as per the
> example in the documentation, i.e. -module(complex6) &
> erlang:load_nif("complex6_nif, 0).

I don't see how you'd get a *compile* error. Could you clarify?

I *have* noticed that the name of the calling module (i.e. the .erl
file) must match the name of the NIF as provided in the first argument
to ERL_NIF_INIT. That doesn't appear to be documented anywhere. Also
note that Erlang will implicitly add the ".so" extension when you call
'erlang:load_nif'.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: NIF Query

John Duffy
In reply to this post by Steve Vinoski-2
Hi Steve

Thanks for you reply. Your help has answered my queries...

1.)  I had a typo in ERL_NIF_INIT(), the module name parameter was pointing to a NIF function name and not the module name.

2.)  I'm compiling with gcc on Debian. With my original compiler options "-Wall -W" the "-W" was generating the uninitialised structure field warning. Without the "-W" flag the warning is suppressed.

Many thanks for your help.

Kind regards

John
----Original message----
From : [hidden email]
Date : 02/09/2017 - 14:24 (BST)
To : [hidden email]
Cc : [hidden email]
Subject : Re: [erlang-questions] NIF Query



On Fri, Sep 1, 2017 at 7:13 PM, John Duffy <[hidden email]> wrote:
Hi

I'm trying to follow the Interoperability Tutorial Users Guide (Version 9.0) to create a module containing some simple NIFS. I have managed to do this, however I have needed to digress from the documentation as follows...

1.)  if my module name doesn't match the filename in the call to erlang:load_nif("filename", 0) I get a compile error. This is not as per the example in the documentation, i.e. -module(complex6) & erlang:load_nif("complex6_nif, 0).

Can you supply the details of the error you're getting? I followed the tutorial on my Mac and had no problems.

2.)  The appears to be a "flags" parameter missing in the {"foo", 1, foo_nif} structure in the documentation which generates a compile error...

static ErlNifFunc nif_funcs[] = {
    {"foo", 1, foo_nif},
    {"bar", 1, bar_nif}
};

I have changed this to that below, which compiles, but what is the nature/value of "flags"?

static ErlNifFunc nif_funcs[] = {
    {"foo", 1, foo_nif, 0},
    {"bar", 1, bar_nif, 0}
};


I may be missing something, or the Tutorial may need updating?

The flags field is described in http://erlang.org/doc/man/erl_nif.html -- it's currently used for values specific to dirty NIFs, which are also described on that page. For a regular NIF like the one in the tutorial, flags should be 0. Since the nif_funcs variable in the tutorial is defined as static, the C compiler is guaranteed to initialize flags to 0 because an initial value for it isn't specified.

Whether you get a problem from the flags field or not depends on your compiler and the options you pass to it. For example, copying and pasting the complex6_nif.c from the tutorial into a file and compiling it as follows on my Mac produced no errors or warnings:

clang -o complex6_nif.so complex.c complex6_nif.c -I/usr/local/lib/erlang/usr/include -bundle -flat_namespace -undefined suppress

But adding the -Wmissing-field-initializers option results in warnings:

complex6_nif.c:27:23: warning: missing field 'flags' initializer [-Wmissing-field-initializers]
    {"foo", 1, foo_nif},
                      ^
complex6_nif.c:28:23: warning: missing field 'flags' initializer [-Wmissing-field-initializers]
    {"bar", 1, bar_nif}
                      ^
2 warnings generated.

--steve



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: NIF Query

John Duffy
In reply to this post by Roger Lipscombe-2
Roger

Thanks for your reply. My issues were caused by not specifying the module name correctly in the first argument to ERL_NIF_INIT() and a gcc compiler option "-W" which was generating the uninitialised structure field warning.

Kind regards

John

----Original message----
From : [hidden email]
Date : 02/09/2017 - 20:17 (BST)
To : [hidden email]
Cc : [hidden email]
Subject : Re: [erlang-questions] NIF Query

On 2 September 2017 at 00:13, John Duffy <[hidden email]> wrote:
> 1.)  if my module name doesn't match the filename in the call to
> erlang:load_nif("filename", 0) I get a compile error. This is not as per the
> example in the documentation, i.e. -module(complex6) &
> erlang:load_nif("complex6_nif, 0).

I don't see how you'd get a *compile* error. Could you clarify?

I *have* noticed that the name of the calling module (i.e. the .erl
file) must match the name of the NIF as provided in the first argument
to ERL_NIF_INIT. That doesn't appear to be documented anywhere. Also
note that Erlang will implicitly add the ".so" extension when you call
'erlang:load_nif'.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions