Driver question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Driver question

Sean Hinde-2
Hi,

I want to do someting like this:

Allocate some storage in my driver, pass the data to erlang, and then, if
the data goes out of scope at an erlang level have the storage automatically
freed out of the driver by garbage collection.

Maybe driver binaries exhibit this effect already? Or do they need to be
freed in the driver as well as Erlang?

There is a part of the inet_drv where the refc value of the binary is
checked to see if anyone else still needs it, which then frees it if no-one
else does, but this still implies some cleanup routine run regularly in the
driver.

Thoughts anyone? Is it a sensible thing to try to do?

Thanks,
Sean



NOTICE AND DISCLAIMER:
This email (including attachments) is confidential.  If you have received
this email in error please notify the sender immediately and delete this
email from your system without copying or disseminating it or placing any
reliance upon its contents.  We cannot accept liability for any breaches of
confidence arising through use of email.  Any opinions expressed in this
email (including attachments) are those of the author and do not necessarily
reflect our opinions.  We will not accept responsibility for any commitments
made by our employees outside the scope of our business.  We do not warrant
the accuracy or completeness of such information.




Reply | Threaded
Open this post in threaded view
|

Driver question

Raimo Niskanen-3
Sean Hinde wrote:

>
> Hi,
>
> I want to do someting like this:
>
> Allocate some storage in my driver, pass the data to erlang, and then, if
> the data goes out of scope at an erlang level have the storage automatically
> freed out of the driver by garbage collection.
>
> Maybe driver binaries exhibit this effect already? Or do they need to be
> freed in the driver as well as Erlang?
>

Yes they do. They might be exactly what you need. The driver binaries
are reference counted, and when you call driver_allocate_binary(),
refcnt is set to 1 (only the driver is holding the binary).

When you later call driver_output_binary(), refcnt is incremented to 2,
and note that the driver must not change the contents from now on, since
erlang processes now can see the data and depend on this. The driver
migth as well call driver_free_binary(), which just decrements refcnt.
Only when refcnt decrements to 0, the binary is actually freed.

Every erlang process that now receives the binary will increment refcnt,
and when the binary is garbage collected and found to be unused refcnt
is decremented and finaly freed by the last erlang processe that garbage
collects it.

A driver that implements the outputv() callback receives binaries in an
ErlIOVec. When it does not need the data anymore, driver_free_binary
must be used to free the binaries, or if driver_enq(), driver_deq() and
related (the queue interface) are used, they take care of the refcnt and
freeing.

> There is a part of the inet_drv where the refc value of the binary is
> checked to see if anyone else still needs it, which then frees it if no-one
> else does, but this still implies some cleanup routine run regularly in the
> driver.
>

I guess that the trick in inet_drv is about reusing a buffer that has
not been sent to erlang, instead of actually freeing the memory and then
allocating a new buffer, which is rather expensive.

/ Raimo Niskanen, Erlang/OPT, Ericsson UAB


Reply | Threaded
Open this post in threaded view
|

Driver question

Martin Carlson-2
In reply to this post by Sean Hinde-2
Hello, I have two questions.
    I would like to do sone parsing of data. What is the best way to do this? I
have an integer and I would like to make sure that is is a particular length
and that it is comprised entirly of digits. I have looked at io_lib and a
number of other spots but havent really found anything too elegant.  Everything
seems to involve converting to strings or using large function gaurds. If this
is the way to go I will, but if anyone has a better idea I would like to know.
I am always parsing packets in erlang so any info on that in general would be
nice.

    I have an application running that emails the interested parties when it
goes down. I have accomplished this in a way that I think is rather kludgy. I
use a linked process that traps exit signals from my main supervisor. I was
wondering if there is a call back that is called during the shutdown sequence
of the app that I could use to make sure that the email is sent on the death of
the app.

    If anyone has nice solutions to either of these problems please let me
know.

       Cheers,
        Martin