erlang-nif

Allocating memory in Erlang C NIF

匆匆过客 提交于 2019-12-22 23:12:22
问题 Why would one use void *enif_alloc_resource(ErlNifResourceType* type, unsigned size) as opposed to void *enif_alloc(size_t size) when trying to allocate memory from an Erlang C NIF? Reference does not specify much as to why. http://www.erlang.org/doc/man/erl_nif.html#enif_alloc 回答1: enif_alloc_resource is used to create resources which are garbage collected by the vm when not used any more. enif_alloc works just like malloc, only is uses an Erlang VM specific implementation rather than the

Erlang: Interfacing with Xalan: port driver or nif?

左心房为你撑大大i 提交于 2019-12-12 13:53:15
问题 I'd like to get a real XSLT processor working with erlang. Which would be the best interface, nif or port driver? According to the nif documentation, nif calls block the runtime, so they should not take long. Is processing a long xml document too long? Also, I'd like to allow erlang callbacks during the transformation. Does that seem possible? Possible with nif but not port drivers or vice versa? I've never written any C, so I figured this would be good introduction. Xalan is C++. I assume

Erlang, pass an nif object between functions

喜欢而已 提交于 2019-12-10 08:42:54
问题 I write a C nif code and in function new , it creates a stack struct with enif_alloc_resource and returns that. when i use function enif_make_resources , it always returns <<>> in erlang. Here is my C code: #include "erl_nif.h" static ErlNifResourceType *MEM_RESOURCE; typedef struct { int n; int ps; int pe; ERL_NIF_TERM *data; } Stack; Stack *new(ErlNifEnv *env, int size) { Stack *s = (Stack*) enif_alloc_resource(MEM_RESOURCE, sizeof(Stack)); s->n = size; s->ps = 0; s->pe = size - 1; s->data

erlang rebar escriptize & nifs

China☆狼群 提交于 2019-12-06 21:38:26
问题 I can use nif's if I write the escript myself, however when I use rebar escriptize the nif functions cannot be found. I think it is because *.so objects are not getting packed like beam files. Here is an simple example; rebar.config : {deps, [ {'jiffy', "", {git, "https://github.com/davisp/jiffy.git", {branch, master}}} ]}. {escript_incl_apps, [jiffy]}. %% I tried this to see what happens if the so got in there but didn't help {escript_incl_extra, [{"deps/jiffy/priv/jiffy.so", "/path/to/my

Allocating memory in Erlang C NIF

给你一囗甜甜゛ 提交于 2019-12-06 05:29:23
Why would one use void *enif_alloc_resource(ErlNifResourceType* type, unsigned size) as opposed to void *enif_alloc(size_t size) when trying to allocate memory from an Erlang C NIF? Reference does not specify much as to why. http://www.erlang.org/doc/man/erl_nif.html#enif_alloc enif_alloc_resource is used to create resources which are garbage collected by the vm when not used any more. enif_alloc works just like malloc, only is uses an Erlang VM specific implementation rather than the OSs malloc. Take a look at the documentation for ErlNifResourceType and the functions which use it for some

Erlang, pass an nif object between functions

[亡魂溺海] 提交于 2019-12-05 15:26:57
I write a C nif code and in function new , it creates a stack struct with enif_alloc_resource and returns that. when i use function enif_make_resources , it always returns <<>> in erlang. Here is my C code: #include "erl_nif.h" static ErlNifResourceType *MEM_RESOURCE; typedef struct { int n; int ps; int pe; ERL_NIF_TERM *data; } Stack; Stack *new(ErlNifEnv *env, int size) { Stack *s = (Stack*) enif_alloc_resource(MEM_RESOURCE, sizeof(Stack)); s->n = size; s->ps = 0; s->pe = size - 1; s->data = enif_alloc(sizeof(ERL_NIF_TERM) * size); return s; } static ERL_NIF_TERM new_nif(ErlNifEnv* env, int

erlang rebar escriptize & nifs

寵の児 提交于 2019-12-05 02:31:12
I can use nif's if I write the escript myself, however when I use rebar escriptize the nif functions cannot be found. I think it is because *.so objects are not getting packed like beam files. Here is an simple example; rebar.config : {deps, [ {'jiffy', "", {git, "https://github.com/davisp/jiffy.git", {branch, master}}} ]}. {escript_incl_apps, [jiffy]}. %% I tried this to see what happens if the so got in there but didn't help {escript_incl_extra, [{"deps/jiffy/priv/jiffy.so", "/path/to/my/proj"}]}. test.erl : -module(test). -export([main/1]). main(_Args) -> jiffy:decode(<<"1">>), ok. rebar

Erlang NIF Test — OS X Lion

▼魔方 西西 提交于 2019-12-03 13:18:59
问题 I'm trying to compile the NIF Test from Erlang (http://www.erlang.org/doc/man/erl_nif.html) on Mac OS X Lion. I can't get it to compile. Am I missing a compiler flag? Here's the error I get: Computer:~ me $ gcc -fPIC -shared -o niftest.so niftest.c -I /usr/local/Cellar/erlang/R14B02/lib/erlang/usr/include/ Undefined symbols for architecture x86_64: "_enif_make_string", referenced from: _hello in ccXfh0oG.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status I