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