


MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



NAME
     malloc, free, realloc, calloc, cfree, memalign, valloc, mal-
     locmap,   mallopt,  mallinfo,  malloc_debug,  malloc_verify,
     alloca - memory allocator

SYNOPSIS
     #include <malloc.h>

     char *malloc(size)
     unsigned size;

     int free(ptr)
     char *ptr;

     char *realloc(ptr, size)
     char *ptr;
     unsigned size;

     char *calloc(nelem, elsize)
     unsigned nelem, elsize;

     int cfree(ptr)
     char *ptr;

     char *memalign(alignment, size)
     unsigned alignment;
     unsigned size;

     char *valloc(size)
     unsigned size;

     void mallocmap()

     int mallopt(cmd, value)
     int cmd, value;

     struct mallinfo mallinfo()

     #include <alloca.h>

     char *alloca(size)
     int size;

SYSTEM V SYNOPSIS
     #include <malloc.h>

     void *malloc(size)
     size_t size;

     void free(ptr)
     void *ptr;




Sun Release 4.1   Last change: 24 January 1990                  1






MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



     void *realloc(ptr, size)
     void *ptr;
     size_t size;

     void *calloc(nelem, elsize)
     size_t nelem;
     size_t elsize;

     void *memalign(alignment, size)
     size_t alignment;
     size_t size;

     void *valloc(size)
     size_t size;

     The XPG2 versions of the functions listed  in  this  section
     are  declared  as they are in SYNOPSIS above, except free(),
     which is declared as:

     void free(ptr)
     char *ptr;

DESCRIPTION
     These routines provide a general-purpose  memory  allocation
     package.  They maintain a table of free blocks for efficient
     allocation and coalescing of free storage.  When there is no
     suitable  space  already  free, the allocation routines call
     sbrk() (see brk(2)) to get more memory from the system.

     Each of the allocation routines returns a pointer  to  space
     suitably  aligned  for  storage  of any type of object. Each
     returns a NULL pointer if the request  cannot  be  completed
     (see DIAGNOSTICS).

     malloc() returns a pointer to  a  block  of  at  least  _s_i_z_e
     bytes, which is appropriately aligned.

     free() releases a previously allocated block.  Its  argument
     is  a  pointer  to a block previously allocated by malloc(),
     calloc(), realloc(), malloc(), or memalign().

     realloc() changes the size of the block referenced by _p_t_r to
     _s_i_z_e  bytes  and  returns  a pointer to the (possibly moved)
     block.  The contents will be unchanged up to the  lesser  of
     the  new  and  old sizes.  If unable to honor a reallocation
     request, realloc() leaves its first argument unaltered.  For
     backwards  compatibility,  realloc()  accepts a pointer to a
     block freed since the most recent  call  to  malloc(),  cal-
     loc(),  realloc(),  valloc(),  or  memalign().   Note: using
     realloc() with a block freed _b_e_f_o_r_e the most recent call  to
     malloc(), calloc(), realloc(), valloc(), or memalign() is an
     error.



Sun Release 4.1   Last change: 24 January 1990                  2






MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



     calloc() uses malloc() to allocate space  for  an  array  of
     _n_e_l_e_m  elements  of  size  _e_l_s_i_z_e,  initializes the space to
     zeros, and returns a pointer to the initialized block.   The
     block can be freed with free() or cfree().

     memalign() allocates _s_i_z_e bytes  on  a  specified  alignment
     boundary, and returns a pointer to the allocated block.  The
     value of the returned address is guaranteed to  be  an  even
     multiple of _a_l_i_g_n_m_e_n_t.  Note: the value of _a_l_i_g_n_m_e_n_t must be
     a power of two, and must be greater than  or  equal  to  the
     size of a word.

     valloc(_s_i_z_e) is equivalent to memalign(getpagesize(), _s_i_z_e).

     mallocmap() prints a map of the heap to the standard output.
     mallocmap() prints each block's address, size (in bytes) and
     status (free or busy).  A block must have a size that is  no
     larger than the current extent of the heap.

     mallopt() allows quick allocation of small blocks of memory.
     mallopt()  tells  subsequent  calls  to malloc() to allocate
     _h_o_l_d_i_n_g _b_l_o_c_k_s containing small blocks.   Under  this  small
     block  algorithm, a request to malloc() for a small block of
     memory returns a pointer to one of the  pre-allocated  small
     blocks.   Different holding blocks are created as needed for
     different sizes of small blocks.

     _c_m_d  may  be  one  of  the  following  values,  defined   in
     <malloc.h>:

     M_MXFAST       Set the maximum size of blocks  to  be  allo-
                    cated  using  the small block algorithm (_m_a_x_-
                    _f_a_s_t) to _v_a_l_u_e.  The algorithm allocates  all
                    blocks  smaller  than _m_a_x_f_a_s_t in large groups
                    and then doles them out very  quickly.   Ini-
                    tially,  _m_a_x_f_a_s_t  is  0  and  the small block
                    algorithm is disabled.

     M_NLBLKS       Set the number of small blocks in  a  holding
                    block   (_n_u_m_l_b_l_k_s)  to  _v_a_l_u_e.   The  holding
                    blocks each contain _n_u_m_l_b_l_k_s blocks. _n_u_m_l_b_l_k_s
                    must  be  greater  than 1.  The default value
                    for _n_u_m_l_b_l_k_s is 100.

     M_GRAIN        Set the granularity for small block  requests
                    (_g_r_a_i_n)  to  _v_a_l_u_e.   The sizes of all blocks
                    smaller than _m_a_x_f_a_s_t are rounded  up  to  the
                    nearest  multiple  of  _g_r_a_i_n.   _g_r_a_i_n must be
                    greater than 0.  The default value  of  _g_r_a_i_n
                    is  the  smallest  number of bytes which will
                    allow alignment of any data type.  When _g_r_a_i_n
                    is  set, _v_a_l_u_e is rounded up to a multiple of



