[erlang-patches] ei - decode functions without memory copy

Joseph Wayne Norton <>
Wed Oct 26 14:36:04 CEST 2011


I wanted to ask if there is interest in adding new decode functions to the ei application that do not perform any memory copying.

The purpose is to avoid unnecessary memory copy by the ei functions themselves and to avoid unnecessary memory allocation by the decode function callers.

Here are two sample implementations - one for atoms and one for binaries.

I could prepare an git-based patch if someone can provide some feedback for additional functions that should be included and for a better naming convention for these new functions.

thanks,

- Joe N.



// This function inspects an atom from the binary format.  The p
// parameter is the name of the atom and the name should be
// zero-terminated.  If the name is equal to the atom in binary
// format, returns 0.  Otherwise, return -1.  If name is NULL, no
// comparison is done and returns 0.

int
ei_inspect_atom(const char *buf, int *index, char *p)
{
    const char *s = buf + *index;
    const char *s0 = s;
    int len;

    if (get8(s) != ERL_ATOM_EXT) return -1;

    len = get16be(s);

    if (len > MAXATOMLEN) return -1;

    if (p) {
        if (len != (int) strlen(p)) return -1;
        if (memcmp(p, s, len)) return -1;
    }
    s += len;
    *index += s-s0;

    return 0;
}

// This function inspects a binary from the binary format. The p
// parameter is set to the address of the binary.  The len parameter
// is set to the actual size of the binary.

int
ei_inspect_binary(const char *buf, int *index, void **p, long *lenp)
{
    const char *s = buf + *index;
    const char *s0 = s;
    long len;

    if (get8(s) != ERL_BINARY_EXT) return -1;

    len = get32be(s);
    if (p) *p = (void*) s;
    s += len;

    if (lenp) *lenp = len;
    *index += s-s0;

    return 0;
}


Joseph Wayne Norton




Joseph Wayne Norton






More information about the erlang-patches mailing list