[erlang-questions] mmap file to binary

Max Lapshin <>
Fri Apr 9 12:32:48 CEST 2010


On Fri, Apr 9, 2010 at 2:23 PM, Angel Alvarez <> wrote:
>
> Maybe a new alloc for mmaped files will allow manage diferences among OSes in a easy way...
>
> http://www.erlang.org/doc/man/erts_alloc.html
>
> It seems that the mmaped case is already covered using the  mseg_alloc:
>
> mseg_alloc
>

Maybe it will be good to dig inside this. However, there seems to be easy way:

erl_binary.h has erts_create_magic_binary. It should be changed to:

ERTS_GLB_INLINE Binary *
erts_create_mmap_binary(size_t size, int fd)
{
    Uint bsize = ERTS_MAGIC_BIN_SIZE(0);
    Binary* bptr = erts_alloc_fnf(ERTS_ALC_T_BINARY, 0);
    if (!bptr)
	erts_alloc_n_enomem(ERTS_ALC_T2N(ERTS_ALC_T_BINARY), bsize);
    ERTS_CHK_BIN_ALIGNMENT(bptr);
    bptr->flags = BIN_FLAG_MAGIC;
    bptr->orig_size = ERTS_MAGIC_BIN_ORIG_SIZE(size);
    bptr->orig_bytes = mmap(0, size, PROT_READ, MAP_FILE, fd, 0);
    erts_refc_init(&bptr->refc, 0);
    ERTS_MAGIC_BIN_DESTRUCTOR(bptr) = binary_mmap_destructor;
    return bptr;
}

static void binary_mmap_destructor(Binary *bptr)
{
  munmap(bptr->orig_bytes, bptr->orig_size);
}


More information about the erlang-questions mailing list