Changeset cb4f078 in mainline for kernel/generic/src/lib/memstr.c
- Timestamp:
- 2011-03-28T20:53:55Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8caaea7
- Parents:
- 603c8740
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/lib/memstr.c
r603c8740 rcb4f078 28 28 */ 29 29 30 /** @addtogroup generic 30 /** @addtogroup generic 31 31 * @{ 32 32 */ … … 34 34 /** 35 35 * @file 36 * @brief 36 * @brief Memory string operations. 37 37 * 38 * This file provides architecture independent functions to manipulate blocks of 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 41 * versions of these functions. 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. 42 41 */ 43 42 … … 46 45 #include <align.h> 47 46 48 /** Copyblock of memory.47 /** Fill block of memory. 49 48 * 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 52 * cannot overlap. 49 * Fill cnt bytes at dst address with the value val. 53 50 * 54 * @param src Source address to copy from.55 * @param dst Destination address to copy to.56 * @param cnt Number of bytes to copy.51 * @param dst Destination address to fill. 52 * @param cnt Number of bytes to fill. 53 * @param val Value to fill. 57 54 * 58 * @return Destination address.59 55 */ 60 void *_memcpy(void *dst, const void *src, size_t cnt)56 void memsetb(void *dst, size_t cnt, uint8_t val) 61 57 { 62 unsigned int i, j; 58 __builtin_memset(dst, val, cnt); 59 } 60 61 /** Fill block of memory. 62 * 63 * Fill cnt words at dst address with the value val. The filling 64 * is done word-by-word. 65 * 66 * @param dst Destination address to fill. 67 * @param cnt Number of words to fill. 68 * @param val Value to fill. 69 * 70 */ 71 void memsetw(void *dst, size_t cnt, uint16_t val) 72 { 73 size_t i; 74 uint16_t *ptr = (uint16_t *) dst; 63 75 64 if (ALIGN_UP((uintptr_t) src, sizeof(sysarg_t)) != (uintptr_t) src || 65 ALIGN_UP((uintptr_t) dst, sizeof(sysarg_t)) != (uintptr_t) dst) { 66 for (i = 0; i < cnt; i++) 67 ((uint8_t *) dst)[i] = ((uint8_t *) src)[i]; 68 } else { 69 for (i = 0; i < cnt / sizeof(sysarg_t); i++) 70 ((sysarg_t *) dst)[i] = ((sysarg_t *) src)[i]; 71 72 for (j = 0; j < cnt % sizeof(sysarg_t); j++) 73 ((uint8_t *)(((sysarg_t *) dst) + i))[j] = 74 ((uint8_t *)(((sysarg_t *) src) + i))[j]; 75 } 76 77 return (char *) dst; 76 for (i = 0; i < cnt; i++) 77 ptr[i] = val; 78 } 79 80 /** Fill block of memory. 81 * 82 * Fill cnt bytes at dst address with the value val. 83 * 84 * @param dst Destination address to fill. 85 * @param val Value to fill. 86 * @param cnt Number of bytes to fill. 87 * 88 * @return Destination address. 89 * 90 */ 91 void *memset(void *dst, int val, size_t cnt) 92 { 93 return __builtin_memset(dst, val, cnt); 94 } 95 96 /** Move memory block without overlapping. 97 * 98 * Copy cnt bytes from src address to dst address. The source 99 * and destination memory areas cannot overlap. 100 * 101 * @param dst Destination address to copy to. 102 * @param src Source address to copy from. 103 * @param cnt Number of bytes to copy. 104 * 105 * @return Destination address. 106 * 107 */ 108 void *memcpy(void *dst, const void *src, size_t cnt) 109 { 110 return __builtin_memcpy(dst, src, cnt); 78 111 } 79 112 80 113 /** Move memory block with possible overlapping. 81 114 * 82 * Copy cnt bytes from src address to dst address. The source and destination83 * memory areas may overlap.115 * Copy cnt bytes from src address to dst address. The source 116 * and destination memory areas may overlap. 84 117 * 85 * @param src Source address to copy from.86 * @param dst Destination address to copy to.87 * @param cnt 118 * @param dst Destination address to copy to. 119 * @param src Source address to copy from. 120 * @param cnt Number of bytes to copy. 88 121 * 89 * @return Destination address. 122 * @return Destination address. 123 * 90 124 */ 91 void *memmove(void *dst, const void *src, size_t n)125 void *memmove(void *dst, const void *src, size_t cnt) 92 126 { 93 const uint8_t *sp;94 uint8_t *dp;95 96 127 /* Nothing to do? */ 97 128 if (src == dst) 98 129 return dst; 99 130 100 131 /* Non-overlapping? */ 101 if (dst >= src + n || src >= dst + n) { 102 return memcpy(dst, src, n); 103 } 104 132 if ((dst >= src + cnt) || (src >= dst + cnt)) 133 return memcpy(dst, src, cnt); 134 135 const uint8_t *sp; 136 uint8_t *dp; 137 105 138 /* Which direction? */ 106 139 if (src > dst) { … … 108 141 sp = src; 109 142 dp = dst; 110 111 while ( n-- != 0)143 144 while (cnt-- != 0) 112 145 *dp++ = *sp++; 113 146 } else { 114 147 /* Backwards. */ 115 sp = src + ( n- 1);116 dp = dst + ( n- 1);117 118 while ( n-- != 0)148 sp = src + (cnt - 1); 149 dp = dst + (cnt - 1); 150 151 while (cnt-- != 0) 119 152 *dp-- = *sp--; 120 153 } 121 154 122 155 return dst; 123 }124 125 /** Fill block of memory126 *127 * Fill cnt bytes at dst address with the value x. The filling is done128 * byte-by-byte.129 *130 * @param dst Destination address to fill.131 * @param cnt Number of bytes to fill.132 * @param x Value to fill.133 *134 */135 void _memsetb(void *dst, size_t cnt, uint8_t x)136 {137 unsigned int i;138 uint8_t *p = (uint8_t *) dst;139 140 for (i = 0; i < cnt; i++)141 p[i] = x;142 }143 144 /** Fill block of memory.145 *146 * Fill cnt words at dst address with the value x. The filling is done147 * word-by-word.148 *149 * @param dst Destination address to fill.150 * @param cnt Number of words to fill.151 * @param x Value to fill.152 *153 */154 void _memsetw(void *dst, size_t cnt, uint16_t x)155 {156 unsigned int i;157 uint16_t *p = (uint16_t *) dst;158 159 for (i = 0; i < cnt; i++)160 p[i] = x;161 156 } 162 157
Note:
See TracChangeset
for help on using the changeset viewer.