117ca54c3Smrg/*
217ca54c3Smrg
317ca54c3SmrgCopyright 1995, 1998  The Open Group
417ca54c3Smrg
517ca54c3SmrgPermission to use, copy, modify, distribute, and sell this software and its
617ca54c3Smrgdocumentation for any purpose is hereby granted without fee, provided that
717ca54c3Smrgthe above copyright notice appear in all copies and that both that
817ca54c3Smrgcopyright notice and this permission notice appear in supporting
917ca54c3Smrgdocumentation.
1017ca54c3Smrg
1117ca54c3SmrgThe above copyright notice and this permission notice shall be
1217ca54c3Smrgincluded in all copies or substantial portions of the Software.
1317ca54c3Smrg
1417ca54c3SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1517ca54c3SmrgEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1617ca54c3SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1717ca54c3SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
1817ca54c3SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1917ca54c3SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2017ca54c3SmrgOTHER DEALINGS IN THE SOFTWARE.
2117ca54c3Smrg
2217ca54c3SmrgExcept as contained in this notice, the name of The Open Group shall
2317ca54c3Smrgnot be used in advertising or otherwise to promote the sale, use or
2417ca54c3Smrgother dealings in this Software without prior written authorization
2517ca54c3Smrgfrom The Open Group.
2617ca54c3Smrg
2717ca54c3Smrg*/
2817ca54c3Smrg/*
2917ca54c3Smrg * The purpose of this header is to define the macros ALLOCATE_LOCAL and
3017ca54c3Smrg * DEALLOCATE_LOCAL appropriately for the platform being compiled on.
3117ca54c3Smrg * These macros are used to make fast, function-local memory allocations.
3217ca54c3Smrg * Their characteristics are as follows:
3317ca54c3Smrg *
34ea1d6981Smrg * void *ALLOCATE_LOCAL(size_t size)
3517ca54c3Smrg *    Returns a pointer to size bytes of memory, or NULL if the allocation
3617ca54c3Smrg *    failed.  The memory must be freed with DEALLOCATE_LOCAL before the
3717ca54c3Smrg *    function that made the allocation returns.  You should not ask for
3817ca54c3Smrg *    large blocks of memory with this function, since on many platforms
3917ca54c3Smrg *    the memory comes from the stack, which may have limited size.
4017ca54c3Smrg *
4117ca54c3Smrg * void DEALLOCATE_LOCAL(void *)
4217ca54c3Smrg *    Frees the memory allocated by ALLOCATE_LOCAL.  Omission of this
4317ca54c3Smrg *    step may be harmless on some platforms, but will result in
4417ca54c3Smrg *    memory leaks or worse on others.
4517ca54c3Smrg *
4617ca54c3Smrg * Before including this file, you should define two macros,
4717ca54c3Smrg * ALLOCATE_LOCAL_FALLBACK and DEALLOCATE_LOCAL_FALLBACK, that have the
4817ca54c3Smrg * same characteristics as ALLOCATE_LOCAL and DEALLOCATE_LOCAL.  The
4917ca54c3Smrg * header uses the fallbacks if it doesn't know a "better" way to define
5017ca54c3Smrg * ALLOCATE_LOCAL and DEALLOCATE_LOCAL.  Typical usage would be:
5117ca54c3Smrg *
5217ca54c3Smrg *    #define ALLOCATE_LOCAL_FALLBACK(_size) malloc(_size)
5317ca54c3Smrg *    #define DEALLOCATE_LOCAL_FALLBACK(_ptr) free(_ptr)
5417ca54c3Smrg *    #include "Xalloca.h"
5517ca54c3Smrg */
5617ca54c3Smrg
5717ca54c3Smrg#ifndef XALLOCA_H
5817ca54c3Smrg#define XALLOCA_H 1
5917ca54c3Smrg
6017ca54c3Smrg#ifndef INCLUDE_ALLOCA_H
6117ca54c3Smrg/* Need to add more here to match Imake *.cf's */
6217ca54c3Smrg# if defined(HAVE_ALLOCA_H) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
6317ca54c3Smrg#  define INCLUDE_ALLOCA_H
6417ca54c3Smrg# endif
6517ca54c3Smrg#endif
6617ca54c3Smrg
6717ca54c3Smrg#ifdef INCLUDE_ALLOCA_H
6817ca54c3Smrg#  include <alloca.h>
6917ca54c3Smrg#endif
7017ca54c3Smrg
7117ca54c3Smrg#ifndef NO_ALLOCA
7217ca54c3Smrg/*
7317ca54c3Smrg * os-dependent definition of local allocation and deallocation
7417ca54c3Smrg * If you want something other than (DE)ALLOCATE_LOCAL_FALLBACK
7517ca54c3Smrg * for ALLOCATE/DEALLOCATE_LOCAL then you add that in here.
7617ca54c3Smrg */
7717ca54c3Smrg
7817ca54c3Smrg
7917ca54c3Smrg#  ifdef __GNUC__
8017ca54c3Smrg#    ifndef alloca
8117ca54c3Smrg#      define alloca __builtin_alloca
8217ca54c3Smrg#    endif /* !alloca */
83ea1d6981Smrg#    define ALLOCATE_LOCAL(size) alloca((size_t)(size))
8417ca54c3Smrg#  else /* ! __GNUC__ */
8517ca54c3Smrg
8617ca54c3Smrg/*
8717ca54c3Smrg * warning: old mips alloca (pre 2.10) is unusable, new one is built in
8817ca54c3Smrg * Test is easy, the new one is named __builtin_alloca and comes
8917ca54c3Smrg * from alloca.h which #defines alloca.
9017ca54c3Smrg */
9117ca54c3Smrg#      if defined(__sun) || defined(alloca)
9217ca54c3Smrg/*
9317ca54c3Smrg * Some System V boxes extract alloca.o from /lib/libPW.a; if you
9417ca54c3Smrg * decide that you don't want to use alloca, you might want to fix it here.
9517ca54c3Smrg */
9617ca54c3Smrg/* alloca might be a macro taking one arg (hi, Sun!), so give it one. */
9717ca54c3Smrg#        if !defined(__cplusplus)
9817ca54c3Smrg#          define __Xnullarg		/* as nothing */
9917ca54c3Smrg           extern void *alloca(__Xnullarg);
10017ca54c3Smrg#        endif
101ea1d6981Smrg#        define ALLOCATE_LOCAL(size) alloca((size_t)(size))
10217ca54c3Smrg#      endif /* who does alloca */
10317ca54c3Smrg#  endif /* __GNUC__ */
10417ca54c3Smrg
10517ca54c3Smrg#endif /* NO_ALLOCA */
10617ca54c3Smrg
10717ca54c3Smrg#if !defined(ALLOCATE_LOCAL)
10817ca54c3Smrg#  if defined(ALLOCATE_LOCAL_FALLBACK) && defined(DEALLOCATE_LOCAL_FALLBACK)
10917ca54c3Smrg#    define ALLOCATE_LOCAL(_size)  ALLOCATE_LOCAL_FALLBACK(_size)
11017ca54c3Smrg#    define DEALLOCATE_LOCAL(_ptr) DEALLOCATE_LOCAL_FALLBACK(_ptr)
11117ca54c3Smrg#  else /* no fallbacks supplied; error */
11217ca54c3Smrg#    define ALLOCATE_LOCAL(_size)  ALLOCATE_LOCAL_FALLBACK undefined!
11317ca54c3Smrg#    define DEALLOCATE_LOCAL(_ptr) DEALLOCATE_LOCAL_FALLBACK undefined!
11417ca54c3Smrg#  endif /* defined(ALLOCATE_LOCAL_FALLBACK && DEALLOCATE_LOCAL_FALLBACK) */
11517ca54c3Smrg#else
11617ca54c3Smrg#  if !defined(DEALLOCATE_LOCAL)
11717ca54c3Smrg#    define DEALLOCATE_LOCAL(_ptr) do {} while(0)
11817ca54c3Smrg#  endif
11917ca54c3Smrg#endif /* defined(ALLOCATE_LOCAL) */
12017ca54c3Smrg
12117ca54c3Smrg#endif /* XALLOCA_H */
122