source: mainline/kernel/generic/src/lib/memstr.c@ bf1fb9f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf1fb9f was 16da5f8e, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

String functions should be declared in string.h (and implemented in string.c) in the kernel.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[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
[cc73a8a1]30/** @addtogroup generic
[b45c443]31 * @{
32 */
33
[cf26ba9]34/**
[b45c443]35 * @file
[cf26ba9]36 * @brief Memory string operations.
37 *
[9f3363e]38 * This file provides architecture independent functions to manipulate blocks of
39 * memory. These functions are optimized as much as generic functions of this
40 * type can be. However, architectures are free to provide even more optimized
41 * versions of these functions.
[cf26ba9]42 */
43
[f761f1eb]44#include <memstr.h>
45#include <arch/types.h>
[2d7a5fe]46#include <align.h>
[f761f1eb]47
[9f3363e]48/** Copy block of memory.
[d34657e]49 *
[9f3363e]50 * Copy cnt bytes from src address to dst address. The copying is done
51 * word-by-word and then byte-by-byte. The source and destination memory areas
52 * cannot overlap.
[d34657e]53 *
[9f3363e]54 * @param src Source address to copy from.
55 * @param dst Destination address to copy to.
56 * @param cnt Number of bytes to copy.
[d34657e]57 *
[9f3363e]58 * @return Destination address.
[d34657e]59 */
[9f3363e]60void *_memcpy(void *dst, const void *src, size_t cnt)
[f761f1eb]61{
[6c441cf8]62 unsigned int i, j;
[f761f1eb]63
[7f1c620]64 if (ALIGN_UP((uintptr_t) src, sizeof(unative_t)) != (uintptr_t) src ||
[9f3363e]65 ALIGN_UP((uintptr_t) dst, sizeof(unative_t)) != (uintptr_t) dst) {
[2d7a5fe]66 for (i = 0; i < cnt; i++)
[7f1c620]67 ((uint8_t *) dst)[i] = ((uint8_t *) src)[i];
[2d7a5fe]68 } else {
[0f269c2]69 for (i = 0; i < cnt / sizeof(unative_t); i++)
[7f1c620]70 ((unative_t *) dst)[i] = ((unative_t *) src)[i];
[7a255e69]71
[0f269c2]72 for (j = 0; j < cnt % sizeof(unative_t); j++)
[9f3363e]73 ((uint8_t *)(((unative_t *) dst) + i))[j] =
74 ((uint8_t *)(((unative_t *) src) + i))[j];
[2d7a5fe]75 }
[9c0a9b3]76
[da349da0]77 return (char *) dst;
[f761f1eb]78}
79
[ac48fef]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
[d34657e]125/** Fill block of memory
126 *
[9f3363e]127 * Fill cnt bytes at dst address with the value x. The filling is done
128 * byte-by-byte.
[d34657e]129 *
[9f3363e]130 * @param dst Destination address to fill.
131 * @param cnt Number of bytes to fill.
132 * @param x Value to fill.
[d34657e]133 *
134 */
[e32e092]135void _memsetb(void *dst, size_t cnt, uint8_t x)
[f761f1eb]136{
[6c441cf8]137 unsigned int i;
[7f1c620]138 uint8_t *p = (uint8_t *) dst;
[f761f1eb]139
[bd1deed]140 for (i = 0; i < cnt; i++)
[f761f1eb]141 p[i] = x;
142}
[379d73f3]143
[9f3363e]144/** Fill block of memory.
[379d73f3]145 *
[9f3363e]146 * Fill cnt words at dst address with the value x. The filling is done
147 * word-by-word.
[379d73f3]148 *
[9f3363e]149 * @param dst Destination address to fill.
150 * @param cnt Number of words to fill.
151 * @param x Value to fill.
[379d73f3]152 *
153 */
[e32e092]154void _memsetw(void *dst, size_t cnt, uint16_t x)
[379d73f3]155{
[6c441cf8]156 unsigned int i;
[7f1c620]157 uint16_t *p = (uint16_t *) dst;
[379d73f3]158
[bd1deed]159 for (i = 0; i < cnt; i++)
[379d73f3]160 p[i] = x;
161}
[b45c443]162
[cc73a8a1]163/** @}
[b45c443]164 */
Note: See TracBrowser for help on using the repository browser.