Memory Allocation
*****************

The GNU system provides several methods for allocating memory space under explicit program control.  They vary in generality and in efficiency.

Dynamic Memory Allocation Concepts
==================================

Dynamic memory allocation is a technique in which programs determine as they are running where to store some information.  You need dynamic allocation when the number of memory blocks you need, or how long you continue to need them, depends on the data you are working on.

For example, you may need a block to store a line read from an input file; since there is no limit to how long a line can be, you must allocate the storage dynamically and make it dynamically larger as you read more of the line.

Or, you may need a block for each record or each definition in the input data; since you can't know in advance how many there will be, you must allocate a new block for each record or definition as you read it.

When you use dynamic allocation, the allocation of a block of memory is an action that the program requests explicitly.  You call a function or macro when you want to allocate space, and specify the size with an argument.  If you want to free the space, you do so by calling another function or macro. You can do these things whenever you want, as often as you want.

Dynamic Allocation and C
========================

The C language supports two kinds of memory allocation through the variables in C programs:

  * Static allocation is what happens when you declare a static or global variable.  Each static or global variable defines one block of space, of a fixed size.  The space is allocated once, when your program is started, and is never freed.

  * Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable.  The space for an automatic variable is allocated when the compound statement containing the declaration is entered, and is freed when that compound statement is exited.

     In GNU C, the length of the automatic storage can be an expression that varies.  In other C implementations, it must be a constant.

Dynamic allocation is not supported by C variables; there is no storage class ``dynamic'', and there can never be a C variable whose value is stored in dynamically allocated space.  The only way to refer to dynamically allocated space is through a pointer.  Because it is less convenient, and because the actual process of dynamic allocation requires more computation time, programmers use dynamic allocation only when neither static nor automatic allocation will serve.

For example, if you want to allocate dynamically some space to hold a struct foobar, you cannot declare a variable of type struct foobar whose contents are the dynamically allocated space.  But you can declare a variable of pointer type struct foobar * and assign it the address of the space.  Then you can use the operators * and -> on this pointer variable to refer to the contents of the space:

     {
       struct foobar *ptr
          = (struct foobar *) malloc (sizeof (struct foobar));
       ptr->name = x;
       ptr->next = current_foobar;
       current_foobar = ptr;
     }

Unconstrained Allocation
========================

The most general dynamic allocation facility is malloc.  It allows you to allocate blocks of memory of any size at any time, make them bigger or smaller at any time, and free the blocks individually at any time (or never).

Basic Storage Allocation
------------------------

To allocate a block of memory, call malloc.  The prototype for this function is in stdlib.h.

 -- Function: void * malloc (size_t SIZE)
     This function returns a pointer to a newly allocated block SIZE bytes long, or a null pointer if the block could not be allocated.

The contents of the block are undefined; you must initialize it yourself (or use calloc instead; see Allocating Cleared Space). Normally you would cast the value as a pointer to the kind of object that you want to store in the block.  Here we show an example of doing so, and of initializing the space with zeros using the library function memset (see Copying and Concatenation):

     struct foo *ptr;
     ...
     ptr = (struct foo *) malloc (sizeof (struct foo));
     if (ptr == 0) abort ();
     memset (ptr, 0, sizeof (struct foo));

You can store the result of malloc into any pointer variable without a cast, because ANSI C automatically converts the type void * to another type of pointer when necessary.  But the cast is necessary in contexts other than assignment operators or if you might want your code to run in traditional C.

Remember that when allocating space for a string, the argument to malloc must be one plus the length of the string.  This is because a string is terminated with a null character that doesn't count in the ``length'' of the string but does need space.  For example:

     char *ptr;
     ...
     ptr = (char *) malloc (length + 1);

See Representation of Strings, for more information about this.

Examples of malloc
------------------

If no more space is available, malloc returns a null pointer. You should check the value of every call to malloc.  It is useful to write a subroutine that calls malloc and reports an error if the value is a null pointer, returning only if the value is nonzero.  This function is conventionally called xmalloc.  Here it is:

     void *
     xmalloc (size_t size)
     {
       register void *value = malloc (size);
       if (value == 0)
         fatal ("virtual memory exhausted");
       return value;
     }

Here is a real example of using malloc (by way of xmalloc). The function savestring will copy a sequence of characters into a newly allocated null-terminated string:

     char *
     savestring (const char *ptr, size_t len)
     {
       register char *value = (char *) xmalloc (len + 1);
       memcpy (value, ptr, len);
       value[len] = '\0';
       return value;
     }

The block that malloc gives you is guaranteed to be aligned so that it can hold any type of data.  In the GNU system, the address is always a multiple of eight; if the size of block is 16 or more, then the address is always a multiple of 16.  Only rarely is any higher boundary (such as a page boundary) necessary; for those cases, use memalign or valloc (see Aligned Memory Blocks).

Note that the memory located after the end of the block is likely to be in use for something else; perhaps a block already allocated by another call to malloc.  If you attempt to treat the block as longer than you asked for it to be, you are liable to destroy the data that malloc uses to keep track of its blocks, or you may destroy the contents of another block.  If you have already allocated a block and discover you want it to be bigger, use realloc (see Changing Block Size).

Freeing Memory Allocated with malloc
------------------------------------

When you no longer need a block that you got with malloc, use the function free to make the block available to be allocated again. The prototype for this function is in stdlib.h.

 -- Function: void free (void *PTR)
     The free function deallocates the block of storage pointed at by PTR.

 -- Function: void cfree (void *PTR)
     This function does the same thing as free.  It's provided for backward compatibility with SunOS; you should use free instead.

