~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to boot/generic/src/memstr.c

  • Committer: Jakub Jermar
  • Date: 2012-03-29 21:41:07 UTC
  • Revision ID: jakub@jermar.eu-20120329214107-3sgmwls8rr10y1q2
Add memmove() to boot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
        return dst;
51
51
}
52
52
 
 
53
/** Move memory block with possible overlapping.
 
54
 *
 
55
 * Copy cnt bytes from src address to dst address. The source
 
56
 * and destination memory areas may overlap.
 
57
 *
 
58
 * @param dst Destination address to copy to.
 
59
 * @param src Source address to copy from.
 
60
 * @param cnt Number of bytes to copy.
 
61
 *
 
62
 * @return Destination address.
 
63
 *
 
64
 */
 
65
void *memmove(void *dst, const void *src, size_t cnt)
 
66
{
 
67
        /* Nothing to do? */
 
68
        if (src == dst)
 
69
                return dst;
 
70
        
 
71
        /* Non-overlapping? */
 
72
        if ((dst >= src + cnt) || (src >= dst + cnt))
 
73
                return memcpy(dst, src, cnt);
 
74
        
 
75
        uint8_t *dp;
 
76
        const uint8_t *sp;
 
77
        
 
78
        /* Which direction? */
 
79
        if (src > dst) {
 
80
                /* Forwards. */
 
81
                dp = dst;
 
82
                sp = src;
 
83
                
 
84
                while (cnt-- != 0)
 
85
                        *dp++ = *sp++;
 
86
        } else {
 
87
                /* Backwards. */
 
88
                dp = dst + (cnt - 1);
 
89
                sp = src + (cnt - 1);
 
90
                
 
91
                while (cnt-- != 0)
 
92
                        *dp-- = *sp--;
 
93
        }
 
94
        
 
95
        return dst;
 
96
}
 
97
 
53
98
/** @}
54
99
 */