source: mainline/boot/generic/src/memstr.c@ c77cfd8

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c77cfd8 was 10d65d70, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Use compiler-provided freestanding headers

Standard-compliant C compiler must provide these, so there is no point
in us trying to guess or repeat the correct contents.
This includes <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>,
<stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>.

<stdint.h> and <limits.h> need more work.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <memstr.h>
30#include <stddef.h>
31#include <stdint.h>
32
33/** Move memory block without overlapping.
34 *
35 * Copy cnt bytes from src address to dst address. The source
36 * and destination memory areas cannot overlap.
37 *
38 * @param dst Destination address to copy to.
39 * @param src Source address to copy from.
40 * @param cnt Number of bytes to copy.
41 *
42 * @return Destination address.
43 *
44 */
45void *memcpy(void *dst, const void *src, size_t cnt)
46{
47 uint8_t *dp = (uint8_t *) dst;
48 const uint8_t *sp = (uint8_t *) src;
49
50 while (cnt-- != 0)
51 *dp++ = *sp++;
52
53 return dst;
54}
55
56/** Fill block of memory.
57 *
58 * Fill cnt bytes at dst address with the value val.
59 *
60 * @param dst Destination address to fill.
61 * @param val Value to fill.
62 * @param cnt Number of bytes to fill.
63 *
64 * @return Destination address.
65 *
66 */
67void *memset(void *dst, int val, size_t cnt)
68{
69 uint8_t *dp = (uint8_t *) dst;
70
71 while (cnt-- != 0)
72 *dp++ = val;
73
74 return dst;
75}
76
77/** Move memory block with possible overlapping.
78 *
79 * Copy cnt bytes from src address to dst address. The source
80 * and destination memory areas may overlap.
81 *
82 * @param dst Destination address to copy to.
83 * @param src Source address to copy from.
84 * @param cnt Number of bytes to copy.
85 *
86 * @return Destination address.
87 *
88 */
89void *memmove(void *dst, const void *src, size_t cnt)
90{
91 /* Nothing to do? */
92 if (src == dst)
93 return dst;
94
95 /* Non-overlapping? */
96 if ((dst >= src + cnt) || (src >= dst + cnt))
97 return memcpy(dst, src, cnt);
98
99 uint8_t *dp;
100 const uint8_t *sp;
101
102 /* Which direction? */
103 if (src > dst) {
104 /* Forwards. */
105 dp = dst;
106 sp = src;
107
108 while (cnt-- != 0)
109 *dp++ = *sp++;
110 } else {
111 /* Backwards. */
112 dp = dst + (cnt - 1);
113 sp = src + (cnt - 1);
114
115 while (cnt-- != 0)
116 *dp-- = *sp--;
117 }
118
119 return dst;
120}
121
122/** @}
123 */
Note: See TracBrowser for help on using the repository browser.