1 | /* $Id: memmove.cpp 106917 2024-11-10 02:26:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - CRT Strings, memmove().
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/string.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Copies a memory.
|
---|
47 | *
|
---|
48 | * @returns pvDst.
|
---|
49 | * @param pvDst Pointer to the target block.
|
---|
50 | * @param pvSrc Pointer to the source block.
|
---|
51 | * @param cb The size of the block.
|
---|
52 | */
|
---|
53 | #ifdef IPRT_NO_CRT
|
---|
54 | # undef memmove
|
---|
55 | void *RT_NOCRT(memmove)(void *pvDst, const void *pvSrc, size_t cb)
|
---|
56 | #elif RT_MSC_PREREQ(RT_MSC_VER_VS2005)
|
---|
57 | _CRT_INSECURE_DEPRECATE_MEMORY(memmove_s) void * __cdecl
|
---|
58 | memmove(__out_bcount_full_opt(_Size) void *pvDst, __in_bcount_opt(_Size) const void *pvSrc, __in size_t cb)
|
---|
59 | #else
|
---|
60 | void *memmove(void *pvDst, const void *pvSrc, size_t cb)
|
---|
61 | #endif
|
---|
62 | {
|
---|
63 | union
|
---|
64 | {
|
---|
65 | uint8_t volatile *pu8;
|
---|
66 | size_t volatile *puz;
|
---|
67 | void *pv;
|
---|
68 | uintptr_t uPtr;
|
---|
69 | } uTrg;
|
---|
70 | uTrg.pv = pvDst;
|
---|
71 |
|
---|
72 | union
|
---|
73 | {
|
---|
74 | uint8_t const volatile *pu8;
|
---|
75 | size_t const volatile *puz;
|
---|
76 | void const *pv;
|
---|
77 | uintptr_t uPtr;
|
---|
78 | } uSrc;
|
---|
79 | uSrc.pv = pvSrc;
|
---|
80 |
|
---|
81 | if (uTrg.uPtr < uSrc.uPtr || uTrg.uPtr >= uSrc.uPtr + cb)
|
---|
82 | {
|
---|
83 | /*
|
---|
84 | * Copy forwards.
|
---|
85 | */
|
---|
86 | /* size word moves. */
|
---|
87 | size_t c = cb / sizeof(size_t);
|
---|
88 | while (c-- > 0)
|
---|
89 | *uTrg.puz++ = *uSrc.puz++;
|
---|
90 |
|
---|
91 | /* Remaining byte moves. */
|
---|
92 | c = cb % sizeof(*uTrg.puz);
|
---|
93 | while (c-- > 0)
|
---|
94 | *uTrg.pu8++ = *uSrc.pu8++;
|
---|
95 | }
|
---|
96 | else if (uTrg.uPtr != uSrc.uPtr)
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | * Copy backwards.
|
---|
100 | */
|
---|
101 | uTrg.uPtr += cb;
|
---|
102 | uSrc.uPtr += cb;
|
---|
103 |
|
---|
104 | /* Byte stuff. */
|
---|
105 | size_t c = cb % sizeof(*uTrg.puz);
|
---|
106 | while (c-- > 0)
|
---|
107 | *--uTrg.pu8 = *--uSrc.pu8;
|
---|
108 |
|
---|
109 | /* size word moves. */
|
---|
110 | c = cb / sizeof(size_t);
|
---|
111 | while (c-- > 0)
|
---|
112 | *--uTrg.puz = *--uSrc.puz;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return pvDst;
|
---|
116 | }
|
---|
117 | RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(memmove);
|
---|
118 |
|
---|