Freeing a block alters the contents of the block.  Do not expect to find any data (such as a pointer to the next block in a chain of blocks) in the block after freeing it.  Copy whatever you need out of the block before freeing it!  Here is an example of the proper way to free all the blocks in a chain, and the strings that they point to:

     struct chain
       {
         struct chain *next;
         char *name;
       }

     void
     free_chain (struct chain *chain)
     {
       while (chain != 0)
         {
           struct chain *next = chain->next;
           free (chain->name);
           free (chain);
           chain = next;
         }
     }

Occasionally, free can actually return memory to the operating system and make the process smaller.  Usually, all it can do is allow a later call to malloc to reuse the space.  In the meantime, the space remains in your program as part of a free-list used internally by malloc.

There is no point in freeing blocks at the end of a program, because all of the program's space is given back to the system when the process terminates.

Changing the Size of a Block
----------------------------

Often you do not know for certain how big a block you will ultimately need at the time you must begin to use the block.  For example, the block might be a buffer that you use to hold a line being read from a file; no matter how long you make the buffer initially, you may encounter a line that is longer.

You can make the block longer by calling realloc.  This function is declared in stdlib.h.

 -- Function: void * realloc (void *PTR, size_t NEWSIZE)
     The realloc function changes the size of the block whose address is PTR to be NEWSIZE.

     Since the space after the end of the block may be in use, realloc may find it necessary to copy the block to a new address where more free space is available.  The value of realloc is the new address of the block.  If the block needs to be moved, realloc copies the old contents.

     If you pass a null pointer for PTR, realloc behaves just like malloc (NEWSIZE).  This can be convenient, but beware that older implementations (before ANSI C) may not support this behavior, and will probably crash when realloc is passed a null pointer.

Like malloc, realloc may return a null pointer if no memory space is available to make the block bigger.  When this happens, the original block is untouched; it has not been modified or relocated.

In most cases it makes no difference what happens to the original block when realloc fails, because the application program cannot continue when it is out of memory, and the only thing to do is to give a fatal error message.  Often it is convenient to write and use a subroutine, conventionally called xrealloc, that takes care of the error message as xmalloc does for malloc:

     void *
     xrealloc (void *ptr, size_t size)
     {
       register void *value = realloc (ptr, size);
       if (value == 0)
         fatal ("Virtual memory exhausted");
       return value;
     }

You can also use realloc to make a block smaller.  The reason you would do this is to avoid tying up a lot of memory space when only a little is needed.  Making a block smaller sometimes necessitates copying it, so it can fail if no other space is available.

If the new size you specify is the same as the old size, realloc is guaranteed to change nothing and return the same address that you gave.

Allocating Cleared Space
------------------------

The function calloc allocates memory and clears it to zero.  It is declared in stdlib.h.

 -- Function: void * calloc (size_t COUNT, size_t ELTSIZE)
     This function allocates a block long enough to contain a vector of COUNT elements, each of size ELTSIZE.  Its contents are cleared to zero before calloc returns.

You could define calloc as follows:

     void *
     calloc (size_t count, size_t eltsize)
     {
       size_t size = count * eltsize;
       void *value = malloc (size);
       if (value != 0)
         memset (value, 0, size);
       return value;
     }

We rarely use calloc today, because it is equivalent to such a simple combination of other features that are more often used.  It is a historical holdover that is not quite obsolete.

Efficiency Considerations for malloc
------------------------------------

To make the best use of malloc, it helps to know that the GNU version of malloc always dispenses small amounts of memory in blocks whose sizes are powers of two.  It keeps separate pools for each power of two.  This holds for sizes up to a page size.  Therefore, if you are free to choose the size of a small block in order to make malloc more efficient, make it a power of two.

Once a page is split up for a particular block size, it can't be reused for another size unless all the blocks in it are freed.  In many programs, this is unlikely to happen.  Thus, you can sometimes make a program use memory more efficiently by using blocks of the same size for many different purposes.

When you ask for memory blocks of a page or larger, malloc uses a different strategy; it rounds the size up to a multiple of a page, and it can coalesce and split blocks as needed.

The reason for the two strategies is that it is important to allocate and free small blocks as fast as possible, but speed is less important for a large block since the program normally spends a fair amount of time using it.  Also, large blocks are normally fewer in number. Therefore, for large blocks, it makes sense to use a method which takes more time to minimize the wasted space.

Allocating Aligned Memory Blocks
--------------------------------

The address of a block returned by malloc or realloc in the GNU system is always a multiple of eight.  If you need a block whose address is a multiple of a higher power of two than that, use memalign or valloc.  These functions are declared in stdlib.h.

With the GNU library, you can use free to free the blocks that memalign and valloc return.  That does not work in BSD, however---BSD does not provide any way to free such blocks.

 -- Function: void * memalign (size_t SIZE, size_t BOUNDARY)
     The memalign function allocates a block of SIZE bytes whose address is a multiple of BOUNDARY.  The BOUNDARY must be a power of two!  The function memalign works by calling malloc to allocate a somewhat larger block, and then returning an address within the block that is on the specified boundary.

 -- Function: void * valloc (size_t SIZE)
     Using valloc is like using memalign and passing the page size as the value of the second argument.  It is implemented like this:

          void *
          valloc (size_t size)
          {
            return memalign (size, getpagesize ());
          }

Heap Consistency Checking
-------------------------

