ncdf4a13/libsrc/onstack.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright 1997, University Corporation for Atmospheric Research
00003  *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
00004  */
00005 /* $Id: onstack.h,v 2.6 1997/12/18 20:11:01 davis Exp $ */
00006 
00007 #ifndef _ONSTACK_H_
00008 #define _ONSTACK_H_
00009 /* include after ncconfig.h */
00010 /**
00011  * This file provides definitions which allow us to
00012  * "allocate" arrays on the stack where possible.
00013  * (Where not possible, malloc and free are used.)
00014  *
00015  * The macro ALLOC_ONSTACK(name, type, nelems) is used to declare 
00016  * an array of 'type' named 'name' which is 'nelems' long.
00017  * FREE_ONSTACK(name) is placed at the end of the scope of 'name'
00018  * to call 'free' if necessary.
00019  * 
00020  * The macro ALLOC_ONSTACK wraps a call to alloca() on most systems.
00021  */
00022 
00023 #if HAVE_ALLOCA
00024 /*
00025  * Implementation based on alloca()
00026  */
00027 
00028 #if defined(__GNUC__)
00029 # if !defined(alloca)
00030 # define alloca __builtin_alloca
00031 # endif
00032 #else
00033 # if HAVE_ALLOCA_H
00034 #  include <alloca.h>
00035 # elif defined(_AIX)
00036 #  pragma alloca
00037 # endif /* HAVE_ALLOCA_H */
00038 #endif /* __GNUC__ */
00039 
00040 # if !defined(ALLOCA_ARG_T)
00041 # define ALLOCA_ARG_T int /* the usual type of the alloca argument */
00042 # endif
00043 
00044 # define ALLOC_ONSTACK(name, type, nelems) \
00045         type *const name = (type *) alloca((ALLOCA_ARG_T)((nelems) * sizeof(type)))
00046 
00047 # define FREE_ONSTACK(name)
00048 
00049 #elif defined(_CRAYC) && !__cplusplus && __STDC__ > 1
00050 /*
00051  * Cray C allows sizing of arrays with non-constant values.
00052  */
00053 
00054 # define ALLOC_ONSTACK(name, type, nelems) \
00055         type name[nelems]
00056 
00057 # define FREE_ONSTACK(name)
00058 
00059 #else
00060 /*
00061  * Default implementation. When all else fails, use malloc/free.
00062  */
00063 
00064 # define ALLOC_ONSTACK(name, type, nelems) \
00065         type *const name = (type *) malloc((nelems) * sizeof(type))
00066 
00067 # define FREE_ONSTACK(name) \
00068         free(name)
00069 
00070 #endif
00071         
00072 #endif /* _ONSTACK_H_ */

Generated on Thu Mar 16 18:10:09 2006 for nco by  doxygen 1.4.4