[a2da43c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <memstr.h>
|
---|
[de1712e] | 30 | #include <stddef.h>
|
---|
[10d65d70] | 31 | #include <stdint.h>
|
---|
[a2da43c] | 32 |
|
---|
[45f7449] | 33 | /** Move memory block without overlapping.
|
---|
| 34 | *
|
---|
| 35 | * Copy cnt bytes from src address to dst address. The source
|
---|
| 36 | * and destination memory areas cannot overlap.
|
---|
| 37 | *
|
---|
| 38 | * @param dst Destination address to copy to.
|
---|
| 39 | * @param src Source address to copy from.
|
---|
| 40 | * @param cnt Number of bytes to copy.
|
---|
| 41 | *
|
---|
| 42 | * @return Destination address.
|
---|
| 43 | *
|
---|
| 44 | */
|
---|
| 45 | void *memcpy(void *dst, const void *src, size_t cnt)
|
---|
| 46 | {
|
---|
| 47 | uint8_t *dp = (uint8_t *) dst;
|
---|
| 48 | const uint8_t *sp = (uint8_t *) src;
|
---|
[a35b458] | 49 |
|
---|
[45f7449] | 50 | while (cnt-- != 0)
|
---|
| 51 | *dp++ = *sp++;
|
---|
[a35b458] | 52 |
|
---|
[45f7449] | 53 | return dst;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /** Fill block of memory.
|
---|
| 57 | *
|
---|
| 58 | * Fill cnt bytes at dst address with the value val.
|
---|
| 59 | *
|
---|
| 60 | * @param dst Destination address to fill.
|
---|
| 61 | * @param val Value to fill.
|
---|
| 62 | * @param cnt Number of bytes to fill.
|
---|
| 63 | *
|
---|
| 64 | * @return Destination address.
|
---|
| 65 | *
|
---|
| 66 | */
|
---|
| 67 | void *memset(void *dst, int val, size_t cnt)
|
---|
| 68 | {
|
---|
| 69 | uint8_t *dp = (uint8_t *) dst;
|
---|
[a35b458] | 70 |
|
---|
[45f7449] | 71 | while (cnt-- != 0)
|
---|
| 72 | *dp++ = val;
|
---|
[a35b458] | 73 |
|
---|
[45f7449] | 74 | return dst;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[b31f735] | 77 | /** Move memory block with possible overlapping.
|
---|
| 78 | *
|
---|
| 79 | * Copy cnt bytes from src address to dst address. The source
|
---|
| 80 | * and destination memory areas may overlap.
|
---|
| 81 | *
|
---|
| 82 | * @param dst Destination address to copy to.
|
---|
| 83 | * @param src Source address to copy from.
|
---|
| 84 | * @param cnt Number of bytes to copy.
|
---|
| 85 | *
|
---|
| 86 | * @return Destination address.
|
---|
| 87 | *
|
---|
| 88 | */
|
---|
| 89 | void *memmove(void *dst, const void *src, size_t cnt)
|
---|
| 90 | {
|
---|
| 91 | /* Nothing to do? */
|
---|
| 92 | if (src == dst)
|
---|
| 93 | return dst;
|
---|
[a35b458] | 94 |
|
---|
[b31f735] | 95 | /* Non-overlapping? */
|
---|
| 96 | if ((dst >= src + cnt) || (src >= dst + cnt))
|
---|
| 97 | return memcpy(dst, src, cnt);
|
---|
[a35b458] | 98 |
|
---|
[b31f735] | 99 | uint8_t *dp;
|
---|
| 100 | const uint8_t *sp;
|
---|
[a35b458] | 101 |
|
---|
[b31f735] | 102 | /* Which direction? */
|
---|
| 103 | if (src > dst) {
|
---|
| 104 | /* Forwards. */
|
---|
| 105 | dp = dst;
|
---|
| 106 | sp = src;
|
---|
[a35b458] | 107 |
|
---|
[b31f735] | 108 | while (cnt-- != 0)
|
---|
| 109 | *dp++ = *sp++;
|
---|
| 110 | } else {
|
---|
| 111 | /* Backwards. */
|
---|
| 112 | dp = dst + (cnt - 1);
|
---|
| 113 | sp = src + (cnt - 1);
|
---|
[a35b458] | 114 |
|
---|
[b31f735] | 115 | while (cnt-- != 0)
|
---|
| 116 | *dp-- = *sp--;
|
---|
| 117 | }
|
---|
[a35b458] | 118 |
|
---|
[b31f735] | 119 | return dst;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[a2da43c] | 122 | /** @}
|
---|
| 123 | */
|
---|