You can ask malloc to check the consistency of dynamic storage by using the mcheck function.  This function is a GNU extension, declared in malloc.h.

 -- Function: int mcheck (void (*ABORTFN) (enum mcheck_status STATUS))
     Calling mcheck tells malloc to perform occasional consistency checks.  These will catch things such as writing past the end of a block that was allocated with malloc.

     The ABORTFN argument is the function to call when an inconsistency is found.  If you supply a null pointer, then mcheck uses a default function which prints a message and calls abort (see Aborting a Program).  The function you supply is called with one argument, which says what sort of inconsistency was detected; its type is described below.

     It is too late to begin allocation checking once you have allocated anything with malloc.  So mcheck does nothing in that case.  The function returns -1 if you call it too late, and 0 otherwise (when it is successful).

     The easiest way to arrange to call mcheck early enough is to use the option -lmcheck when you link your program; then you don't need to modify your program source at all.

 -- Function: enum mcheck_status mprobe (void *POINTER)
     The mprobe function lets you explicitly check for inconsistencies in a particular allocated block.  You must have already called mcheck at the beginning of the program, to do its occasional checks; calling mprobe requests an additional consistency check to be done at the time of the call.

     The argument POINTER must be a pointer returned by malloc or realloc.  mprobe returns a value that says what inconsistency, if any, was found.  The values are described below.

 -- Data Type: enum mcheck_status
     This enumerated type describes what kind of inconsistency was detected in an allocated block, if any.  Here are the possible values:

     MCHECK_DISABLED
          mcheck was not called before the first allocation. No consistency checking can be done.

     MCHECK_OK
          No inconsistency detected.

     MCHECK_HEAD
          The data immediately before the block was modified. This commonly happens when an array index or pointer is decremented too far.

     MCHECK_TAIL
          The data immediately after the block was modified. This commonly happens when an array index or pointer is incremented too far.

     MCHECK_FREE
          The block was already freed.

Storage Allocation Hooks
------------------------

The GNU C library lets you modify the behavior of malloc, realloc, and free by specifying appropriate hook functions.  You can use these hooks to help you debug programs that use dynamic storage allocation, for example.

The hook variables are declared in malloc.h.

 -- Variable: __malloc_hook
     The value of this variable is a pointer to function that malloc uses whenever it is called.  You should define this function to look like malloc; that is, like:

          void *FUNCTION (size_t SIZE)

 -- Variable: __realloc_hook
     The value of this variable is a pointer to function that realloc uses whenever it is called.  You should define this function to look like realloc; that is, like:

          void *FUNCTION (void *PTR, size_t SIZE)

 -- Variable: __free_hook
     The value of this variable is a pointer to function that free uses whenever it is called.  You should define this function to look like free; that is, like:

          void FUNCTION (void *PTR)

You must make sure that the function you install as a hook for one of these functions does not call that function recursively without restoring the old value of the hook first!  Otherwise, your program will get stuck in an infinite recursion.

Here is an example showing how to use __malloc_hook properly.  It installs a function that prints out information every time malloc is called.

     static void *(*old_malloc_hook) (size_t);
     static void *
     my_malloc_hook (size_t size)
     {
       void *result;
       __malloc_hook = old_malloc_hook;
       result = malloc (size);
       __malloc_hook = my_malloc_hook;
       printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
       return result;
     }

     main ()
     {
       ...
       old_malloc_hook = __malloc_hook;
       __malloc_hook = my_malloc_hook;
       ...
     }

The mcheck function (see Heap Consistency Checking) works by installing such hooks.

Statistics for Storage Allocation with malloc
---------------------------------------------

You can get information about dynamic storage allocation by calling the mstats function.  This function and its associated data type are declared in malloc.h; they are a GNU extension.

 -- Data Type: struct mstats
     This structure type is used to return information about the dynamic storage allocator.  It contains the following members:

     size_t bytes_total
          This is the total size of memory managed by malloc, in bytes.

     size_t chunks_used
          This is the number of chunks in use.  (The storage allocator internally gets chunks of memory from the operating system, and then carves them up to satisfy individual malloc requests; see Efficiency and Malloc.)

     size_t bytes_used
          This is the number of bytes in use.

     size_t chunks_free
          This is the number of chunks which are free -- that is, that have been allocated by the operating system to your program, but which are not now being used.

     size_t bytes_free
          This is the number of bytes which are free.

 -- Function: struct mstats mstats (void)
     This function returns information about the current dynamic memory usage in a structure of type struct mstats.

Summary of malloc-Related Functions
-----------------------------------

Here is a summary of the functions that work with malloc:

void *malloc (size_t SIZE)
     Allocate a block of SIZE bytes.  See Basic Allocation.

void free (void *ADDR)
     Free a block previously allocated by malloc.  See Freeing after Malloc.

void *realloc (void *ADDR, size_t SIZE)
     Make a block previously allocated by malloc larger or smaller, possibly by copying it to a new location.  See Changing Block Size.

void *calloc (size_t COUNT, size_t ELTSIZE)
     Allocate a block of COUNT * ELTSIZE bytes using malloc, and set its contents to zero.  See Allocating Cleared Space.

void *valloc (size_t SIZE)
     Allocate a block of SIZE bytes, starting on a page boundary. See Aligned Memory Blocks.

void *memalign (size_t SIZE, size_t BOUNDARY)
     Allocate a block of SIZE bytes, starting on an address that is a multiple of BOUNDARY.  See Aligned Memory Blocks.

int mcheck (void (*ABORTFN) (void))
     Tell malloc to perform occasional consistency checks on dynamically allocated memory, and to call ABORTFN when an inconsistency is found.  See Heap Consistency Checking.

void *(*__malloc_hook) (size_t SIZE)
     A pointer to a function that malloc uses whenever it is called.

void *(*__realloc_hook) (void *PTR, size_t SIZE)
     A pointer to a function that realloc uses whenever it is called.

void (*__free_hook) (void *PTR)
     A pointer to a function that free uses whenever it is called.

struct mstats mstats (void)
     Return information about the current dynamic memory usage. See Statistics of Malloc.

