TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crt_memmove.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <string.h>
9 #include "crt_impl_private.h"
10 
11 static void *memcpy_r(void *dest, const void *src, size_t n)
12 {
13  union tfm_mem_addr_t p_dest, p_src;
14 
15  p_dest.uint_addr = (uintptr_t)dest + n;
16  p_src.uint_addr = (uintptr_t)src + n;
17 
18  /* Byte copy for unaligned address. check the last bit of address. */
19  while (n && (GET_MEM_ADDR_BIT0(p_dest.uint_addr) ||
20  GET_MEM_ADDR_BIT0(p_src.uint_addr))) {
21  *(--p_dest.p_byte) = *(--p_src.p_byte);
22  n--;
23  }
24 
25  /* Double byte copy for aligned address.
26  * Check the 2nd last bit of address.
27  */
28  while (n >= sizeof(uint16_t) && (GET_MEM_ADDR_BIT1(p_dest.uint_addr) ||
29  GET_MEM_ADDR_BIT1(p_src.uint_addr))) {
30  *(--p_dest.p_dbyte) = *(--p_src.p_dbyte);
31  n -= sizeof(uint16_t);
32  }
33 
34  /* Quad byte copy for aligned address. */
35  while (n >= sizeof(uint32_t)) {
36  *(--p_dest.p_qbyte) = *(--p_src.p_qbyte);
37  n -= sizeof(uint32_t);
38  }
39 
40  /* Byte copy for the remaining bytes. */
41  while (n--) {
42  *(--p_dest.p_byte) = *(--p_src.p_byte);
43  }
44 
45  return dest;
46 }
47 
48 /*
49  * For overlapped memory area:
50  * 1) overlapped: use reverse memory move.
51  * 2) non-overlapped: use forward memory move.
52  */
53 void *memmove(void *dest, const void *src, size_t n)
54 {
55  /*
56  * FixMe: Add a "assert (dest == NULL || src == NULL)" here
57  * after "assert()" for sprtl is implemented.
58  */
59  if (src >= dest) {
60  memcpy(dest, src, n);
61  } else {
62  memcpy_r(dest, src, n);
63  }
64 
65  return dest;
66 }
#define GET_MEM_ADDR_BIT1(x)
uintptr_t uint_addr
#define GET_MEM_ADDR_BIT0(x)
void * memcpy(void *dest, const void *src, size_t n)
Definition: crt_memcpy.c:10
void * memmove(void *dest, const void *src, size_t n)
Definition: crt_memmove.c:53