source: mainline/boot/generic/src/memstr.c@ 39916d6

Last change on this file since 39916d6 was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2010 Jiri Svoboda
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <memstr.h>
8#include <stddef.h>
9#include <stdint.h>
10
11/** Move memory block without overlapping.
12 *
13 * Copy cnt bytes from src address to dst address. The source
14 * and destination memory areas cannot overlap.
15 *
16 * @param dst Destination address to copy to.
17 * @param src Source address to copy from.
18 * @param cnt Number of bytes to copy.
19 *
20 * @return Destination address.
21 *
22 */
23void *memcpy(void *dst, const void *src, size_t cnt)
24{
25 uint8_t *dp = (uint8_t *) dst;
26 const uint8_t *sp = (uint8_t *) src;
27
28 while (cnt-- != 0)
29 *dp++ = *sp++;
30
31 return dst;
32}
33
34/** Fill block of memory.
35 *
36 * Fill cnt bytes at dst address with the value val.
37 *
38 * @param dst Destination address to fill.
39 * @param val Value to fill.
40 * @param cnt Number of bytes to fill.
41 *
42 * @return Destination address.
43 *
44 */
45void *memset(void *dst, int val, size_t cnt)
46{
47 uint8_t *dp = (uint8_t *) dst;
48
49 while (cnt-- != 0)
50 *dp++ = val;
51
52 return dst;
53}
54
55/** Move memory block with possible overlapping.
56 *
57 * Copy cnt bytes from src address to dst address. The source
58 * and destination memory areas may overlap.
59 *
60 * @param dst Destination address to copy to.
61 * @param src Source address to copy from.
62 * @param cnt Number of bytes to copy.
63 *
64 * @return Destination address.
65 *
66 */
67void *memmove(void *dst, const void *src, size_t cnt)
68{
69 /* Nothing to do? */
70 if (src == dst)
71 return dst;
72
73 /* Non-overlapping? */
74 if ((dst >= src + cnt) || (src >= dst + cnt))
75 return memcpy(dst, src, cnt);
76
77 uint8_t *dp;
78 const uint8_t *sp;
79
80 /* Which direction? */
81 if (src > dst) {
82 /* Forwards. */
83 dp = dst;
84 sp = src;
85
86 while (cnt-- != 0)
87 *dp++ = *sp++;
88 } else {
89 /* Backwards. */
90 dp = dst + (cnt - 1);
91 sp = src + (cnt - 1);
92
93 while (cnt-- != 0)
94 *dp-- = *sp--;
95 }
96
97 return dst;
98}
99
100/** @}
101 */
Note: See TracBrowser for help on using the repository browser.