Obstacks
========

An obstack is a pool of memory containing a stack of objects.  You can create any number of separate obstacks, and then allocate objects in specified obstacks.  Within each obstack, the last object allocated must always be the first one freed, but distinct obstacks are independent of each other.

Aside from this one constraint of order of freeing, obstacks are totally general: an obstack can contain any number of objects of any size.  They are implemented with macros, so allocation is usually very fast as long as the objects are usually small.  And the only space overhead per object is the padding needed to start each object on a suitable boundary.

Creating Obstacks
-----------------

The utilities for manipulating obstacks are declared in the header file obstack.h.

 -- Data Type: struct obstack
     An obstack is represented by a data structure of type struct obstack.  This structure has a small fixed size; it records the status of the obstack and how to find the space in which objects are allocated. It does not contain any of the objects themselves.  You should not try to access the contents of the structure directly; use only the functions described in this chapter.

You can declare variables of type struct obstack and use them as obstacks, or you can allocate obstacks dynamically like any other kind of object.  Dynamic allocation of obstacks allows your program to have a variable number of different stacks.  (You can even allocate an obstack structure in another obstack, but this is rarely useful.)

All the functions that work with obstacks require you to specify which obstack to use.  You do this with a pointer of type struct obstack *.  In the following, we often say ``an obstack'' when strictly speaking the object at hand is such a pointer.

The objects in the obstack are packed into large blocks called chunks.  The struct obstack structure points to a chain of the chunks currently in use.

The obstack library obtains a new chunk whenever you allocate an object that won't fit in the previous chunk.  Since the obstack library manages chunks automatically, you don't need to pay much attention to them, but you do need to supply a function which the obstack library should use to get a chunk.  Usually you supply a function which uses malloc directly or indirectly.  You must also supply a function to free a chunk. These matters are described in the following section.

Preparing for Using Obstacks
----------------------------

Each source file in which you plan to use the obstack functions must include the header file obstack.h, like this:

     #include <obstack.h>

Also, if the source file uses the macro obstack_init, it must declare or define two functions or macros that will be called by the obstack library.  One, obstack_chunk_alloc, is used to allocate the chunks of memory into which objects are packed.  The other, obstack_chunk_free, is used to return chunks when the objects in them are freed.  These macros should appear before any use of obstacks in the source file.

Usually these are defined to use malloc via the intermediary xmalloc (see Unconstrained Allocation).  This is done with the following pair of macro definitions:

     #define obstack_chunk_alloc xmalloc
     #define obstack_chunk_free free

Though the storage you get using obstacks really comes from malloc, using obstacks is faster because malloc is called less often, for larger blocks of memory.  See Obstack Chunks, for full details.

At run time, before the program can use a struct obstack object as an obstack, it must initialize the obstack by calling obstack_init.

 -- Function: int obstack_init (struct obstack *OBSTACK-PTR)
     Initialize obstack OBSTACK-PTR for allocation of objects.  This function calls the obstack's obstack_chunk_alloc function.  It returns 0 if obstack_chunk_alloc returns a null pointer, meaning that it is out of memory.  Otherwise, it returns 1.  If you supply an obstack_chunk_alloc function that calls exit (see Program Termination) or longjmp (see Non-Local Exits) when out of memory, you can safely ignore the value that obstack_init returns.

Here are two examples of how to allocate the space for an obstack and initialize it.  First, an obstack that is a static variable:

     static struct obstack myobstack;
     ...
     obstack_init (&myobstack);

Second, an obstack that is itself dynamically allocated:

     struct obstack *myobstack_ptr
       = (struct obstack *) xmalloc (sizeof (struct obstack));

     obstack_init (myobstack_ptr);

Allocation in an Obstack
------------------------

The most direct way to allocate an object in an obstack is with obstack_alloc, which is invoked almost like malloc.

 -- Function: void * obstack_alloc (struct obstack *OBSTACK-PTR, int SIZE)
     This allocates an uninitialized block of SIZE bytes in an obstack and returns its address.  Here OBSTACK-PTR specifies which obstack to allocate the block in; it is the address of the struct obstack object which represents the obstack.  Each obstack function or macro requires you to specify an OBSTACK-PTR as the first argument.

     This function calls the obstack's obstack_chunk_alloc function if it needs to allocate a new chunk of memory; it returns a null pointer if obstack_chunk_alloc returns one.  In that case, it has not changed the amount of memory allocated in the obstack.  If you supply an obstack_chunk_alloc function that calls exit (see Program Termination) or longjmp (see Non-Local Exits) when out of memory, then obstack_alloc will never return a null pointer.

For example, here is a function that allocates a copy of a string STR in a specific obstack, which is in the variable string_obstack:

     struct obstack string_obstack;

     char *
     copystring (char *string)
     {
       char *s = (char *) obstack_alloc (&string_obstack,
                                         strlen (string) + 1);
       memcpy (s, string, strlen (string));
       return s;
     }

To allocate a block with specified contents, use the function obstack_copy, declared like this:

 -- Function: void * obstack_copy (struct obstack *OBSTACK-PTR, void *ADDRESS, int SIZE)
     This allocates a block and initializes it by copying SIZE bytes of data starting at ADDRESS.  It can return a null pointer under the same conditions as obstack_alloc.

 -- Function: void * obstack_copy0 (struct obstack *OBSTACK-PTR, void *ADDRESS, int SIZE)
     Like obstack_copy, but appends an extra byte containing a null character.  This extra byte is not counted in the argument SIZE.

The obstack_copy0 function is convenient for copying a sequence of characters into an obstack as a null-terminated string.  Here is an example of its use:

     char *
     obstack_savestring (char *addr, int size)
     {
       return obstack_copy0 (&myobstack, addr, size);
     }

