Replace all internal uses of __bzero with memset. This removes the need
[glibc.git] / string / bits / string2.h
blob6a26e2bc68070d5437dbb2c3dc665c32ba00350c
1 /* Machine-independant string function optimizations.
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _STRING_H
21 # error "Never use <bits/string2.h> directly; include <string.h> instead."
22 #endif
24 #ifndef __NO_STRING_INLINES
26 /* Unlike the definitions in the header <bits/string.h> the
27 definitions contained here are not optimized down to assembler
28 level. Those optimizations are not always a good idea since this
29 means the code size increases a lot. Instead the definitions here
30 optimize some functions in a way which do not dramatically
31 increase the code size and which do not use assembler. The main
32 trick is to use GCC's `__builtin_constant_p' function.
34 Every function XXX which has a defined version in
35 <bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
36 to make sure we don't get redefinitions.
38 We must use here macros instead of inline functions since the
39 trick won't work with the latter. */
41 #ifndef __STRING_INLINE
42 # ifdef __cplusplus
43 # define __STRING_INLINE inline
44 # else
45 # define __STRING_INLINE __extern_inline
46 # endif
47 #endif
49 /* Dereferencing a pointer arg to run sizeof on it fails for the void
50 pointer case, so we use this instead.
51 Note that __x is evaluated twice. */
52 #define __string2_1bptr_p(__x) \
53 ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
55 /* Set N bytes of S to 0. */
56 #if !defined _HAVE_STRING_ARCH_memset
57 # define __bzero(s, n) __builtin_memset (s, '\0', n)
58 #endif
61 /* Copy SRC to DEST, returning pointer to final NUL byte. */
62 #ifdef __USE_GNU
63 # ifndef _HAVE_STRING_ARCH_stpcpy
64 # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
65 /* In glibc we use this function frequently but for namespace reasons
66 we have to use the name `__stpcpy'. */
67 # define stpcpy(dest, src) __stpcpy (dest, src)
68 # endif
69 #endif
72 /* Copy no more than N characters of SRC to DEST. */
73 #ifndef _HAVE_STRING_ARCH_strncpy
74 # define strncpy(dest, src, n) __builtin_strncpy (dest, src, n)
75 #endif
78 /* Append no more than N characters from SRC onto DEST. */
79 #ifndef _HAVE_STRING_ARCH_strncat
80 # ifdef _USE_STRING_ARCH_strchr
81 # define strncat(dest, src, n) \
82 (__extension__ ({ char *__dest = (dest); \
83 __builtin_constant_p (src) && __builtin_constant_p (n) \
84 ? (strlen (src) < ((size_t) (n)) \
85 ? strcat (__dest, src) \
86 : (*((char *) __mempcpy (strchr (__dest, '\0'), \
87 src, n)) = '\0', __dest)) \
88 : strncat (dest, src, n); }))
89 # else
90 # define strncat(dest, src, n) __builtin_strncat (dest, src, n)
91 # endif
92 #endif
95 /* Return the length of the initial segment of S which
96 consists entirely of characters not in REJECT. */
97 #ifndef _HAVE_STRING_ARCH_strcspn
98 # define strcspn(s, reject) __builtin_strcspn (s, reject)
99 #endif
102 /* Return the length of the initial segment of S which
103 consists entirely of characters in ACCEPT. */
104 #ifndef _HAVE_STRING_ARCH_strspn
105 # define strspn(s, accept) __builtin_strspn (s, accept)
106 #endif
109 /* Find the first occurrence in S of any character in ACCEPT. */
110 #ifndef _HAVE_STRING_ARCH_strpbrk
111 # define strpbrk(s, accept) __builtin_strpbrk (s, accept)
112 #endif
115 #ifndef _FORCE_INLINES
116 # undef __STRING_INLINE
117 #endif
119 #endif /* No string inlines. */