Changeset cb4f078 in mainline for kernel/generic/src/lib/memstr.c


Ignore:
Timestamp:
2011-03-28T20:53:55Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8caaea7
Parents:
603c8740
Message:

unify kernel byte string implementations

File:
1 edited

Legend:

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

    r603c8740 rcb4f078  
    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 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.
    4241 */
    4342
     
    4645#include <align.h>
    4746
    48 /** Copy block of memory.
     47/** Fill block of memory.
    4948 *
    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.
    5350 *
    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.
    5754 *
    58  * @return              Destination address.
    5955 */
    60 void *_memcpy(void *dst, const void *src, size_t cnt)
     56void memsetb(void *dst, size_t cnt, uint8_t val)
    6157{
    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 */
     71void memsetw(void *dst, size_t cnt, uint16_t val)
     72{
     73        size_t i;
     74        uint16_t *ptr = (uint16_t *) dst;
    6375       
    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 */
     91void *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 */
     108void *memcpy(void *dst, const void *src, size_t cnt)
     109{
     110        return __builtin_memcpy(dst, src, cnt);
    78111}
    79112
    80113/** Move memory block with possible overlapping.
    81114 *
    82  * Copy cnt bytes from src address to dst address. The source and destination
    83  * memory areas may overlap.
     115 * Copy cnt bytes from src address to dst address. The source
     116 * and destination memory areas may overlap.
    84117 *
    85  * @param src           Source address to copy from.
    86  * @param dst           Destination address to copy to.
    87  * @param cnt           Number of bytes to copy.
     118 * @param dst Destination address to copy to.
     119 * @param src Source address to copy from.
     120 * @param cnt Number of bytes to copy.
    88121 *
    89  * @return              Destination address.
     122 * @return Destination address.
     123 *
    90124 */
    91 void *memmove(void *dst, const void *src, size_t n)
     125void *memmove(void *dst, const void *src, size_t cnt)
    92126{
    93         const uint8_t *sp;
    94         uint8_t *dp;
    95 
    96127        /* Nothing to do? */
    97128        if (src == dst)
    98129                return dst;
    99 
     130       
    100131        /* 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       
    105138        /* Which direction? */
    106139        if (src > dst) {
     
    108141                sp = src;
    109142                dp = dst;
    110 
    111                 while (n-- != 0)
     143               
     144                while (cnt-- != 0)
    112145                        *dp++ = *sp++;
    113146        } else {
    114147                /* 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)
    119152                        *dp-- = *sp--;
    120153        }
    121 
     154       
    122155        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;
    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 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;       
    161156}
    162157
Note: See TracChangeset for help on using the changeset viewer.