Sun Release 4.1   Last change: 24 January 1990                  3






MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



                    this default.

     M_KEEP         Preserve data in a freed block until the next
                    malloc(),   realloc(),   or  calloc().   This
                    option is  provided  only  for  compatibility
                    with  the  old version of malloc() and is not
                    recommended.

     mallopt() may be called repeatedly, but may  not  be  called
     after the first small block is allocated.

     mallinfo() can be used during program development to  deter-
     mine  the best settings for the parameters set by mallopt().
     Do not call mallinfo() until after a call to malloc().  mal-
     linfo()  provides  information  describing  space usage.  It
     returns a mallinfo structure, defined in <malloc.h> as:

          struct mallinfo {
               int arena;     /* total space in arena */
               int ordblks;   /* number of ordinary blocks */
               int smblks;    /* number of small blocks */
               int hblks;     /* number of holding blocks */
               int hblkhd;    /* space in holding block headers */
               int usmblks;   /* space in small blocks in use */
               int fsmblks;   /* space in free small blocks */
               int uordblks;  /* space in ordinary blocks in use */
               int fordblks;  /* space in free ordinary blocks */
               int keepcost;  /* cost of enabling keep option */

               int mxfast;    /* max size of small blocks */
               int nlblks;    /* number of small blocks in a holding block */
               int grain;     /* small block rounding factor */
               int uordbytes; /* space (including overhead) allocated in ord. blks */
               int allocated; /* number of ordinary blocks allocated */
               int treeoverhead;   /* bytes used in maintaining the free tree */
          };

     alloca() allocates _s_i_z_e bytes of space in the stack frame of
     the  caller,  and  returns a pointer to the allocated block.
     This temporary space is automatically freed when the  caller
     returns.   Note  that  if  the allocated block is beyond the
     current stack limit, the resulting behavior is undefined.

     malloc(), realloc(), memalign() and valloc() return  a  non-
     NULL  pointer  if _s_i_z_e is 0, and calloc() returns a non-NULL
     pointer if _n_e_l_e_m or _e_l_s_i_z_e is 0, but these  pointers  should
     _n_o_t be dereferenced.

     Note: Always cast the value returned by malloc(), realloc(),
     calloc(), memalign(), valloc() or alloca().





Sun Release 4.1   Last change: 24 January 1990                  4






MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



SYSTEM V DESCRIPTION
     The XPG2 versions of  malloc(),  realloc(),  memalign()  and
     valloc() return NULL if _s_i_z_e is 0.  The XPG2 version of cal-
     loc() returns NULL if _n_e_l_e_m or _e_l_s_i_z_e is 0.

RETURN VALUES
     On success, malloc(), calloc(), realloc(), memalign(),  val-
     loc()  and  alloca()  return  a  pointer  to  space suitably
     aligned for storage of any type of object.  On failure, they
     return NULL.

     free() and cfree() return:

     1    on success.

     0    on failure and set errno to indicate the error.

     mallopt() returns 0 on  success.   If  mallopt()  is  called
     after the allocation of a small block, or if _c_m_d or _v_a_l_u_e is
     invalid, it returns a non-zero value.

     mallinfo() returns a struct mallinfo.

SYSTEM V RETURN VALUES
     If _s_i_z_e is 0, the  XPG2  versions  of  malloc(),  realloc(),
     memalign() and valloc() return NULL.

     If _n_e_l_e_m or _e_l_s_i_z_e  is  0,  the  XPG2  version  of  calloc()
     returns NULL.

     free() does not return a value.

