Rev 3750 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3750 | Rev 4011 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * Copyright (c) 2008 Jiri Svoboda |
3 | * Copyright (c) 2008 Jiri Svoboda |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | /** @addtogroup generic |
30 | /** @addtogroup generic |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | 33 | ||
34 | /** |
34 | /** |
35 | * @file |
35 | * @file |
36 | * @brief Memory string operations. |
36 | * @brief Memory string operations. |
37 | * |
37 | * |
38 | * This file provides architecture independent functions to manipulate blocks of |
38 | * This file provides architecture independent functions to manipulate blocks of |
39 | * memory. These functions are optimized as much as generic functions of this |
39 | * memory. These functions are optimized as much as generic functions of this |
40 | * type can be. However, architectures are free to provide even more optimized |
40 | * type can be. However, architectures are free to provide even more optimized |
41 | * versions of these functions. |
41 | * versions of these functions. |
42 | */ |
42 | */ |
43 | 43 | ||
44 | #include <memstr.h> |
44 | #include <memstr.h> |
45 | #include <arch/types.h> |
45 | #include <arch/types.h> |
46 | #include <align.h> |
46 | #include <align.h> |
47 | 47 | ||
48 | /** Copy block of memory. |
48 | /** Copy block of memory. |
49 | * |
49 | * |
50 | * Copy cnt bytes from src address to dst address. The copying is done |
50 | * Copy cnt bytes from src address to dst address. The copying is done |
51 | * word-by-word and then byte-by-byte. The source and destination memory areas |
51 | * word-by-word and then byte-by-byte. The source and destination memory areas |
52 | * cannot overlap. |
52 | * cannot overlap. |
53 | * |
53 | * |
54 | * @param src Source address to copy from. |
54 | * @param src Source address to copy from. |
55 | * @param dst Destination address to copy to. |
55 | * @param dst Destination address to copy to. |
56 | * @param cnt Number of bytes to copy. |
56 | * @param cnt Number of bytes to copy. |
57 | * |
57 | * |
58 | * @return Destination address. |
58 | * @return Destination address. |
59 | */ |
59 | */ |
60 | void *_memcpy(void *dst, const void *src, size_t cnt) |
60 | void *_memcpy(void *dst, const void *src, size_t cnt) |
61 | { |
61 | { |
62 | unsigned int i, j; |
62 | unsigned int i, j; |
63 | 63 | ||
64 | if (ALIGN_UP((uintptr_t) src, sizeof(unative_t)) != (uintptr_t) src || |
64 | if (ALIGN_UP((uintptr_t) src, sizeof(unative_t)) != (uintptr_t) src || |
65 | ALIGN_UP((uintptr_t) dst, sizeof(unative_t)) != (uintptr_t) dst) { |
65 | ALIGN_UP((uintptr_t) dst, sizeof(unative_t)) != (uintptr_t) dst) { |
66 | for (i = 0; i < cnt; i++) |
66 | for (i = 0; i < cnt; i++) |
67 | ((uint8_t *) dst)[i] = ((uint8_t *) src)[i]; |
67 | ((uint8_t *) dst)[i] = ((uint8_t *) src)[i]; |
68 | } else { |
68 | } else { |
69 | for (i = 0; i < cnt / sizeof(unative_t); i++) |
69 | for (i = 0; i < cnt / sizeof(unative_t); i++) |
70 | ((unative_t *) dst)[i] = ((unative_t *) src)[i]; |
70 | ((unative_t *) dst)[i] = ((unative_t *) src)[i]; |
71 | 71 | ||
72 | for (j = 0; j < cnt % sizeof(unative_t); j++) |
72 | for (j = 0; j < cnt % sizeof(unative_t); j++) |
73 | ((uint8_t *)(((unative_t *) dst) + i))[j] = |
73 | ((uint8_t *)(((unative_t *) dst) + i))[j] = |
74 | ((uint8_t *)(((unative_t *) src) + i))[j]; |
74 | ((uint8_t *)(((unative_t *) src) + i))[j]; |
75 | } |
75 | } |
76 | 76 | ||
77 | return (char *) dst; |
77 | return (char *) dst; |
78 | } |
78 | } |
79 | 79 | ||
80 | /** Move memory block with possible overlapping. |
80 | /** Move memory block with possible overlapping. |
81 | * |
81 | * |
82 | * Copy cnt bytes from src address to dst address. The source and destination |
82 | * Copy cnt bytes from src address to dst address. The source and destination |
83 | * memory areas may overlap. |
83 | * memory areas may overlap. |
84 | * |
84 | * |
85 | * @param src Source address to copy from. |
85 | * @param src Source address to copy from. |
86 | * @param dst Destination address to copy to. |
86 | * @param dst Destination address to copy to. |
87 | * @param cnt Number of bytes to copy. |
87 | * @param cnt Number of bytes to copy. |
88 | * |
88 | * |
89 | * @return Destination address. |
89 | * @return Destination address. |
90 | */ |
90 | */ |
91 | void *memmove(void *dst, const void *src, size_t n) |
91 | void *memmove(void *dst, const void *src, size_t n) |
92 | { |
92 | { |
93 | const uint8_t *sp; |
93 | const uint8_t *sp; |
94 | uint8_t *dp; |
94 | uint8_t *dp; |
95 | 95 | ||
96 | /* Nothing to do? */ |
96 | /* Nothing to do? */ |
97 | if (src == dst) |
97 | if (src == dst) |
98 | return dst; |
98 | return dst; |
99 | 99 | ||
100 | /* Non-overlapping? */ |
100 | /* Non-overlapping? */ |
101 | if (dst >= src + n || src >= dst + n) { |
101 | if (dst >= src + n || src >= dst + n) { |
102 | return memcpy(dst, src, n); |
102 | return memcpy(dst, src, n); |
103 | } |
103 | } |
104 | 104 | ||
105 | /* Which direction? */ |
105 | /* Which direction? */ |
106 | if (src > dst) { |
106 | if (src > dst) { |
107 | /* Forwards. */ |
107 | /* Forwards. */ |
108 | sp = src; |
108 | sp = src; |
109 | dp = dst; |
109 | dp = dst; |
110 | 110 | ||
111 | while (n-- != 0) |
111 | while (n-- != 0) |
112 | *dp++ = *sp++; |
112 | *dp++ = *sp++; |
113 | } else { |
113 | } else { |
114 | /* Backwards. */ |
114 | /* Backwards. */ |
115 | sp = src + (n - 1); |
115 | sp = src + (n - 1); |
116 | dp = dst + (n - 1); |
116 | dp = dst + (n - 1); |
117 | 117 | ||
118 | while (n-- != 0) |
118 | while (n-- != 0) |
119 | *dp-- = *sp--; |
119 | *dp-- = *sp--; |
120 | } |
120 | } |
121 | 121 | ||
122 | return dst; |
122 | return dst; |
123 | } |
123 | } |
124 | 124 | ||
125 | /** Fill block of memory |
125 | /** Fill block of memory |
126 | * |
126 | * |
127 | * Fill cnt bytes at dst address with the value x. The filling is done |
127 | * Fill cnt bytes at dst address with the value x. The filling is done |
128 | * byte-by-byte. |
128 | * byte-by-byte. |
129 | * |
129 | * |
130 | * @param dst Destination address to fill. |
130 | * @param dst Destination address to fill. |
131 | * @param cnt Number of bytes to fill. |
131 | * @param cnt Number of bytes to fill. |
132 | * @param x Value to fill. |
132 | * @param x Value to fill. |
133 | * |
133 | * |
134 | */ |
134 | */ |
135 | void _memsetb(void *dst, size_t cnt, uint8_t x) |
135 | void _memsetb(void *dst, size_t cnt, uint8_t x) |
136 | { |
136 | { |
137 | unsigned int i; |
137 | unsigned int i; |
138 | uint8_t *p = (uint8_t *) dst; |
138 | uint8_t *p = (uint8_t *) dst; |
139 | 139 | ||
140 | for (i = 0; i < cnt; i++) |
140 | for (i = 0; i < cnt; i++) |
141 | p[i] = x; |
141 | p[i] = x; |
142 | } |
142 | } |
143 | 143 | ||
144 | /** Fill block of memory. |
144 | /** Fill block of memory. |
145 | * |
145 | * |
146 | * Fill cnt words at dst address with the value x. The filling is done |
146 | * Fill cnt words at dst address with the value x. The filling is done |
147 | * word-by-word. |
147 | * word-by-word. |
148 | * |
148 | * |
149 | * @param dst Destination address to fill. |
149 | * @param dst Destination address to fill. |
150 | * @param cnt Number of words to fill. |
150 | * @param cnt Number of words to fill. |
151 | * @param x Value to fill. |
151 | * @param x Value to fill. |
152 | * |
152 | * |
153 | */ |
153 | */ |
154 | void _memsetw(void *dst, size_t cnt, uint16_t x) |
154 | void _memsetw(void *dst, size_t cnt, uint16_t x) |
155 | { |
155 | { |
156 | unsigned int i; |
156 | unsigned int i; |
157 | uint16_t *p = (uint16_t *) dst; |
157 | uint16_t *p = (uint16_t *) dst; |
158 | 158 | ||
159 | for (i = 0; i < cnt; i++) |
159 | for (i = 0; i < cnt; i++) |
160 | p[i] = x; |
160 | p[i] = x; |
161 | } |
161 | } |
162 | 162 | ||
163 | /** Copy string. |
- | |
164 | * |
- | |
165 | * Copy string from src address to dst address. The copying is done |
- | |
166 | * char-by-char until the null character. The source and destination memory |
- | |
167 | * areas cannot overlap. |
- | |
168 | * |
- | |
169 | * @param src Source string to copy from. |
- | |
170 | * @param dst Destination string to copy to. |
- | |
171 | * |
- | |
172 | * @return Address of the destination string. |
- | |
173 | */ |
- | |
174 | char *strcpy(char *dest, const char *src) |
- | |
175 | { |
- | |
176 | char *orig = dest; |
- | |
177 | - | ||
178 | while ((*(dest++) = *(src++))) |
- | |
179 | ; |
- | |
180 | return orig; |
- | |
181 | } |
- | |
182 | - | ||
183 | /** @} |
163 | /** @} |
184 | */ |
164 | */ |
185 | 165 |