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


Ignore:
Timestamp:
2008-12-27T11:28:06Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9a63657
Parents:
b755225
Message:

Add memmove() function to kernel.

File:
1 edited

Legend:

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

    rb755225 rac48fef  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2008 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    7778}
    7879
     80/** Move memory block with possible overlapping.
     81 *
     82 * Copy cnt bytes from src address to dst address. The source and destination
     83 * memory areas may overlap.
     84 *
     85 * @param src           Source address to copy from.
     86 * @param dst           Destination address to copy to.
     87 * @param cnt           Number of bytes to copy.
     88 *
     89 * @return              Destination address.
     90 */
     91void *memmove(void *dst, const void *src, size_t n)
     92{
     93        const uint8_t *sp;
     94        uint8_t *dp;
     95
     96        /* Nothing to do? */
     97        if (src == dst)
     98                return dst;
     99
     100        /* Non-overlapping? */
     101        if (dst >= src + n || src >= dst + n) {
     102                return memcpy(dst, src, n);
     103        }
     104
     105        /* Which direction? */
     106        if (src > dst) {
     107                /* Forwards. */
     108                sp = src;
     109                dp = dst;
     110
     111                while (n-- != 0)
     112                        *dp++ = *sp++;
     113        } else {
     114                /* Backwards. */
     115                sp = src + (n - 1);
     116                dp = dst + (n - 1);
     117
     118                while (n-- != 0)
     119                        *dp-- = *sp--;
     120        }
     121
     122        return dst;
     123}
     124
    79125/** Fill block of memory
    80126 *
Note: See TracChangeset for help on using the changeset viewer.