[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 |
|
---|
[f761f1eb] | 43 | #include <memstr.h>
|
---|
[d99c1d2] | 44 | #include <typedefs.h>
|
---|
[2d7a5fe] | 45 | #include <align.h>
|
---|
[f761f1eb] | 46 |
|
---|
[cb4f078] | 47 | /** Fill block of memory.
|
---|
| 48 | *
|
---|
| 49 | * Fill cnt bytes at dst address with the value val.
|
---|
| 50 | *
|
---|
| 51 | * @param dst Destination address to fill.
|
---|
| 52 | * @param cnt Number of bytes to fill.
|
---|
| 53 | * @param val Value to fill.
|
---|
| 54 | *
|
---|
| 55 | */
|
---|
| 56 | void memsetb(void *dst, size_t cnt, uint8_t val)
|
---|
| 57 | {
|
---|
| 58 | __builtin_memset(dst, val, cnt);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /** Fill block of memory.
|
---|
[d34657e] | 62 | *
|
---|
[cb4f078] | 63 | * Fill cnt words at dst address with the value val. The filling
|
---|
| 64 | * is done word-by-word.
|
---|
[d34657e] | 65 | *
|
---|
[cb4f078] | 66 | * @param dst Destination address to fill.
|
---|
| 67 | * @param cnt Number of words to fill.
|
---|
| 68 | * @param val Value to fill.
|
---|
[d34657e] | 69 | *
|
---|
| 70 | */
|
---|
[cb4f078] | 71 | void memsetw(void *dst, size_t cnt, uint16_t val)
|
---|
[f761f1eb] | 72 | {
|
---|
[cb4f078] | 73 | size_t i;
|
---|
| 74 | uint16_t *ptr = (uint16_t *) dst;
|
---|
[f761f1eb] | 75 |
|
---|
[cb4f078] | 76 | for (i = 0; i < cnt; i++)
|
---|
| 77 | ptr[i] = val;
|
---|
[f761f1eb] | 78 | }
|
---|
| 79 |
|
---|
[cb4f078] | 80 | /** Fill block of memory.
|
---|
[ac48fef] | 81 | *
|
---|
[cb4f078] | 82 | * Fill cnt bytes at dst address with the value val.
|
---|
[ac48fef] | 83 | *
|
---|
[cb4f078] | 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.
|
---|
[ac48fef] | 89 | *
|
---|
| 90 | */
|
---|
[cb4f078] | 91 | void *memset(void *dst, int val, size_t cnt)
|
---|
[ac48fef] | 92 | {
|
---|
[cb4f078] | 93 | return __builtin_memset(dst, val, cnt);
|
---|
| 94 | }
|
---|
[ac48fef] | 95 |
|
---|
[cb4f078] | 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 | */
|
---|
| 108 | void *memcpy(void *dst, const void *src, size_t cnt)
|
---|
| 109 | {
|
---|
| 110 | return __builtin_memcpy(dst, src, cnt);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /** Move memory block with possible overlapping.
|
---|
| 114 | *
|
---|
| 115 | * Copy cnt bytes from src address to dst address. The source
|
---|
| 116 | * and destination memory areas may overlap.
|
---|
| 117 | *
|
---|
| 118 | * @param dst Destination address to copy to.
|
---|
| 119 | * @param src Source address to copy from.
|
---|
| 120 | * @param cnt Number of bytes to copy.
|
---|
| 121 | *
|
---|
| 122 | * @return Destination address.
|
---|
| 123 | *
|
---|
| 124 | */
|
---|
| 125 | void *memmove(void *dst, const void *src, size_t cnt)
|
---|
| 126 | {
|
---|
[ac48fef] | 127 | /* Nothing to do? */
|
---|
| 128 | if (src == dst)
|
---|
| 129 | return dst;
|
---|
[cb4f078] | 130 |
|
---|
[ac48fef] | 131 | /* Non-overlapping? */
|
---|
[cb4f078] | 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 |
|
---|
[ac48fef] | 138 | /* Which direction? */
|
---|
| 139 | if (src > dst) {
|
---|
| 140 | /* Forwards. */
|
---|
| 141 | sp = src;
|
---|
| 142 | dp = dst;
|
---|
[cb4f078] | 143 |
|
---|
| 144 | while (cnt-- != 0)
|
---|
[ac48fef] | 145 | *dp++ = *sp++;
|
---|
| 146 | } else {
|
---|
| 147 | /* Backwards. */
|
---|
[cb4f078] | 148 | sp = src + (cnt - 1);
|
---|
| 149 | dp = dst + (cnt - 1);
|
---|
| 150 |
|
---|
| 151 | while (cnt-- != 0)
|
---|
[ac48fef] | 152 | *dp-- = *sp--;
|
---|
| 153 | }
|
---|
[379d73f3] | 154 |
|
---|
[cb4f078] | 155 | return dst;
|
---|
[379d73f3] | 156 | }
|
---|
[b45c443] | 157 |
|
---|
[cc73a8a1] | 158 | /** @}
|
---|
[b45c443] | 159 | */
|
---|