[erlang-questions] Add a new data structure to erlang VM.

Constantin Kulikov zxnotdead@REDACTED
Sun Jul 10 12:10:44 CEST 2016


This seem to work
https://gist.github.com/Bad-ptr/3ea0d6d65ae18b5b68c6cb15a96fef8e .

However the documentation says that:
> All terms of type ERL_NIF_TERM belong to an environment of type
ErlNifEnv. The lifetime of a term is controlled by the lifetime of its
environment object.

> Variables of type ERL_NIF_TERM can refer to any Erlang term. This is an
opaque type and values of it can only by used either as arguments to API
functions or as return values from NIFs. All ERL_NIF_TERM's belong to an
environment (ErlNifEnv). A term can not be destructed individually, it is
valid until its environment is destructed.

> The environment is only valid in the thread where it was supplied as
argument until the NIF returns. It is thus useless and dangerous to store
pointers to process bound environments between NIF calls.

So, it is not clear to me is it safe to store an ERL_NIF_TERM(in a form as
it passed to a NIF function) in a datastructure created by a NIF function?
And does the GC aware that this terms must not be collected ? Etc.


On 9 July 2016 at 21:59, Steve Vinoski <vinoski@REDACTED> wrote:

>
>
> On Sat, Jul 9, 2016 at 1:54 PM, Constantin Kulikov <zxnotdead@REDACTED>
> wrote:
>
>> If I want to add a new datastructure to erlang(my local fork of the
>> erlang-otp from github), what should I do? Yes, I mean a C-level module
>> realization.
>>
>> Let's say I want to add a double linked list(just for simplicity) module
>> with an interface like that:
>> dlist:new() -> ?some king of reference to a list?
>> dlist:push(item) -> ?reference to a list?
>> dlist:pop() -> {item, ?reference to a list?}
>> etc...
>>
>> Is it possible at all? Has anyone ever tried to do something like this?
>> What definitions to what files should I add? How must I allocate my data
>> structure? How must I reference items in it?
>> Can someone point me to source locations where erlang's list/ets
>> table/tuple operations(creating a new, storing a reference to objects)
>> defined?
>>
>
> See
>
> http://erlang.org/doc/tutorial/nif.html
> http://erlang.org/doc/man/erl_nif.html
>
> --steve
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160710/1641078a/attachment.htm>


More information about the erlang-questions mailing list