Changes in kernel/generic/src/lib/memstr.c [cb4f078:96b02eb9] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/lib/memstr.c
rcb4f078 r96b02eb9 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 39 * of memory. These functions are optimized as much as generic functions of 40 * this type can be. 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. 41 42 */ 42 43 … … 45 46 #include <align.h> 46 47 47 /** Fillblock of memory.48 /** Copy block of memory. 48 49 * 49 * Fill cnt bytes at dst address with the value val. 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. 50 53 * 51 * @param dst Destination address to fill.52 * @param cnt Number of bytes to fill.53 * @param val Value to fill.54 * @param src Source address to copy from. 55 * @param dst Destination address to copy to. 56 * @param cnt Number of bytes to copy. 54 57 * 58 * @return Destination address. 55 59 */ 56 void memsetb(void *dst, size_t cnt, uint8_t val)60 void *_memcpy(void *dst, const void *src, size_t cnt) 57 61 { 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; 62 unsigned int i, j; 75 63 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); 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; 111 78 } 112 79 113 80 /** Move memory block with possible overlapping. 114 81 * 115 * Copy cnt bytes from src address to dst address. The source 116 * and destinationmemory areas may overlap.82 * Copy cnt bytes from src address to dst address. The source and destination 83 * memory areas may overlap. 117 84 * 118 * @param dst Destination address to copy to.119 * @param src Source address to copy from.120 * @param cnt 85 * @param src Source address to copy from. 86 * @param dst Destination address to copy to. 87 * @param cnt Number of bytes to copy. 121 88 * 122 * @return Destination address. 123 * 89 * @return Destination address. 124 90 */ 125 void *memmove(void *dst, const void *src, size_t cnt)91 void *memmove(void *dst, const void *src, size_t n) 126 92 { 93 const uint8_t *sp; 94 uint8_t *dp; 95 127 96 /* Nothing to do? */ 128 97 if (src == dst) 129 98 return dst; 130 99 131 100 /* Non-overlapping? */ 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 101 if (dst >= src + n || src >= dst + n) { 102 return memcpy(dst, src, n); 103 } 104 138 105 /* Which direction? */ 139 106 if (src > dst) { … … 141 108 sp = src; 142 109 dp = dst; 143 144 while ( cnt-- != 0)110 111 while (n-- != 0) 145 112 *dp++ = *sp++; 146 113 } else { 147 114 /* Backwards. */ 148 sp = src + ( cnt- 1);149 dp = dst + ( cnt- 1);150 151 while ( cnt-- != 0)115 sp = src + (n - 1); 116 dp = dst + (n - 1); 117 118 while (n-- != 0) 152 119 *dp-- = *sp--; 153 120 } 121 122 return dst; 123 } 124 125 /** Fill block of memory 126 * 127 * Fill cnt bytes at dst address with the value x. The filling is done 128 * 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; 154 139 155 return dst; 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 done 147 * 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; 156 161 } 157 162
Note:
See TracChangeset
for help on using the changeset viewer.