1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
Copyright (c) 2014-2015 Datalight, Inc.
All Rights Reserved Worldwide.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; use version 2 of the License.
This program is distributed in the hope that it will be useful,
but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Businesses and individuals that for commercial or other reasons cannot
comply with the terms of the GPLv2 license may obtain a commercial license
before incorporating Reliance Edge into proprietary software for
distribution in any form. Visit http://www.datalight.com/reliance-edge for
more information.
*/
/** @file
@brief Default implementations of memory manipulation functions.
These implementations are intended to be small and simple, and thus forego
all optimizations. If the C library is available, or if there are better
third-party implementations available in the system, those can be used
instead by defining the appropriate macros in redconf.h.
These functions are not intended to be completely 100% ANSI C compatible
implementations, but rather are designed to meet the needs of Reliance Edge.
The compatibility is close enough that ANSI C compatible implementations
can be "dropped in" as replacements without difficulty.
*/
#include <redfs.h>
#ifndef RedMemCpyUnchecked
static void RedMemCpyUnchecked(void *pDest, const void *pSrc, uint32_t ulLen);
#endif
#ifndef RedMemMoveUnchecked
static void RedMemMoveUnchecked(void *pDest, const void *pSrc, uint32_t ulLen);
#endif
#ifndef RedMemSetUnchecked
static void RedMemSetUnchecked(void *pDest, uint8_t bVal, uint32_t ulLen);
#endif
#ifndef RedMemCmpUnchecked
static int32_t RedMemCmpUnchecked(const void *pMem1, const void *pMem2, uint32_t ulLen);
#endif
/** @brief Copy memory from one address to another.
The source and destination memory buffers should not overlap. If the
buffers overlap, use RedMemMove() instead.
@param pDest The destination buffer.
@param pSrc The source buffer.
@param ulLen The number of bytes to copy.
*/
void RedMemCpy(
void *pDest,
const void *pSrc,
uint32_t ulLen)
{
if((pDest == NULL) || (pSrc == NULL))
{
REDERROR();
}
else
{
RedMemCpyUnchecked(pDest, pSrc, ulLen);
}
}
#ifndef RedMemCpyUnchecked
/** @brief Copy memory from one address to another.
This function should only be called from RedMemCpy().
@param pDest The destination buffer.
@param pSrc The source buffer.
@param ulLen The number of bytes to copy.
*/
static void RedMemCpyUnchecked(
void *pDest,
const void *pSrc,
uint32_t ulLen)
{
uint8_t *pbDest = CAST_VOID_PTR_TO_UINT8_PTR(pDest);
const uint8_t *pbSrc = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pSrc);
uint32_t ulIdx;
for(ulIdx = 0U; ulIdx < ulLen; ulIdx++)
{
pbDest[ulIdx] = pbSrc[ulIdx];
}
}
#endif
/** @brief Move memory from one address to another.
Supports overlapping memory regions. If memory regions do not overlap, it
is generally better to use RedMemCpy() instead.
@param pDest The destination buffer.
@param pSrc The source buffer.
@param ulLen The number of bytes to copy.
*/
void RedMemMove(
void *pDest,
const void *pSrc,
uint32_t ulLen)
{
if((pDest == NULL) || (pSrc == NULL))
{
REDERROR();
}
else
{
RedMemMoveUnchecked(pDest, pSrc, ulLen);
}
}
#ifndef RedMemMoveUnchecked
/** @brief Move memory from one address to another.
This function should only be called from RedMemMove().
@param pDest The destination buffer.
@param pSrc The source buffer.
@param ulLen The number of bytes to copy.
*/
static void RedMemMoveUnchecked(
void *pDest,
const void *pSrc,
uint32_t ulLen)
{
uint8_t *pbDest = CAST_VOID_PTR_TO_UINT8_PTR(pDest);
const uint8_t *pbSrc = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pSrc);
uint32_t ulIdx;
if(MEMMOVE_MUST_COPY_FORWARD(pbDest, pbSrc))
{
/* If the destination is lower than the source with overlapping memory
regions, we must copy from start to end in order to copy the memory
correctly.
Don't use RedMemCpy() to do this. It is possible that RedMemCpy()
has been replaced (even though this function has not been replaced)
with an implementation that cannot handle any kind of buffer
overlap.
*/
for(ulIdx = 0U; ulIdx < ulLen; ulIdx++)
{
pbDest[ulIdx] = pbSrc[ulIdx];
}
}
else
{
ulIdx = ulLen;
while(ulIdx > 0U)
{
ulIdx--;
pbDest[ulIdx] = pbSrc[ulIdx];
}
}
}
#endif /* RedMemMoveUnchecked */
/** @brief Initialize a buffer with the specified byte value.
@param pDest The buffer to initialize.
@param bVal The byte value with which to initialize @p pDest.
@param ulLen The number of bytes to initialize.
*/
void RedMemSet(
void *pDest,
uint8_t bVal,
uint32_t ulLen)
{
if(pDest == NULL)
{
REDERROR();
}
else
{
RedMemSetUnchecked(pDest, bVal, ulLen);
}
}
#ifndef RedMemSetUnchecked
/** @brief Initialize a buffer with the specified byte value.
This function should only be called from RedMemSet().
@param pDest The buffer to initialize.
@param bVal The byte value with which to initialize @p pDest.
@param ulLen The number of bytes to initialize.
*/
static void RedMemSetUnchecked(
void *pDest,
uint8_t bVal,
uint32_t ulLen)
{
uint8_t *pbDest = CAST_VOID_PTR_TO_UINT8_PTR(pDest);
uint32_t ulIdx;
for(ulIdx = 0U; ulIdx < ulLen; ulIdx++)
{
pbDest[ulIdx] = bVal;
}
}
#endif
/** @brief Compare the contents of two buffers.
@param pMem1 The first buffer to compare.
@param pMem2 The second buffer to compare.
@param ulLen The length to compare.
@return Zero if the two buffers are the same, otherwise nonzero.
@retval 0 @p pMem1 and @p pMem2 are the same.
@retval 1 @p pMem1 is greater than @p pMem2, as determined by the
values of the first differing bytes.
@retval -1 @p pMem2 is greater than @p pMem1, as determined by the
values of the first differing bytes.
*/
int32_t RedMemCmp(
const void *pMem1,
const void *pMem2,
uint32_t ulLen)
{
int32_t lResult;
if((pMem1 == NULL) || (pMem2 == NULL))
{
REDERROR();
lResult = 0;
}
else
{
lResult = RedMemCmpUnchecked(pMem1, pMem2, ulLen);
}
return lResult;
}
#ifndef RedMemCmpUnchecked
/** @brief Compare the contents of two buffers.
@param pMem1 The first buffer to compare.
@param pMem2 The second buffer to compare.
@param ulLen The length to compare.
@return Zero if the two buffers are the same, otherwise nonzero.
*/
static int32_t RedMemCmpUnchecked(
const void *pMem1,
const void *pMem2,
uint32_t ulLen)
{
const uint8_t *pbMem1 = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pMem1);
const uint8_t *pbMem2 = CAST_VOID_PTR_TO_CONST_UINT8_PTR(pMem2);
uint32_t ulIdx = 0U;
int32_t lResult;
while((ulIdx < ulLen) && (pbMem1[ulIdx] == pbMem2[ulIdx]))
{
ulIdx++;
}
if(ulIdx == ulLen)
{
lResult = 0;
}
else if(pbMem1[ulIdx] > pbMem2[ulIdx])
{
lResult = 1;
}
else
{
lResult = -1;
}
return lResult;
}
#endif
|