1/* memcopy.h -- definitions for memory copy functions. Generic C version.
2 Copyright (C) 1991-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _MEMCOPY_H
20#define _MEMCOPY_H 1
21
22/* The strategy of the memory functions is:
23
24 1. Copy bytes until the destination pointer is aligned.
25
26 2. Copy words in unrolled loops. If the source and destination
27 are not aligned in the same way, use word memory operations,
28 but shift and merge two read words before writing.
29
30 3. Copy the few remaining bytes.
31
32 This is fast on processors that have at least 10 registers for
33 allocation by GCC, and that can access memory at reg+const in one
34 instruction.
35
36 I made an "exhaustive" test of this memmove when I wrote it,
37 exhaustive in the sense that I tried all alignment and length
38 combinations, with and without overlap. */
39
40#include <sys/cdefs.h>
41#include <endian.h>
42#include <pagecopy.h>
43
44/* The macros defined in this file are:
45
46 BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
47
48 BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
49
50 WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
51
52 WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
53
54 MERGE(old_word, sh_1, new_word, sh_2)
55 [I fail to understand. I feel stupid. --roland]
56*/
57
58/* Type to use for aligned memory operations. */
59#include <string-optype.h>
60#include <string-opthr.h>
61#define OPSIZ (sizeof (op_t))
62
63/* Type to use for unaligned operations. */
64typedef unsigned char byte;
65
66#if __BYTE_ORDER == __LITTLE_ENDIAN
67#define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
68#endif
69#if __BYTE_ORDER == __BIG_ENDIAN
70#define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
71#endif
72
73/* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
74 without any assumptions about alignment of the pointers. */
75#define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
76 do \
77 { \
78 size_t __nbytes = (nbytes); \
79 while (__nbytes > 0) \
80 { \
81 byte __x = ((byte *) src_bp)[0]; \
82 src_bp += 1; \
83 __nbytes -= 1; \
84 ((byte *) dst_bp)[0] = __x; \
85 dst_bp += 1; \
86 } \
87 } while (0)
88
89/* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
90 beginning at the bytes right before the pointers and continuing towards
91 smaller addresses. Don't assume anything about alignment of the
92 pointers. */
93#define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
94 do \
95 { \
96 size_t __nbytes = (nbytes); \
97 while (__nbytes > 0) \
98 { \
99 byte __x; \
100 src_ep -= 1; \
101 __x = ((byte *) src_ep)[0]; \
102 dst_ep -= 1; \
103 __nbytes -= 1; \
104 ((byte *) dst_ep)[0] = __x; \
105 } \
106 } while (0)
107
108/* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
109 the assumption that DST_BP is aligned on an OPSIZ multiple. If
110 not all bytes could be easily copied, store remaining number of bytes
111 in NBYTES_LEFT, otherwise store 0. */
112extern void _wordcopy_fwd_aligned (long int, long int, size_t)
113 attribute_hidden __THROW;
114extern void _wordcopy_fwd_dest_aligned (long int, long int, size_t)
115 attribute_hidden __THROW;
116#define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
117 do \
118 { \
119 if (src_bp % OPSIZ == 0) \
120 _wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
121 else \
122 _wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
123 src_bp += (nbytes) & -OPSIZ; \
124 dst_bp += (nbytes) & -OPSIZ; \
125 (nbytes_left) = (nbytes) % OPSIZ; \
126 } while (0)
127
128/* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
129 beginning at the words (of type op_t) right before the pointers and
130 continuing towards smaller addresses. May take advantage of that
131 DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
132 easily copied, store remaining number of bytes in NBYTES_REMAINING,
133 otherwise store 0. */
134extern void _wordcopy_bwd_aligned (long int, long int, size_t)
135 attribute_hidden __THROW;
136extern void _wordcopy_bwd_dest_aligned (long int, long int, size_t)
137 attribute_hidden __THROW;
138#define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
139 do \
140 { \
141 if (src_ep % OPSIZ == 0) \
142 _wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
143 else \
144 _wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
145 src_ep -= (nbytes) & -OPSIZ; \
146 dst_ep -= (nbytes) & -OPSIZ; \
147 (nbytes_left) = (nbytes) % OPSIZ; \
148 } while (0)
149
150/* The macro PAGE_COPY_FWD_MAYBE (dstp, srcp, nbytes_left, nbytes) is invoked
151 like WORD_COPY_FWD et al. The pointers should be at least word aligned.
152 This will check if virtual copying by pages can and should be done and do it
153 if so. The pointers will be aligned to PAGE_SIZE bytes. The macro requires
154 that pagecopy.h defines at least PAGE_COPY_THRESHOLD to 0. If
155 PAGE_COPY_THRESHOLD is non-zero, the header must also define PAGE_COPY_FWD
156 and PAGE_SIZE.
157*/
158#if PAGE_COPY_THRESHOLD
159
160# include <assert.h>
161
162# define PAGE_COPY_FWD_MAYBE(dstp, srcp, nbytes_left, nbytes) \
163 do \
164 { \
165 if ((nbytes) >= PAGE_COPY_THRESHOLD \
166 && PAGE_OFFSET ((dstp) - (srcp)) == 0) \
167 { \
168 /* The amount to copy is past the threshold for copying \
169 pages virtually with kernel VM operations, and the \
170 source and destination addresses have the same alignment. */ \
171 size_t nbytes_before = PAGE_OFFSET (-(dstp)); \
172 if (nbytes_before != 0) \
173 { \
174 /* First copy the words before the first page boundary. */ \
175 WORD_COPY_FWD (dstp, srcp, nbytes_left, nbytes_before); \
176 assert (nbytes_left == 0); \
177 nbytes -= nbytes_before; \
178 } \
179 PAGE_COPY_FWD (dstp, srcp, nbytes_left, nbytes); \
180 } \
181 } while (0)
182
183/* The page size is always a power of two, so we can avoid modulo division. */
184# define PAGE_OFFSET(n) ((n) & (PAGE_SIZE - 1))
185
186#else
187
188# define PAGE_COPY_FWD_MAYBE(dstp, srcp, nbytes_left, nbytes) /* nada */
189
190#endif
191
192/* Set to 1 if memcpy is safe to use for forward-copying memmove with
193 overlapping addresses. This is 0 by default because memcpy implementations
194 are generally not safe for overlapping addresses. */
195#define MEMCPY_OK_FOR_FWD_MEMMOVE 0
196
197#endif /* memcopy.h */
198

source code of glibc/sysdeps/generic/memcopy.h