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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8caaea7 was cb4f078, checked in by Martin Decky <martin@…>, 14 years ago

unify kernel byte string implementations

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2008 Jiri Svoboda
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
30/** @addtogroup generic
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Memory string operations.
37 *
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.
41 */
42
43#include <memstr.h>
44#include <typedefs.h>
45#include <align.h>
46
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 */
56void memsetb(void *dst, size_t cnt, uint8_t val)
57{
58 __builtin_memset(dst, val, cnt);
59}
60
61/** Fill block of memory.
62 *
63 * Fill cnt words at dst address with the value val. The filling
64 * is done word-by-word.
65 *
66 * @param dst Destination address to fill.
67 * @param cnt Number of words to fill.
68 * @param val Value to fill.
69 *
70 */
71void memsetw(void *dst, size_t cnt, uint16_t val)
72{
73 size_t i;
74 uint16_t *ptr = (uint16_t *) dst;
75
76 for (i = 0; i < cnt; i++)
77 ptr[i] = val;
78}
79
80/** Fill block of memory.
81 *
82 * Fill cnt bytes at dst address with the value val.
83 *
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.
89 *
90 */
91void *memset(void *dst, int val, size_t cnt)
92{
93 return __builtin_memset(dst, val, cnt);
94}
95
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 */
108void *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 */
125void *memmove(void *dst, const void *src, size_t cnt)
126{
127 /* Nothing to do? */
128 if (src == dst)
129 return dst;
130
131 /* Non-overlapping? */
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
138 /* Which direction? */
139 if (src > dst) {
140 /* Forwards. */
141 sp = src;
142 dp = dst;
143
144 while (cnt-- != 0)
145 *dp++ = *sp++;
146 } else {
147 /* Backwards. */
148 sp = src + (cnt - 1);
149 dp = dst + (cnt - 1);
150
151 while (cnt-- != 0)
152 *dp-- = *sp--;
153 }
154
155 return dst;
156}
157
158/** @}
159 */
Note: See TracBrowser for help on using the repository browser.