3 <<memmove>>---move possibly overlapping memory
10 void *memmove(void *<[dst]>, const void *<[src]>, size_t <[length]>);
13 This function moves <[length]> characters from the block of
14 memory starting at <<*<[src]>>> to the memory starting at
15 <<*<[dst]>>>. <<memmove>> reproduces the characters correctly
16 at <<*<[dst]>>> even if the two areas overlap.
20 The function returns <[dst]> as passed.
23 <<memmove>> is ANSI C.
25 <<memmove>> requires no supporting OS subroutines.
37 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
38 #define UNALIGNED(X, Y) \
39 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
41 /* How many bytes are copied each iteration of the 4X unrolled loop. */
42 #define BIGBLOCKSIZE (sizeof (long) << 2)
44 /* How many bytes are copied each iteration of the word copy loop. */
45 #define LITTLEBLOCKSIZE (sizeof (long))
47 /* Threshhold for punting to the byte copier. */
48 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
52 __inhibit_loop_to_libcall
53 memmove (void *dst_void
,
57 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
59 const char *src
= src_void
;
61 if (src
< dst
&& dst
< src
+ length
)
63 /* Have to copy backwards */
82 const char *src
= src_void
;
84 const long *aligned_src
;
86 if (src
< dst
&& dst
< src
+ length
)
88 /* Destructive overlap...have to copy backwards */
98 /* Use optimizing algorithm for a non-destructive copy to closely
99 match memcpy. If the size is small or either SRC or DST is unaligned,
100 then punt into the byte copy loop. This should be rare. */
101 if (!TOO_SMALL(length
) && !UNALIGNED (src
, dst
))
103 aligned_dst
= (long*)dst
;
104 aligned_src
= (long*)src
;
106 /* Copy 4X long words at a time if possible. */
107 while (length
>= BIGBLOCKSIZE
)
109 *aligned_dst
++ = *aligned_src
++;
110 *aligned_dst
++ = *aligned_src
++;
111 *aligned_dst
++ = *aligned_src
++;
112 *aligned_dst
++ = *aligned_src
++;
113 length
-= BIGBLOCKSIZE
;
116 /* Copy one long word at a time if possible. */
117 while (length
>= LITTLEBLOCKSIZE
)
119 *aligned_dst
++ = *aligned_src
++;
120 length
-= LITTLEBLOCKSIZE
;
123 /* Pick up any residual with a byte copier. */
124 dst
= (char*)aligned_dst
;
125 src
= (char*)aligned_src
;
135 #endif /* not PREFER_SIZE_OVER_SPEED */