Changeset 8a5a902 in mainline for uspace/lib/c/generic/mem.c
- Timestamp:
- 2013-03-29T16:26:39Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0ca441c
- Parents:
- 2d1195c0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/mem.c
r2d1195c0 r8a5a902 37 37 #include <stdlib.h> 38 38 #include <sys/types.h> 39 40 /** Fill memory block with a constant value. */41 void *memset(void *dest, int b, size_t n)42 {43 char *pb;44 unsigned long *pw;45 size_t word_size;46 size_t n_words;47 48 unsigned long pattern;49 size_t i;50 size_t fill;51 52 /* Fill initial segment. */53 word_size = sizeof(unsigned long);54 fill = word_size - ((uintptr_t) dest & (word_size - 1));55 if (fill > n) fill = n;56 57 pb = dest;58 59 i = fill;60 while (i-- != 0)61 *pb++ = b;62 63 /* Compute remaining size. */64 n -= fill;65 if (n == 0) return dest;66 67 n_words = n / word_size;68 n = n % word_size;69 pw = (unsigned long *) pb;70 71 /* Create word-sized pattern for aligned segment. */72 pattern = 0;73 i = word_size;74 while (i-- != 0)75 pattern = (pattern << 8) | (uint8_t) b;76 77 /* Fill aligned segment. */78 i = n_words;79 while (i-- != 0)80 *pw++ = pattern;81 82 pb = (char *) pw;83 84 /* Fill final segment. */85 i = n;86 while (i-- != 0)87 *pb++ = b;88 89 return dest;90 }91 92 struct along {93 unsigned long n;94 } __attribute__ ((packed));95 96 static void *unaligned_memcpy(void *dst, const void *src, size_t n)97 {98 size_t i, j;99 struct along *adst = dst;100 const struct along *asrc = src;101 102 for (i = 0; i < n / sizeof(unsigned long); i++)103 adst[i].n = asrc[i].n;104 105 for (j = 0; j < n % sizeof(unsigned long); j++)106 ((unsigned char *) (((unsigned long *) dst) + i))[j] =107 ((unsigned char *) (((unsigned long *) src) + i))[j];108 109 return (char *) dst;110 }111 112 /** Copy memory block. */113 void *memcpy(void *dst, const void *src, size_t n)114 {115 size_t i;116 size_t mod, fill;117 size_t word_size;118 size_t n_words;119 120 const unsigned long *srcw;121 unsigned long *dstw;122 const uint8_t *srcb;123 uint8_t *dstb;124 125 word_size = sizeof(unsigned long);126 127 /*128 * Are source and destination addresses congruent modulo word_size?129 * If not, use unaligned_memcpy().130 */131 132 if (((uintptr_t) dst & (word_size - 1)) !=133 ((uintptr_t) src & (word_size - 1)))134 return unaligned_memcpy(dst, src, n);135 136 /*137 * mod is the address modulo word size. fill is the length of the138 * initial buffer segment before the first word boundary.139 * If the buffer is very short, use unaligned_memcpy(), too.140 */141 142 mod = (uintptr_t) dst & (word_size - 1);143 fill = word_size - mod;144 if (fill > n) fill = n;145 146 /* Copy the initial segment. */147 148 srcb = src;149 dstb = dst;150 151 i = fill;152 while (i-- != 0)153 *dstb++ = *srcb++;154 155 /* Compute remaining length. */156 157 n -= fill;158 if (n == 0) return dst;159 160 /* Pointers to aligned segment. */161 162 dstw = (unsigned long *) dstb;163 srcw = (const unsigned long *) srcb;164 165 n_words = n / word_size; /* Number of whole words to copy. */166 n -= n_words * word_size; /* Remaining bytes at the end. */167 168 /* "Fast" copy. */169 i = n_words;170 while (i-- != 0)171 *dstw++ = *srcw++;172 173 /*174 * Copy the rest.175 */176 177 srcb = (const uint8_t *) srcw;178 dstb = (uint8_t *) dstw;179 180 i = n;181 while (i-- != 0)182 *dstb++ = *srcb++;183 184 return dst;185 }186 39 187 40 /** Move memory block with possible overlapping. */
Note:
See TracChangeset
for help on using the changeset viewer.