* misc/sys/cdefs.h (__va_arg_pack): Define for GCC 4.3+.
[glibc.git] / string / bits / string3.h
blob876fe77929d02480fa328e40241e529d1c1a9437
1 /* Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #ifndef _STRING_H
20 # error "Never use <bits/string3.h> directly; include <string.h> instead."
21 #endif
23 __warndecl (__warn_memset_zero_len,
24 "memset used with constant zero length parameter; this could be due to transposed parameters");
26 #ifndef __cplusplus
27 /* XXX This is temporarily. We should not redefine any of the symbols
28 and instead integrate the error checking into the original
29 definitions. */
30 # undef memcpy
31 # undef memmove
32 # undef memset
33 # undef strcat
34 # undef strcpy
35 # undef strncat
36 # undef strncpy
37 # ifdef __USE_GNU
38 # undef mempcpy
39 # undef stpcpy
40 # endif
41 # ifdef __USE_BSD
42 # undef bcopy
43 # undef bzero
44 # endif
45 #endif
48 #ifdef __cplusplus
49 __extern_always_inline void *
50 __NTH (memcpy (void *__restrict __dest, __const void *__restrict __src,
51 size_t __len))
53 return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
55 #else
56 # define memcpy(dest, src, len) \
57 ((__bos0 (dest) != (size_t) -1) \
58 ? __builtin___memcpy_chk (dest, src, len, __bos0 (dest)) \
59 : __memcpy_ichk (dest, src, len))
60 static __always_inline void *
61 __NTH (__memcpy_ichk (void *__restrict __dest, __const void *__restrict __src,
62 size_t __len))
64 return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
66 #endif
68 #ifdef __cplusplus
69 __extern_always_inline void *
70 __NTH (memmove (void *__restrict __dest, __const void *__restrict __src,
71 size_t __len))
73 return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
75 #else
76 # define memmove(dest, src, len) \
77 ((__bos0 (dest) != (size_t) -1) \
78 ? __builtin___memmove_chk (dest, src, len, __bos0 (dest)) \
79 : __memmove_ichk (dest, src, len))
80 static __always_inline void *
81 __NTH (__memmove_ichk (void *__dest, __const void *__src, size_t __len))
83 return __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
85 #endif
87 #ifdef __USE_GNU
88 # ifdef __cplusplus
89 __extern_always_inline void *
90 __NTH (mempcpy (void *__restrict __dest, __const void *__restrict __src,
91 size_t __len))
93 return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
95 # else
96 # define mempcpy(dest, src, len) \
97 ((__bos0 (dest) != (size_t) -1) \
98 ? __builtin___mempcpy_chk (dest, src, len, __bos0 (dest)) \
99 : __mempcpy_ichk (dest, src, len))
100 static __always_inline void *
101 __NTH (__mempcpy_ichk (void *__restrict __dest,
102 __const void *__restrict __src, size_t __len))
104 return __builtin___mempcpy_chk (__dest, __src, __len, __bos0 (__dest));
106 # endif
107 #endif
110 /* The first two tests here help to catch a somewhat common problem
111 where the second and third parameter are transposed. This is
112 especially problematic if the intended fill value is zero. In this
113 case no work is done at all. We detect these problems by referring
114 non-existing functions. */
115 #ifdef __cplusplus
116 __extern_always_inline void *
117 __NTH (memset (void *__dest, int __ch, size_t __len))
119 if (__builtin_constant_p (__len) && __len == 0)
121 __warn_memset_zero_len ();
122 return __dest;
124 return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
126 #else
127 # define memset(dest, ch, len) \
128 (__builtin_constant_p (len) && (len) == 0 \
129 ? (__warn_memset_zero_len (), (void) (ch), (void) (len), (void *) (dest)) \
130 : ((__bos0 (dest) != (size_t) -1) \
131 ? __builtin___memset_chk (dest, ch, len, __bos0 (dest)) \
132 : __memset_ichk (dest, ch, len)))
133 static __always_inline void *
134 __NTH (__memset_ichk (void *__dest, int __ch, size_t __len))
136 return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
138 #endif
140 #ifdef __USE_BSD
141 # ifdef __cplusplus
142 __extern_always_inline void
143 __NTH (bcopy (__const void *__restrict __src, void *__restrict __dest,
144 size_t __len))
146 __builtin___memmove_chk (__dest, __src, __len, __bos0 (__dest));
148 __extern_always_inline void
149 __NTH (bzero (void *__dest, size_t __len))
151 __builtin___memset_chk (__dest, '\0', __len, __bos0 (__dest));
153 # else
154 # define bcopy(src, dest, len) ((void) \
155 ((__bos0 (dest) != (size_t) -1) \
156 ? __builtin___memmove_chk (dest, src, len, __bos0 (dest)) \
157 : __memmove_ichk (dest, src, len)))
158 # define bzero(dest, len) ((void) \
159 ((__bos0 (dest) != (size_t) -1) \
160 ? __builtin___memset_chk (dest, '\0', len, __bos0 (dest)) \
161 : __memset_ichk (dest, '\0', len)))
162 # endif
163 #endif
165 #ifdef __cplusplus
166 __extern_always_inline char *
167 __NTH (strcpy (char *__restrict __dest, __const char *__restrict __src))
169 return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
171 #else
172 # define strcpy(dest, src) \
173 ((__bos (dest) != (size_t) -1) \
174 ? __builtin___strcpy_chk (dest, src, __bos (dest)) \
175 : __strcpy_ichk (dest, src))
176 static __always_inline char *
177 __NTH (__strcpy_ichk (char *__restrict __dest, __const char *__restrict __src))
179 return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
181 #endif
183 #ifdef __USE_GNU
184 # ifdef __cplusplus
185 __extern_always_inline char *
186 __NTH (stpcpy (char *__restrict __dest, __const char *__restrict __src))
188 return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
190 # else
191 # define stpcpy(dest, src) \
192 ((__bos (dest) != (size_t) -1) \
193 ? __builtin___stpcpy_chk (dest, src, __bos (dest)) \
194 : __stpcpy_ichk (dest, src))
195 static __always_inline char *
196 __NTH (__stpcpy_ichk (char *__restrict __dest, __const char *__restrict __src))
198 return __builtin___stpcpy_chk (__dest, __src, __bos (__dest));
200 # endif
201 #endif
204 #ifdef __cplusplus
205 __extern_always_inline char *
206 __NTH (strncpy (char *__restrict __dest, __const char *__restrict __src,
207 size_t __len))
209 return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
211 #else
212 # define strncpy(dest, src, len) \
213 ((__bos (dest) != (size_t) -1) \
214 ? __builtin___strncpy_chk (dest, src, len, __bos (dest)) \
215 : __strncpy_ichk (dest, src, len))
216 static __always_inline char *
217 __NTH (__strncpy_ichk (char *__restrict __dest, __const char *__restrict __src,
218 size_t __len))
220 return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
222 #endif
224 // XXX We have no corresponding builtin yet.
225 extern char *__stpncpy_chk (char *__dest, __const char *__src, size_t __n,
226 size_t __destlen) __THROW;
227 extern char *__REDIRECT_NTH (__stpncpy_alias, (char *__dest,
228 __const char *__src,
229 size_t __n), stpncpy);
231 __extern_always_inline char *
232 __NTH (stpncpy (char *__dest, __const char *__src, size_t __n))
234 if (__bos (__dest) != (size_t) -1
235 && (!__builtin_constant_p (__n) || __n <= __bos (__dest)))
236 return __stpncpy_chk (__dest, __src, __n, __bos (__dest));
237 return __stpncpy_alias (__dest, __src, __n);
241 #ifdef __cplusplus
242 __extern_always_inline char *
243 __NTH (strcat (char *__restrict __dest, __const char *__restrict __src))
245 return __builtin___strcat_chk (__dest, __src, __bos (__dest));
247 #else
248 # define strcat(dest, src) \
249 ((__bos (dest) != (size_t) -1) \
250 ? __builtin___strcat_chk (dest, src, __bos (dest)) \
251 : __strcat_ichk (dest, src))
252 static __always_inline char *
253 __NTH (__strcat_ichk (char *__restrict __dest, __const char *__restrict __src))
255 return __builtin___strcat_chk (__dest, __src, __bos (__dest));
257 #endif
260 #ifdef __cplusplus
261 __extern_always_inline char *
262 __NTH (strncat (char *__restrict __dest, __const char *__restrict __src,
263 size_t __len))
265 return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
267 #else
268 # define strncat(dest, src, len) \
269 ((__bos (dest) != (size_t) -1) \
270 ? __builtin___strncat_chk (dest, src, len, __bos (dest)) \
271 : __strncat_ichk (dest, src, len))
272 static __always_inline char *
273 __NTH (__strncat_ichk (char *__restrict __dest, __const char *__restrict __src,
274 size_t __len))
276 return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
278 #endif