Contrast this with the previous example of savestring using malloc (see Basic Allocation).

Freeing Objects in an Obstack
-----------------------------

To free an object allocated in an obstack, use the function obstack_free.  Since the obstack is a stack of objects, freeing one object automatically frees all other objects allocated more recently in the same obstack.

 -- Function: void obstack_free (struct obstack *OBSTACK-PTR, void *OBJECT)
     If OBJECT is a null pointer, everything allocated in the obstack is freed.  Otherwise, OBJECT must be the address of an object allocated in the obstack.  Then OBJECT is freed, along with everything allocated in OBSTACK since OBJECT.

Note that if OBJECT is a null pointer, the result is an uninitialized obstack.  To free all storage in an obstack but leave it valid for further allocation, call obstack_free with the address of the first object allocated on the obstack:

     obstack_free (obstack_ptr, first_object_allocated_ptr);

Recall that the objects in an obstack are grouped into chunks.  When all the objects in a chunk become free, the obstack library automatically frees the chunk (see Preparing for Obstacks).  Then other obstacks, or non-obstack allocation, can reuse the space of the chunk.

Obstack Functions and Macros
----------------------------

The interfaces for using obstacks may be defined either as functions or as macros, depending on the compiler.  The obstack facility works with all C compilers, including both ANSI C and traditional C, but there are precautions you must take if you plan to use compilers other than GNU C.

If you are using an old-fashioned non-ANSI C compiler, all the obstack ``functions'' are actually defined only as macros.  You can call these macros like functions, but you cannot use them in any other way (for example, you cannot take their address).

Calling the macros requires a special precaution: namely, the first operand (the obstack pointer) may not contain any side effects, because it may be computed more than once.  For example, if you write this:

     obstack_alloc (get_obstack (), 4);

you will find that get_obstack may be called several times. If you use *obstack_list_ptr++ as the obstack pointer argument, you will get very strange results since the incrementation may occur several times.

In ANSI C, each function has both a macro definition and a function definition.  The function definition is used if you take the address of the function without calling it.  An ordinary call uses the macro definition by default, but you can request the function definition instead by writing the function name in parentheses, as shown here:

     char *x;
     void *(*funcp) ();
     /* Use the macro.  */
     x = (char *) obstack_alloc (obptr, size);
     /* Call the function.  */
     x = (char *) (obstack_alloc) (obptr, size);
     /* Take the address of the function.  */
     funcp = obstack_alloc;

This is the same situation that exists in ANSI C for the standard library functions.  See Macro Definitions.

Warning: When you do use the macros, you must observe the precaution of avoiding side effects in the first operand, even in ANSI C.

If you use the GNU C compiler, this precaution is not necessary, because various language extensions in GNU C permit defining the macros so as to compute each argument only once.

Growing Objects
---------------

Because storage in obstack chunks is used sequentially, it is possible to build up an object step by step, adding one or more bytes at a time to the end of the object.  With this technique, you do not need to know how much data you will put in the object until you come to the end of it.  We call this the technique of growing objects.  The special functions for adding data to the growing object are described in this section.

You don't need to do anything special when you start to grow an object. Using one of the functions to add data to the object automatically starts it.  However, it is necessary to say explicitly when the object is finished.  This is done with the function obstack_finish.

The actual address of the object thus built up is not known until the object is finished.  Until then, it always remains possible that you will add so much data that the object must be copied into a new chunk.

While the obstack is in use for a growing object, you cannot use it for ordinary allocation of another object.  If you try to do so, the space already added to the growing object will become part of the other object.

 -- Function: void obstack_blank (struct obstack *OBSTACK-PTR, int SIZE)
     The most basic function for adding to a growing object is obstack_blank, which adds space without initializing it.

 -- Function: void obstack_grow (struct obstack *OBSTACK-PTR, void *DATA, int SIZE)
     To add a block of initialized space, use obstack_grow, which is the growing-object analogue of obstack_copy.  It adds SIZE bytes of data to the growing object, copying the contents from DATA.

 -- Function: void obstack_grow0 (struct obstack *OBSTACK-PTR, void *DATA, int SIZE)
     This is the growing-object analogue of obstack_copy0.  It adds SIZE bytes copied from DATA, followed by an additional null character.

 -- Function: void obstack_1grow (struct obstack *OBSTACK-PTR, char C)
     To add one character at a time, use the function obstack_1grow. It adds a single byte containing C to the growing object.

 -- Function: void * obstack_finish (struct obstack *OBSTACK-PTR)
     When you are finished growing the object, use the function obstack_finish to close it off and return its final address.

     Once you have finished the object, the obstack is available for ordinary allocation or for growing another object.

     This function can return a null pointer under the same conditions as obstack_alloc (see Allocation in an Obstack).

When you build an object by growing it, you will probably need to know afterward how long it became.  You need not keep track of this as you grow the object, because you can find out the length from the obstack just before finishing the object with the function obstack_object_size, declared as follows:

 -- Function: int obstack_object_size (struct obstack *OBSTACK-PTR)
     This function returns the current size of the growing object, in bytes. Remember to call this function before finishing the object. After it is finished, obstack_object_size will return zero.

If you have started growing an object and wish to cancel it, you should finish it and then free it, like this:

     obstack_free (obstack_ptr, obstack_finish (obstack_ptr));

This has no effect if no object was growing.

You can use obstack_blank with a negative size argument to make the current object smaller.  Just don't try to shrink it beyond zero length---there's no telling what will happen if you do that.

Extra Fast Growing Objects
--------------------------

The usual functions for growing objects incur overhead for checking whether there is room for the new growth in the current chunk.  If you are frequently constructing objects in small steps of growth, this overhead can be significant.