ERRORS
     malloc(),   calloc(),   realloc(),   valloc(),   memalign(),
     cfree(),  and  free()  will  each fail if one or more of the
     following are true:

     EINVAL         An invalid argument was specified.

                    The value of _p_t_r passed to  free(),  cfree(),
                    or  realloc()  was  not  a pointer to a block
                    previously allocated by  malloc(),  calloc(),
                    realloc(), valloc(), or memalign().

                    The allocation heap is  found  to  have  been
                    corrupted.   More detailed information may be
                    obtained  by  enabling  range  checks   using
                    malloc_debug().

     ENOMEM         _s_i_z_e bytes of memory could not be allocated.





Sun Release 4.1   Last change: 24 January 1990                  5






MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



FILES
     /usr/lib/debug/malloc.o  diagnostic  versions  of   malloc()
                              routines.
     /usr/lib/debug/mallocmap.o
                              routines to  print  a  map  of  the
                              heap.

SEE ALSO
     csh(1), ld(1), brk(2), getrlimit(2), sigvec(2), sigstack(2)

     Stephenson, C.J., _F_a_s_t _F_i_t_s, in _P_r_o_c_e_e_d_i_n_g_s _o_f _t_h_e  _A_C_M  _9_t_h
     _S_y_m_p_o_s_i_u_m  _o_n  _O_p_e_r_a_t_i_n_g  _S_y_s_t_e_m_s,  SIGOPS _O_p_e_r_a_t_i_n_g _S_y_s_t_e_m_s
     _R_e_v_i_e_w, vol. 17, no. 5, October 1983.
     _C_o_r_e _W_a_r_s, in _S_c_i_e_n_t_i_f_i_c _A_m_e_r_i_c_a_n, May 1984.

DIAGNOSTICS
     More detailed diagnostics can be made available to  programs
     using  malloc(),  calloc(), realloc(), valloc(), memalign(),
     cfree(), and free(),  by  including  a  special  relocatable
     object  file  at  link time (see FILES). This file also pro-
     vides routines for control of error handling and  diagnosis,
     as  defined  below.  Note: these routines are _n_o_t defined in
     the standard library.

          int malloc_debug(level)
          int level;

          int malloc_verify()

     malloc_debug() sets the level of error diagnosis and report-
     ing  during  subsequent  calls  to malloc(), calloc(), real-
     loc(), valloc(), memalign(), cfree(), and free().  The value
     of _l_e_v_e_l is interpreted as follows:

     Level 0             malloc(), calloc(), realloc(), valloc(),
                         memalign(),  cfree(),  and free() behave
                         the same as in the standard library.

     Level 1             The routines abort with a message to the
                         standard error if errors are detected in
                         arguments or in  the  heap.   If  a  bad
                         block  is  encountered,  its address and
                         size are included in the message.

     Level 2             Same as level 1, except that the  entire
                         heap  is  examined  on every call to the
                         above routines.

     malloc_debug() returns the previous error diagnostic  level.
     The default level is 1.





Sun Release 4.1   Last change: 24 January 1990                  6






MALLOC(3V)             C LIBRARY FUNCTIONS             MALLOC(3V)



     malloc_verify() attempts to determine if the heap  has  been
     corrupted.   It  scans all blocks in the heap (both free and
     allocated) looking for strange addresses  or  absurd  sizes,
     and also checks for inconsistencies in the free space table.
     malloc_verify() returns 1 if all checks pass without  error,
     and  otherwise  returns 0. The checks can take a significant
     amount of time, so it should not be used indiscriminately.

WARNINGS
     alloca() is machine-, compiler-, and most  of  all,  system-
     dependent.    Its   use   is   strongly   discouraged.   See
     getrlimit(2), sigvec(2), sigstack(2), csh(1), and ld(1).

NOTES
     Because malloc(), realloc(), memalign() and valloc()  return
     a  non-NULL  pointer  if  _s_i_z_e  is 0, and calloc() returns a
     non-NULL pointer if _n_e_l_e_m or _e_l_s_i_z_e is 0, a zero  size  need
     not  be  treated as a special case if it should be passed to
     these functions unpredictably.  Also, the  pointer  returned
     by  these  functions may be passed to subsequent invocations
     of realloc().

SYSTEM V NOTES
     The XPG2 versions of the  allocation  routines  return  NULL
     when passed a zero size (see SYSTEM V DESCRIPTION above).

BUGS
     Since realloc() accepts a pointer to a block freed since the
     last  call  to  malloc(),  calloc(), realloc(), valloc(), or
     memalign(),  a  degradation  of  performance  results.   The
     semantics  of  free() should be changed so that the contents
     of a previously freed block are undefined.























Sun Release 4.1   Last change: 24 January 1990                  7



