source: mainline/boot/generic/src/memstr.c@ 1715b7fe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1715b7fe was 1715b7fe, checked in by Martin Decky <martin@…>, 13 years ago

implement memcpy() and memset() using builtin functions

  • Property mode set to 100644
File size: 3.0 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 <typedefs.h>
31
32/** Copy block of memory.
33 *
34 * Copy cnt bytes from src address to dst address.The source and destination
35 * memory areas cannot overlap.
36 *
37 * @param src Source address to copy from.
38 * @param dst Destination address to copy to.
39 * @param cnt Number of bytes to copy.
40 *
41 * @return Destination address.
42 *
43 */
44void *memcpy(void *dst, const void *src, size_t cnt)
45{
46 return __builtin_memcpy(dst, src, cnt);
47}
48
49/** Fill block of memory.
50 *
51 * Fill cnt bytes at dst address with the value val.
52 *
53 * @param dst Destination address to fill.
54 * @param val Value to fill.
55 * @param cnt Number of bytes to fill.
56 *
57 * @return Destination address.
58 *
59 */
60void *memset(void *dst, int val, size_t cnt)
61{
62 return __builtin_memset(dst, val, cnt);
63}
64
65/** Move memory block with possible overlapping.
66 *
67 * Copy cnt bytes from src address to dst address. The source
68 * and destination memory areas may overlap.
69 *
70 * @param dst Destination address to copy to.
71 * @param src Source address to copy from.
72 * @param cnt Number of bytes to copy.
73 *
74 * @return Destination address.
75 *
76 */
77void *memmove(void *dst, const void *src, size_t cnt)
78{
79 /* Nothing to do? */
80 if (src == dst)
81 return dst;
82
83 /* Non-overlapping? */
84 if ((dst >= src + cnt) || (src >= dst + cnt))
85 return memcpy(dst, src, cnt);
86
87 uint8_t *dp;
88 const uint8_t *sp;
89
90 /* Which direction? */
91 if (src > dst) {
92 /* Forwards. */
93 dp = dst;
94 sp = src;
95
96 while (cnt-- != 0)
97 *dp++ = *sp++;
98 } else {
99 /* Backwards. */
100 dp = dst + (cnt - 1);
101 sp = src + (cnt - 1);
102
103 while (cnt-- != 0)
104 *dp-- = *sp--;
105 }
106
107 return dst;
108}
109
110/** @}
111 */
Note: See TracBrowser for help on using the repository browser.