You can reduce the overhead by using special ``fast growth'' functions that grow the object without checking.  In order to have a robust program, you must do the checking yourself.  If you do this checking in the simplest way each time you are about to add data to the object, you have not saved anything, because that is what the ordinary growth functions do.  But if you can arrange to check less often, or check more efficiently, then you make the program faster.

The function obstack_room returns the amount of room available in the current chunk.  It is declared as follows:

 -- Function: int obstack_room (struct obstack *OBSTACK-PTR)
     This returns the number of bytes that can be added safely to the current growing object (or to an object about to be started) in obstack OBSTACK using the fast growth functions.

While you know there is room, you can use these fast growth functions for adding data to a growing object:

 -- Function: void obstack_1grow_fast (struct obstack *OBSTACK-PTR, char C)
     The function obstack_1grow_fast adds one byte containing the character C to the growing object in obstack OBSTACK-PTR.

 -- Function: void obstack_blank_fast (struct obstack *OBSTACK-PTR, int SIZE)
     The function obstack_blank_fast adds SIZE bytes to the growing object in obstack OBSTACK-PTR without initializing them.

When you check for space using obstack_room and there is not enough room for what you want to add, the fast growth functions are not safe.  In this case, simply use the corresponding ordinary growth function instead.  Very soon this will copy the object to a new chunk; then there will be lots of room available again.

So, each time you use an ordinary growth function, check afterward for sufficient space using obstack_room.  Once the object is copied to a new chunk, there will be plenty of space again, so the program will start using the fast growth functions again.

Here is an example:

     void
     add_string (struct obstack *obstack, char *ptr, int len)
     {
       while (len > 0)
         {
           if (obstack_room (obstack) > len)
             {
               /* We have enough room: add everything fast.  */
               while (len-- > 0)
                 obstack_1grow_fast (obstack, *ptr++);
             }
           else
             {
               /* Not enough room. Add one character slowly,
                  which may copy to a new chunk and make room.  */
               obstack_1grow (obstack, *ptr++);
               len--;
             }
         }
     }

Status of an Obstack
--------------------

Here are functions that provide information on the current status of allocation in an obstack.  You can use them to learn about an object while still growing it.

 -- Function: void * obstack_base (struct obstack *OBSTACK-PTR)
     This function returns the tentative address of the beginning of the currently growing object in OBSTACK-PTR.  If you finish the object immediately, it will have that address.  If you make it larger first, it may outgrow the current chunk---then its address will change!

     If no object is growing, this value says where the next object you allocate will start (once again assuming it fits in the current chunk).

 -- Function: void * obstack_next_free (struct obstack *OBSTACK-PTR)
     This function returns the address of the first free byte in the current chunk of obstack OBSTACK-PTR.  This is the end of the currently growing object.  If no object is growing, obstack_next_free returns the same value as obstack_base.

 -- Function: int obstack_object_size (struct obstack *OBSTACK-PTR)
     This function returns the size in bytes of the currently growing object. This is equivalent to

          obstack_next_free (OBSTACK-PTR) - obstack_base (OBSTACK-PTR)

Alignment of Data in Obstacks
-----------------------------

Each obstack has an alignment boundary; each object allocated in the obstack automatically starts on an address that is a multiple of the specified boundary.  By default, this boundary is 4 bytes.

To access an obstack's alignment boundary, use the macro obstack_alignment_mask, whose function prototype looks like this:

 -- Macro: int obstack_alignment_mask (struct obstack *OBSTACK-PTR)
     The value is a bit mask; a bit that is 1 indicates that the corresponding bit in the address of an object should be 0.  The mask value should be one less than a power of 2; the effect is that all object addresses are multiples of that power of 2.  The default value of the mask is 3, so that addresses are multiples of 4.  A mask value of 0 means an object can start on any multiple of 1 (that is, no alignment is required).

     The expansion of the macro obstack_alignment_mask is an lvalue, so you can alter the mask by assignment.  For example, this statement:

          obstack_alignment_mask (obstack_ptr) = 0;

     has the effect of turning off alignment processing in the specified obstack.

Note that a change in alignment mask does not take effect until after the next time an object is allocated or finished in the obstack.  If you are not growing an object, you can make the new alignment mask take effect immediately by calling obstack_finish. This will finish a zero-length object and then do proper alignment for the next object.

Obstack Chunks
--------------

Obstacks work by allocating space for themselves in large chunks, and then parceling out space in the chunks to satisfy your requests.  Chunks are normally 4096 bytes long unless you specify a different chunk size. The chunk size includes 8 bytes of overhead that are not actually used for storing objects.  Regardless of the specified size, longer chunks will be allocated when necessary for long objects.

The obstack library allocates chunks by calling the function obstack_chunk_alloc, which you must define.  When a chunk is no longer needed because you have freed all the objects in it, the obstack library frees the chunk by calling obstack_chunk_free, which you must also define.

These two must be defined (as macros) or declared (as functions) in each source file that uses obstack_init (see Creating Obstacks). Most often they are defined as macros like this:

     #define obstack_chunk_alloc xmalloc
     #define obstack_chunk_free free

Note that these are simple macros (no arguments).  Macro definitions with arguments will not work!  It is necessary that obstack_chunk_alloc or obstack_chunk_free, alone, expand into a function name if it is not itself a function name.

If you allocate chunks with malloc, the chunk size should be a power of 2.  The default chunk size, 4096, was chosen because it is long enough to satisfy many typical requests on the obstack yet short enough not to waste too much memory in the portion of the last chunk not yet used.

 -- Macro: int obstack_chunk_size (struct obstack *OBSTACK-PTR)
     This returns the chunk size of the given obstack.

