annotate lib/malloca.h @ 10256:af15fcca9925

getdate.y: do not ignore TZ with relative day, month or year offset * lib/getdate.y (get_date): Move the tz-handling block to follow the relative-date-handling, since otherwise, the latter would clobber the sole output (an updated Start value) of the tz-handling block. * tests/test-getdate.c: Tests for the fix
author Ondřej Vašík <ovasik@redhat.com>
date Thu, 03 Jul 2008 12:17:11 +0200
parents c487592d112a
children 2d40c73ca3d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8930
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Safe automatic memory allocation.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2003-2007 Free Software Foundation, Inc.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #ifndef _MALLOCA_H
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #define _MALLOCA_H
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <alloca.h>
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stddef.h>
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdlib.h>
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #ifdef __cplusplus
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 extern "C" {
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 alloca(N); otherwise it returns NULL. It either returns N bytes of
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 memory allocated on the stack, that lasts until the function returns,
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 or NULL.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 Use of safe_alloca should be avoided:
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 - inside arguments of function calls - undefined behaviour,
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 - in inline functions - the allocation may actually last until the
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 calling function returns.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #if HAVE_ALLOCA
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* The OS usually guarantees only one guard page at the bottom of the stack,
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 and a page size can be as small as 4096 bytes. So we cannot safely
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 allocate anything larger than 4096 bytes. Also care for the possibility
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 of a few compiler-allocated temporary stack slots.
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 This must be a macro, not an inline function. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #else
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 # define safe_alloca(N) ((void) (N), NULL)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 memory allocated on the stack, that must be freed using freea() before
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 the function returns. Upon failure, it returns NULL. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 #if HAVE_ALLOCA
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # define malloca(N) \
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 ((N) < 4032 - sa_increment \
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 : mmalloca (N))
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 #else
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 # define malloca(N) \
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 mmalloca (N)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 extern void * mmalloca (size_t n);
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 /* Free a block of memory allocated through malloca(). */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 #if HAVE_ALLOCA
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 extern void freea (void *p);
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #else
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 # define freea free
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
9558
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
73 /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
74 It allocates an array of N objects, each with S bytes of memory,
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
75 on the stack. S must be positive and N must be nonnegative.
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
76 The array must be freed using freea() before the function returns. */
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
77 #if 1
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
78 /* Cf. the definition of xalloc_oversized. */
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
79 # define nmalloca(n, s) \
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
80 ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
81 ? NULL \
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
82 : malloca ((n) * (s)))
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
83 #else
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
84 extern void * nmalloca (size_t n, size_t s);
c487592d112a Protect against integer overflow in malloca() calls.
Bruno Haible <bruno@clisp.org>
parents: 8930
diff changeset
85 #endif
8930
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 #ifdef __cplusplus
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 }
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 /* ------------------- Auxiliary, non-public definitions ------------------- */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 /* Determine the alignment of a type at compile time. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 #if defined __GNUC__
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 # define sa_alignof __alignof__
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 #elif defined __cplusplus
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 #elif defined __hpux
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 values. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 #elif defined _AIX
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 values. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 #else
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 enum
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 {
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 /* The desired alignment of memory allocations is the maximum alignment
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 among all elementary types. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 sa_alignment_long = sa_alignof (long),
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 sa_alignment_double = sa_alignof (double),
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 #if HAVE_LONG_LONG_INT
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 sa_alignment_longlong = sa_alignof (long long),
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 sa_alignment_longdouble = sa_alignof (long double),
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 #if HAVE_LONG_LONG_INT
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 | (sa_alignment_longlong - 1)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 #endif
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 | (sa_alignment_longdouble - 1)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 ) + 1,
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 /* The increment that guarantees room for a magic word must be >= sizeof (int)
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 and a multiple of sa_alignment_max. */
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 };
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
4bbc062a8384 Move to here from allocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 #endif /* _MALLOCA_H */