Rev 3022 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3022 | Rev 4055 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
- | 3 | * Copyright (c) 2008 Jiri Svoboda |
|
3 | * All rights reserved. |
4 | * All rights reserved. |
4 | * |
5 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * are met: |
Line 32... | Line 33... | ||
32 | 33 | ||
33 | /** |
34 | /** |
34 | * @file |
35 | * @file |
35 | * @brief Memory string operations. |
36 | * @brief Memory string operations. |
36 | * |
37 | * |
37 | * This file provides architecture independent functions |
38 | * This file provides architecture independent functions to manipulate blocks of |
38 | * to manipulate blocks of memory. These functions |
- | |
39 | * are optimized as much as generic functions of |
39 | * memory. These functions are optimized as much as generic functions of this |
40 | * this type can be. However, architectures are |
40 | * type can be. However, architectures are free to provide even more optimized |
41 | * free to provide even more optimized versions of these |
- | |
42 | * functions. |
41 | * versions of these functions. |
43 | */ |
42 | */ |
44 | 43 | ||
45 | #include <memstr.h> |
44 | #include <memstr.h> |
46 | #include <arch/types.h> |
45 | #include <arch/types.h> |
47 | #include <align.h> |
46 | #include <align.h> |
48 | 47 | ||
49 | /** Copy block of memory |
48 | /** Copy block of memory. |
50 | * |
49 | * |
51 | * Copy cnt bytes from src address to dst address. |
50 | * Copy cnt bytes from src address to dst address. The copying is done |
52 | * The copying is done word-by-word and then byte-by-byte. |
51 | * word-by-word and then byte-by-byte. The source and destination memory areas |
53 | * The source and destination memory areas cannot overlap. |
52 | * cannot overlap. |
54 | * |
53 | * |
55 | * @param src Origin address to copy from. |
54 | * @param src Source address to copy from. |
56 | * @param dst Origin address to copy to. |
55 | * @param dst Destination address to copy to. |
57 | * @param cnt Number of bytes to copy. |
56 | * @param cnt Number of bytes to copy. |
58 | * |
57 | * |
- | 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 | - | ||
70 | for (i = 0; i < cnt/sizeof(unative_t); i++) |
69 | for (i = 0; i < cnt / sizeof(unative_t); i++) |
71 | ((unative_t *) dst)[i] = ((unative_t *) src)[i]; |
70 | ((unative_t *) dst)[i] = ((unative_t *) src)[i]; |
72 | 71 | ||
73 | 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] = |
|
74 | ((uint8_t *)(((unative_t *) dst) + i))[j] = ((uint8_t *)(((unative_t *) src) + i))[j]; |
74 | ((uint8_t *)(((unative_t *) src) + i))[j]; |
75 | } |
75 | } |
76 | 76 | ||
77 | return (char *) src; |
77 | return (char *) dst; |
78 | } |
78 | } |
79 | 79 | ||
80 | /** Fill block of memory |
80 | /** Move memory block with possible overlapping. |
81 | * |
81 | * |
82 | * Fill cnt bytes at dst address with the value x. |
82 | * Copy cnt bytes from src address to dst address. The source and destination |
83 | * The filling is done byte-by-byte. |
83 | * memory areas may overlap. |
84 | * |
84 | * |
85 | * @param dst Origin address to fill. |
85 | * @param src Source address to copy from. |
86 | * @param cnt Number of bytes to fill. |
86 | * @param dst Destination address to copy to. |
87 | * @param x Value to fill. |
87 | * @param cnt Number of bytes to copy. |
88 | * |
88 | * |
- | 89 | * @return Destination address. |
|
89 | */ |
90 | */ |
90 | void _memsetb(uintptr_t dst, size_t cnt, uint8_t x) |
91 | void *memmove(void *dst, const void *src, size_t n) |
91 | { |
92 | { |
- | 93 | const uint8_t *sp; |
|
92 | unsigned int i; |
94 | uint8_t *dp; |
- | 95 | ||
- | 96 | /* Nothing to do? */ |
|
- | 97 | if (src == dst) |
|
- | 98 | return dst; |
|
- | 99 | ||
- | 100 | /* Non-overlapping? */ |
|
- | 101 | if (dst >= src + n || src >= dst + n) { |
|
93 | uint8_t *p = (uint8_t *) dst; |
102 | return memcpy(dst, src, n); |
94 | 103 | } |
|
- | 104 | ||
95 | for (i = 0; i < cnt; i++) |
105 | /* Which direction? */ |
- | 106 | if (src > dst) { |
|
- | 107 | /* Forwards. */ |
|
- | 108 | sp = src; |
|
96 | p[i] = x; |
109 | dp = dst; |
- | 110 | ||
- | 111 | while (n-- != 0) |
|
- | 112 | *dp++ = *sp++; |
|
- | 113 | } else { |
|
- | 114 | /* Backwards. */ |
|
- | 115 | sp = src + (n - 1); |
|
- | 116 | dp = dst + (n - 1); |
|
- | 117 | ||
- | 118 | while (n-- != 0) |
|
- | 119 | *dp-- = *sp--; |
|
- | 120 | } |
|
- | 121 | ||
- | 122 | return dst; |
|
97 | } |
123 | } |
98 | 124 | ||
99 | /** Fill block of memory |
125 | /** Fill block of memory |
100 | * |
126 | * |
101 | * Fill cnt words at dst address with the value x. |
127 | * Fill cnt bytes at dst address with the value x. The filling is done |
102 | * The filling is done word-by-word. |
128 | * byte-by-byte. |
103 | * |
129 | * |
104 | * @param dst Origin address to fill. |
130 | * @param dst Destination address to fill. |
105 | * @param cnt Number of words to fill. |
131 | * @param cnt Number of bytes to fill. |
106 | * @param x Value to fill. |
132 | * @param x Value to fill. |
107 | * |
133 | * |
108 | */ |
134 | */ |
109 | void _memsetw(uintptr_t dst, size_t cnt, uint16_t x) |
135 | void _memsetb(void *dst, size_t cnt, uint8_t x) |
110 | { |
136 | { |
111 | unsigned int i; |
137 | unsigned int i; |
112 | uint16_t *p = (uint16_t *) dst; |
138 | uint8_t *p = (uint8_t *) dst; |
113 | 139 | ||
114 | for (i = 0; i < cnt; i++) |
140 | for (i = 0; i < cnt; i++) |
115 | p[i] = x; |
141 | p[i] = x; |
116 | } |
142 | } |
117 | 143 | ||
118 | /** Copy string |
144 | /** Fill block of memory. |
119 | * |
145 | * |
120 | * Copy string from src address to dst address. |
146 | * Fill cnt words at dst address with the value x. The filling is done |
121 | * The copying is done char-by-char until the null |
- | |
122 | * character. The source and destination memory areas |
- | |
123 | * cannot overlap. |
147 | * word-by-word. |
124 | * |
148 | * |
125 | * @param src Origin string to copy from. |
149 | * @param dst Destination address to fill. |
126 | * @param dst Origin string to copy to. |
150 | * @param cnt Number of words to fill. |
- | 151 | * @param x Value to fill. |
|
127 | * |
152 | * |
128 | */ |
153 | */ |
129 | char *strcpy(char *dest, const char *src) |
154 | void _memsetw(void *dst, size_t cnt, uint16_t x) |
130 | { |
155 | { |
131 | char *orig = dest; |
156 | unsigned int i; |
- | 157 | uint16_t *p = (uint16_t *) dst; |
|
132 | 158 | ||
133 | while ((*(dest++) = *(src++))) |
159 | for (i = 0; i < cnt; i++) |
134 | ; |
- | |
135 | return orig; |
160 | p[i] = x; |
136 | } |
161 | } |
137 | 162 | ||
138 | /** @} |
163 | /** @} |
139 | */ |
164 | */ |