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