Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/memstr.c

    rcb4f078 r96b02eb9  
    2828 */
    2929
    30 /** @addtogroup generic
     30/** @addtogroup generic 
    3131 * @{
    3232 */
     
    3434/**
    3535 * @file
    36  * @brief Memory string operations.
     36 * @brief       Memory string operations.
    3737 *
    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.
    4142 */
    4243
     
    4546#include <align.h>
    4647
    47 /** Fill block of memory.
     48/** Copy block of memory.
    4849 *
    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.
    5053 *
    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.
    5457 *
     58 * @return              Destination address.
    5559 */
    56 void memsetb(void *dst, size_t cnt, uint8_t val)
     60void *_memcpy(void *dst, const void *src, size_t cnt)
    5761{
    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;
    7563       
    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;
    11178}
    11279
    11380/** Move memory block with possible overlapping.
    11481 *
    115  * Copy cnt bytes from src address to dst address. The source
    116  * and destination memory areas may overlap.
     82 * Copy cnt bytes from src address to dst address. The source and destination
     83 * memory areas may overlap.
    11784 *
    118  * @param dst Destination address to copy to.
    119  * @param src Source address to copy from.
    120  * @param cnt Number of bytes to copy.
     85 * @param src           Source address to copy from.
     86 * @param dst           Destination address to copy to.
     87 * @param cnt           Number of bytes to copy.
    12188 *
    122  * @return Destination address.
    123  *
     89 * @return              Destination address.
    12490 */
    125 void *memmove(void *dst, const void *src, size_t cnt)
     91void *memmove(void *dst, const void *src, size_t n)
    12692{
     93        const uint8_t *sp;
     94        uint8_t *dp;
     95
    12796        /* Nothing to do? */
    12897        if (src == dst)
    12998                return dst;
    130        
     99
    131100        /* 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
    138105        /* Which direction? */
    139106        if (src > dst) {
     
    141108                sp = src;
    142109                dp = dst;
    143                
    144                 while (cnt-- != 0)
     110
     111                while (n-- != 0)
    145112                        *dp++ = *sp++;
    146113        } else {
    147114                /* 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)
    152119                        *dp-- = *sp--;
    153120        }
     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 */
     135void _memsetb(void *dst, size_t cnt, uint8_t x)
     136{
     137        unsigned int i;
     138        uint8_t *p = (uint8_t *) dst;
    154139       
    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 */
     154void _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;       
    156161}
    157162
Note: See TracChangeset for help on using the changeset viewer.