[erlang-questions] Determine if a binary is ref-counted in port driver code

Gustav Simonsson <>
Fri May 13 10:57:03 CEST 2011

>From http://www.erlang.org/doc/man/erl_driver.html

"The ErlDrvBinary structure is a binary, as sent between the emulator and the driver. All binaries are reference counted"

Also, this and the following functions might be of use:

Best Regards,
Gustav Simonsson

----- Original Message -----
From: "Tim Watson" <>
Sent: Thursday, May 12, 2011 10:20:42 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna
Subject: [erlang-questions] Determine if a binary is ref-counted in port driver code

In a port driver I'm working on, I pass an iolist to driver_outputv
and on encountering a binary, need to know whether it is a ref-counted
binary or not (i.e. a so-called heap binary). The location of the
action data in the ErlIOVec depends on this apparently. I've seen some
anecdotal evidence (on the mailing list) to suggest that if the binary
is > 64 bytes it will be ref-counted, but I want to know if there is
an API (or perhaps a macro in the OTP source tree) that I can use to
determine this?

Currently I'm using a hack-around like so:

    if (hsize->xsl_size < 64) {
        data = &ev->iov[++bin_idx].iov_base[0];
    } else {
        data = ev->binv[++bin_idx]->orig_bytes;

This code (taken from
https://github.com/hyperthunk/erlxsl/blob/master/c_src/erlxsl.c) seems
to work, but I'm fairly sure it's not The Right Thing To Do (TM).

Any pointers to better solutions would be much appreciated.


erlang-questions mailing list


More information about the erlang-questions mailing list