Erlang logo
User's Guide
Reference Manual
Release Notes
PDF
Top

Erlang Interface
Reference Manual
Version 3.8


Expand All
Contract All

Table of Contents

erl_malloc

C LIBRARY

erl_malloc

LIBRARY SUMMARY

Memory Allocation Functions

DESCRIPTION

This module provides functions for allocating and deallocating memory.

EXPORTS

ETERM *erl_alloc_eterm(etype)

Types:

unsigned char etype;

This function allocates an (ETERM) structure. Specify etype as one of the following constants:

  • ERL_INTEGER

  • ERL_U_INTEGER /* unsigned integer */

  • ERL_ATOM

  • ERL_PID /* Erlang process identifier */

  • ERL_PORT

  • ERL_REF /* Erlang reference */

  • ERL_LIST

  • ERL_EMPTY_LIST

  • ERL_TUPLE

  • ERL_BINARY

  • ERL_FLOAT

  • ERL_VARIABLE

  • ERL_SMALL_BIG /* bignum */

  • ERL_U_SMALL_BIG /* bignum */

ERL_SMALL_BIG and ERL_U_SMALL_BIG are for creating Erlang bignums, which can contain integers of arbitrary size. The size of an integer in Erlang is machine dependent, but in general any integer larger than 2^28 requires a bignum.

void erl_eterm_release(void)

Clears the freelist, where blocks are placed when they are released by erl_free_term() and erl_free_compound().

void erl_eterm_statistics(allocated, freed)

Types:

long *allocated;
long *freed;

allocated and freed are initialized to contain information about the fix-allocator used to allocate ETERM components. allocated is the number of blocks currently allocated to ETERM objects. freed is the length of the freelist, where blocks are placed when they are released by erl_free_term() and erl_free_compound().

void erl_free_array(array, size)

Types:

ETERM **array;
int size;

This function frees an array of Erlang terms.

array is an array of ETERM* objects.

size is the number of terms in the array.

void erl_free_term(t)

Types:

ETERM *t;

Use this function to free an Erlang term.

void erl_free_compound(t)

Types:

ETERM *t;

Normally it is the programmer's responsibility to free each Erlang term that has been returned from any of the erl_interface functions. However since many of the functions that build new Erlang terms in fact share objects with other existing terms, it may be difficult for the programmer to maintain pointers to all such terms in order to free them individually.

erl_free_compound() will recursively free all of the sub-terms associated with a given Erlang term, regardless of whether we are still holding pointers to the sub-terms.

There is an example in the User Manual under "Building Terms and Patterns"

void erl_malloc(size)

Types:

long size;

This function calls the standard malloc() function.

void erl_free(ptr)

Types:

void *ptr;

This function calls the standard free() function.