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