Back to index

nagios-plugins  1.4.16
malloca.h
Go to the documentation of this file.
00001 /* Safe automatic memory allocation.
00002    Copyright (C) 2003-2007, 2009-2010 Free Software Foundation, Inc.
00003    Written by Bruno Haible <bruno@clisp.org>, 2003.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 #ifndef _MALLOCA_H
00020 #define _MALLOCA_H
00021 
00022 #include <alloca.h>
00023 #include <stddef.h>
00024 #include <stdlib.h>
00025 
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00031 
00032 /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
00033    alloca(N); otherwise it returns NULL.  It either returns N bytes of
00034    memory allocated on the stack, that lasts until the function returns,
00035    or NULL.
00036    Use of safe_alloca should be avoided:
00037      - inside arguments of function calls - undefined behaviour,
00038      - in inline functions - the allocation may actually last until the
00039        calling function returns.
00040 */
00041 #if HAVE_ALLOCA
00042 /* The OS usually guarantees only one guard page at the bottom of the stack,
00043    and a page size can be as small as 4096 bytes.  So we cannot safely
00044    allocate anything larger than 4096 bytes.  Also care for the possibility
00045    of a few compiler-allocated temporary stack slots.
00046    This must be a macro, not an inline function.  */
00047 # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
00048 #else
00049 # define safe_alloca(N) ((void) (N), NULL)
00050 #endif
00051 
00052 /* malloca(N) is a safe variant of alloca(N).  It allocates N bytes of
00053    memory allocated on the stack, that must be freed using freea() before
00054    the function returns.  Upon failure, it returns NULL.  */
00055 #if HAVE_ALLOCA
00056 # define malloca(N) \
00057   ((N) < 4032 - sa_increment                                        \
00058    ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
00059    : mmalloca (N))
00060 #else
00061 # define malloca(N) \
00062   mmalloca (N)
00063 #endif
00064 extern void * mmalloca (size_t n);
00065 
00066 /* Free a block of memory allocated through malloca().  */
00067 #if HAVE_ALLOCA
00068 extern void freea (void *p);
00069 #else
00070 # define freea free
00071 #endif
00072 
00073 /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
00074    It allocates an array of N objects, each with S bytes of memory,
00075    on the stack.  S must be positive and N must be nonnegative.
00076    The array must be freed using freea() before the function returns.  */
00077 #if 1
00078 /* Cf. the definition of xalloc_oversized.  */
00079 # define nmalloca(n, s) \
00080     ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \
00081      ? NULL \
00082      : malloca ((n) * (s)))
00083 #else
00084 extern void * nmalloca (size_t n, size_t s);
00085 #endif
00086 
00087 
00088 #ifdef __cplusplus
00089 }
00090 #endif
00091 
00092 
00093 /* ------------------- Auxiliary, non-public definitions ------------------- */
00094 
00095 /* Determine the alignment of a type at compile time.  */
00096 #if defined __GNUC__
00097 # define sa_alignof __alignof__
00098 #elif defined __cplusplus
00099   template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
00100 # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
00101 #elif defined __hpux
00102   /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
00103      values.  */
00104 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
00105 #elif defined _AIX
00106   /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
00107      values.  */
00108 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
00109 #else
00110 # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
00111 #endif
00112 
00113 enum
00114 {
00115 /* The desired alignment of memory allocations is the maximum alignment
00116    among all elementary types.  */
00117   sa_alignment_long = sa_alignof (long),
00118   sa_alignment_double = sa_alignof (double),
00119 #if HAVE_LONG_LONG_INT
00120   sa_alignment_longlong = sa_alignof (long long),
00121 #endif
00122   sa_alignment_longdouble = sa_alignof (long double),
00123   sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
00124 #if HAVE_LONG_LONG_INT
00125                       | (sa_alignment_longlong - 1)
00126 #endif
00127                       | (sa_alignment_longdouble - 1)
00128                      ) + 1,
00129 /* The increment that guarantees room for a magic word must be >= sizeof (int)
00130    and a multiple of sa_alignment_max.  */
00131   sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
00132 };
00133 
00134 #endif /* _MALLOCA_H */