[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup generic
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf26ba9] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[cf26ba9] | 35 | * @brief Memory string operations.
|
---|
| 36 | *
|
---|
[9f3363e] | 37 | * This file provides architecture independent functions to manipulate blocks of
|
---|
| 38 | * memory. These functions are optimized as much as generic functions of this
|
---|
| 39 | * type can be. However, architectures are free to provide even more optimized
|
---|
| 40 | * versions of these functions.
|
---|
[cf26ba9] | 41 | */
|
---|
| 42 |
|
---|
[f761f1eb] | 43 | #include <memstr.h>
|
---|
| 44 | #include <arch/types.h>
|
---|
[2d7a5fe] | 45 | #include <align.h>
|
---|
[f761f1eb] | 46 |
|
---|
[9f3363e] | 47 | /** Copy block of memory.
|
---|
[d34657e] | 48 | *
|
---|
[9f3363e] | 49 | * Copy cnt bytes from src address to dst address. The copying is done
|
---|
| 50 | * word-by-word and then byte-by-byte. The source and destination memory areas
|
---|
| 51 | * cannot overlap.
|
---|
[d34657e] | 52 | *
|
---|
[9f3363e] | 53 | * @param src Source address to copy from.
|
---|
| 54 | * @param dst Destination address to copy to.
|
---|
| 55 | * @param cnt Number of bytes to copy.
|
---|
[d34657e] | 56 | *
|
---|
[9f3363e] | 57 | * @return Destination address.
|
---|
[d34657e] | 58 | */
|
---|
[9f3363e] | 59 | void *_memcpy(void *dst, const void *src, size_t cnt)
|
---|
[f761f1eb] | 60 | {
|
---|
[6c441cf8] | 61 | unsigned int i, j;
|
---|
[f761f1eb] | 62 |
|
---|
[7f1c620] | 63 | if (ALIGN_UP((uintptr_t) src, sizeof(unative_t)) != (uintptr_t) src ||
|
---|
[9f3363e] | 64 | ALIGN_UP((uintptr_t) dst, sizeof(unative_t)) != (uintptr_t) dst) {
|
---|
[2d7a5fe] | 65 | for (i = 0; i < cnt; i++)
|
---|
[7f1c620] | 66 | ((uint8_t *) dst)[i] = ((uint8_t *) src)[i];
|
---|
[2d7a5fe] | 67 | } else {
|
---|
[0f269c2] | 68 | for (i = 0; i < cnt / sizeof(unative_t); i++)
|
---|
[7f1c620] | 69 | ((unative_t *) dst)[i] = ((unative_t *) src)[i];
|
---|
[7a255e69] | 70 |
|
---|
[0f269c2] | 71 | for (j = 0; j < cnt % sizeof(unative_t); j++)
|
---|
[9f3363e] | 72 | ((uint8_t *)(((unative_t *) dst) + i))[j] =
|
---|
| 73 | ((uint8_t *)(((unative_t *) src) + i))[j];
|
---|
[2d7a5fe] | 74 | }
|
---|
[9c0a9b3] | 75 |
|
---|
[da349da0] | 76 | return (char *) dst;
|
---|
[f761f1eb] | 77 | }
|
---|
| 78 |
|
---|
[d34657e] | 79 | /** Fill block of memory
|
---|
| 80 | *
|
---|
[9f3363e] | 81 | * Fill cnt bytes at dst address with the value x. The filling is done
|
---|
| 82 | * byte-by-byte.
|
---|
[d34657e] | 83 | *
|
---|
[9f3363e] | 84 | * @param dst Destination address to fill.
|
---|
| 85 | * @param cnt Number of bytes to fill.
|
---|
| 86 | * @param x Value to fill.
|
---|
[d34657e] | 87 | *
|
---|
| 88 | */
|
---|
[e32e092] | 89 | void _memsetb(void *dst, size_t cnt, uint8_t x)
|
---|
[f761f1eb] | 90 | {
|
---|
[6c441cf8] | 91 | unsigned int i;
|
---|
[7f1c620] | 92 | uint8_t *p = (uint8_t *) dst;
|
---|
[f761f1eb] | 93 |
|
---|
[bd1deed] | 94 | for (i = 0; i < cnt; i++)
|
---|
[f761f1eb] | 95 | p[i] = x;
|
---|
| 96 | }
|
---|
[379d73f3] | 97 |
|
---|
[9f3363e] | 98 | /** Fill block of memory.
|
---|
[379d73f3] | 99 | *
|
---|
[9f3363e] | 100 | * Fill cnt words at dst address with the value x. The filling is done
|
---|
| 101 | * word-by-word.
|
---|
[379d73f3] | 102 | *
|
---|
[9f3363e] | 103 | * @param dst Destination address to fill.
|
---|
| 104 | * @param cnt Number of words to fill.
|
---|
| 105 | * @param x Value to fill.
|
---|
[379d73f3] | 106 | *
|
---|
| 107 | */
|
---|
[e32e092] | 108 | void _memsetw(void *dst, size_t cnt, uint16_t x)
|
---|
[379d73f3] | 109 | {
|
---|
[6c441cf8] | 110 | unsigned int i;
|
---|
[7f1c620] | 111 | uint16_t *p = (uint16_t *) dst;
|
---|
[379d73f3] | 112 |
|
---|
[bd1deed] | 113 | for (i = 0; i < cnt; i++)
|
---|
[379d73f3] | 114 | p[i] = x;
|
---|
| 115 | }
|
---|
[b45c443] | 116 |
|
---|
[9f3363e] | 117 | /** Copy string.
|
---|
[bd1deed] | 118 | *
|
---|
[9f3363e] | 119 | * Copy string from src address to dst address. The copying is done
|
---|
| 120 | * char-by-char until the null character. The source and destination memory
|
---|
| 121 | * areas cannot overlap.
|
---|
[bd1deed] | 122 | *
|
---|
[9f3363e] | 123 | * @param src Source string to copy from.
|
---|
| 124 | * @param dst Destination string to copy to.
|
---|
[bd1deed] | 125 | *
|
---|
[9f3363e] | 126 | * @return Address of the destination string.
|
---|
[bd1deed] | 127 | */
|
---|
| 128 | char *strcpy(char *dest, const char *src)
|
---|
| 129 | {
|
---|
| 130 | char *orig = dest;
|
---|
| 131 |
|
---|
[cc85fb9] | 132 | while ((*(dest++) = *(src++)))
|
---|
| 133 | ;
|
---|
[bd1deed] | 134 | return orig;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[cc73a8a1] | 137 | /** @}
|
---|
[b45c443] | 138 | */
|
---|