Since this macro expands to an lvalue, you can specify a new chunk size by assigning it a new value.  Doing so does not affect the chunks already allocated, but will change the size of chunks allocated for that particular obstack in the future.  It is unlikely to be useful to make the chunk size smaller, but making it larger might improve efficiency if you are allocating many objects whose size is comparable to the chunk size.  Here is how to do so cleanly:

     if (obstack_chunk_size (obstack_ptr) < NEW-CHUNK-SIZE)
       obstack_chunk_size (obstack_ptr) = NEW-CHUNK-SIZE;

Summary of Obstack Functions
----------------------------

Here is a summary of all the functions associated with obstacks.  Each takes the address of an obstack (struct obstack *) as its first argument.

void obstack_init (struct obstack *OBSTACK-PTR)
     Initialize use of an obstack.  See Creating Obstacks.

void *obstack_alloc (struct obstack *OBSTACK-PTR, int SIZE)
     Allocate an object of SIZE uninitialized bytes. See Allocation in an Obstack.

void *obstack_copy (struct obstack *OBSTACK-PTR, void *ADDRESS, int SIZE)
     Allocate an object of SIZE bytes, with contents copied from ADDRESS.  See Allocation in an Obstack.

void *obstack_copy0 (struct obstack *OBSTACK-PTR, void *ADDRESS, int SIZE)
     Allocate an object of SIZE+1 bytes, with SIZE of them copied from ADDRESS, followed by a null character at the end. See Allocation in an Obstack.

void obstack_free (struct obstack *OBSTACK-PTR, void *OBJECT)
     Free OBJECT (and everything allocated in the specified obstack more recently than OBJECT).  See Freeing Obstack Objects.

void obstack_blank (struct obstack *OBSTACK-PTR, int SIZE)
     Add SIZE uninitialized bytes to a growing object. See Growing Objects.

void obstack_grow (struct obstack *OBSTACK-PTR, void *ADDRESS, int SIZE)
     Add SIZE bytes, copied from ADDRESS, to a growing object. See Growing Objects.

void obstack_grow0 (struct obstack *OBSTACK-PTR, void *ADDRESS, int SIZE)
     Add SIZE bytes, copied from ADDRESS, to a growing object, and then add another byte containing a null character.  See Growing Objects.

void obstack_1grow (struct obstack *OBSTACK-PTR, char DATA-CHAR)
     Add one byte containing DATA-CHAR to a growing object. See Growing Objects.

void *obstack_finish (struct obstack *OBSTACK-PTR)
     Finalize the object that is growing and return its permanent address. See Growing Objects.

int obstack_object_size (struct obstack *OBSTACK-PTR)
     Get the current size of the currently growing object.  See Growing Objects.

void obstack_blank_fast (struct obstack *OBSTACK-PTR, int SIZE)
     Add SIZE uninitialized bytes to a growing object without checking that there is enough room.  See Extra Fast Growing.

void obstack_1grow_fast (struct obstack *OBSTACK-PTR, char DATA-CHAR)
     Add one byte containing DATA-CHAR to a growing object without checking that there is enough room.  See Extra Fast Growing.

int obstack_room (struct obstack *OBSTACK-PTR)
     Get the amount of room now available for growing the current object. See Extra Fast Growing.

int obstack_alignment_mask (struct obstack *OBSTACK-PTR)
     The mask used for aligning the beginning of an object.  This is an lvalue.  See Obstacks Data Alignment.

int obstack_chunk_size (struct obstack *OBSTACK-PTR)
     The size for allocating chunks.  This is an lvalue.  See Obstack Chunks.

void *obstack_base (struct obstack *OBSTACK-PTR)
     Tentative starting address of the currently growing object. See Status of an Obstack.

void *obstack_next_free (struct obstack *OBSTACK-PTR)
     Address just after the end of the currently growing object. See Status of an Obstack.

Automatic Storage with Variable Size
====================================

The function alloca supports a kind of half-dynamic allocation in which blocks are allocated dynamically but freed automatically.

Allocating a block with alloca is an explicit action; you can allocate as many blocks as you wish, and compute the size at run time.  But all the blocks are freed when you exit the function that alloca was called from, just as if they were automatic variables declared in that function.  There is no way to free the space explicitly.

The prototype for alloca is in stdlib.h.  This function is a BSD extension.

 -- Function: void * alloca (size_t SIZE);
     The return value of alloca is the address of a block of SIZE bytes of storage, allocated in the stack frame of the calling function.

Do not use alloca inside the arguments of a function call---you will get unpredictable results, because the stack space for the alloca would appear on the stack in the middle of the space for the function arguments.  An example of what to avoid is foo (x, alloca (4), y).

alloca Example
--------------

As an example of use of alloca, here is a function that opens a file name made from concatenating two argument strings, and returns a file descriptor or minus one signifying failure:

     int
     open2 (char *str1, char *str2, int flags, int mode)
     {
       char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
       strcpy (name, str1);
       strcat (name, str2);
       return open (name, flags, mode);
     }

Here is how you would get the same results with malloc and free:

     int
     open2 (char *str1, char *str2, int flags, int mode)
     {
       char *name = (char *) malloc (strlen (str1) + strlen (str2) + 1);
       int desc;
       if (name == 0)
         fatal ("virtual memory exceeded");
       strcpy (name, str1);
       strcat (name, str2);
       desc = open (name, flags, mode);
       free (name);
       return desc;
     }

As you can see, it is simpler with alloca.  But alloca has other, more important advantages, and some disadvantages.

Advantages of alloca
--------------------

Here are the reasons why alloca may be preferable to malloc:

  * Using alloca wastes very little space and is very fast.  (It is open-coded by the GNU C compiler.)

  * Since alloca does not have separate pools for different sizes of block, space used for any size block can be reused for any other size. alloca does not cause storage fragmentation.

  * Nonlocal exits done with longjmp (see Non-Local Exits) automatically free the space allocated with alloca when they exit through the function that called alloca.  This is the most important reason to use alloca.

     To illustrate this, suppose you have a function open_or_report_error which returns a descriptor, like open, if it succeeds, but does not return to its caller if it fails.  If the file cannot be opened, it prints an error message and jumps out to the command level of your program using longjmp. Let's change open2 (see Alloca Example) to use this subroutine:

          int
          open2 (char *str1, char *str2, int flags, int mode)
          {
            char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
            strcpy (name, str1);
            strcat (name, str2);
            return open_or_report_error (name, flags, mode);
          }

     Because of the way alloca works, the storage it allocates is freed even when an error occurs, with no special effort required.

     By contrast, the previous definition of open2 (which uses malloc and free) would develop a storage leak if it were changed in this way.  Even if you are willing to make more changes to fix it, there is no easy way to do so.

Disadvantages of alloca
-----------------------

These are the disadvantages of alloca in comparison with malloc:

  * If you try to allocate more storage than the machine can provide, you don't get a clean error message.  Instead you get a fatal signal like the one you would get from an infinite recursion; probably a segmentation violation (see Program Error Signals).

  * Some non-GNU systems fail to support alloca, so it is less portable.  However, a slower emulation of alloca written in C is available for use on systems with this deficiency.

GNU C Variable-Size Arrays
--------------------------

In GNU C, you can replace most uses of alloca with an array of variable size.  Here is how open2 would look then:

     int open2 (char *str1, char *str2, int flags, int mode)
     {
       char name[strlen (str1) + strlen (str2) + 1];
       strcpy (name, str1);
       strcat (name, str2);
       return open (name, flags, mode);
     }

But alloca is not always equivalent to a variable-sized array, for several reasons:

  * A variable size array's space is freed at the end of the scope of the name of the array.  The space allocated with alloca remains until the end of the function.

  * It is possible to use alloca within a loop, allocating an additional block on each iteration.  This is impossible with variable-sized arrays.

Note: If you mix use of alloca and variable-sized arrays within one function, exiting a scope in which a variable-sized array was declared frees all blocks allocated with alloca during the execution of that scope.

Relocating Allocator
====================

Any system of dynamic memory allocation has overhead: the amount of space it uses is more than the amount the program asks for.  The relocating memory allocator achieves very low overhead by moving blocks in memory as necessary, on its own initiative.

Concepts of Relocating Allocation
---------------------------------

The relocating memory allocator achieves very low overhead by moving blocks in memory as necessary, on its own initiative.

When you allocate a block with malloc, the address of the block never changes unless you use realloc to change its size.  Thus, you can safely store the address in various places, temporarily or permanently, as you like.  This is not safe when you use the relocating memory allocator, because any and all relocatable blocks can move whenever you allocate memory in any fashion.  Even calling malloc or realloc can move the relocatable blocks.

For each relocatable block, you must make a handle---a pointer object in memory, designated to store the address of that block.  The relocating allocator knows where each block's handle is, and updates the address stored there whenever it moves the block, so that the handle always points to the block.  Each time you access the contents of the block, you should fetch its address anew from the handle.

To call any of the relocating allocator functions from a signal handler is almost certainly incorrect, because the signal could happen at any time and relocate all the blocks.  The only way to make this safe is to block the signal around any access to the contents of any relocatable block---not a convenient mode of operation.  See Nonreentrancy.

Allocating and Freeing Relocatable Blocks
-----------------------------------------

In the descriptions below, HANDLEPTR designates the address of the handle.  All the functions are declared in malloc.h; all are GNU extensions.

 -- Function: void * r_alloc (void **HANDLEPTR, size_t SIZE)
     This function allocates a relocatable block of size SIZE.  It stores the block's address in *HANDLEPTR and returns a non-null pointer to indicate success.

     If r_alloc can't get the space needed, it stores a null pointer in *HANDLEPTR, and returns a null pointer.

 -- Function: void r_alloc_free (void **HANDLEPTR)
     This function is the way to free a relocatable block.  It frees the block that *HANDLEPTR points to, and stores a null pointer in *HANDLEPTR to show it doesn't point to an allocated block any more.

 -- Function: void * r_re_alloc (void **HANDLEPTR, size_t SIZE)
     The function r_re_alloc adjusts the size of the block that *HANDLEPTR points to, making it SIZE bytes long.  It stores the address of the resized block in *HANDLEPTR and returns a non-null pointer to indicate success.

     If enough memory is not available, this function returns a null pointer and does not modify *HANDLEPTR.

Memory Usage Warnings
=====================

You can ask for warnings as the program approaches running out of memory space, by calling memory_warnings.  This tells malloc to check memory usage every time it asks for more memory from the operating system.  This is a GNU extension declared in malloc.h.

 -- Function: void memory_warnings (void *START, void (*WARN-FUNC) (const char *))
     Call this function to request warnings for nearing exhaustion of virtual memory.

     The argument START says where data space begins, in memory.  The allocator compares this against the last address used and against the limit of data space, to determine the fraction of available memory in use.  If you supply zero for START, then a default value is used which is right in most circumstances.

     For WARN-FUNC, supply a function that malloc can call to warn you.  It is called with a string (a warning message) as argument. Normally it ought to display the string for the user to read.

The warnings come when memory becomes 75% full, when it becomes 85% full, and when it becomes 95% full.  Above 95% you get another warning each time memory usage increases.