FreeNOS
MemoryBlock.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifdef __clang__
19#pragma clang optimize off
20#endif
21
22#ifdef __GNUC__
23#pragma GCC push_options
24#pragma GCC optimize ("O0")
25#endif
26
27#include "Macros.h"
28#include "MemoryBlock.h"
29
30void * MemoryBlock::set(void *dest, int ch, unsigned count)
31{
32 char *temp;
33
34 for(temp = (char *) dest; count != 0; count--)
35 {
36 *temp++ = ch;
37 }
38 return (dest);
39}
40
41Size MemoryBlock::copy(void *dest, const void *src, Size count)
42{
43 const char *sp = (const char *)src;
44 char *dp = (char *)dest;
45
46 for(Size i = count; i != 0; i--)
47 *dp++ = *sp++;
48
49 return (count);
50}
51
52Size MemoryBlock::copy(char *dst, char *src, Size count)
53{
54 char *d = dst;
55 const char *s = src;
56 unsigned n = count;
57
58 // Copy as many bytes as will fit
59 if (n != 0) {
60 while (--n != 0) {
61 if ((*d++ = *s++) == '\0')
62 break;
63 }
64 }
65
66 // Not enough room in dst, add NUL and traverse rest of src
67 if (n == 0) {
68 if (count != 0)
69 *d = '\0';
70 while (*s++)
71 ;
72 }
73 // Count does not include NUL
74 return(s - src - 1);
75}
76
77bool MemoryBlock::compare(const void *p1, const void *p2, const Size count)
78{
79 const char *ch1 = (const char *) p1;
80 const char *ch2 = (const char *) p2;
81
82 for (Size i = 0; i < count; i++)
83 {
84 if (*ch1++ != *ch2++)
85 {
86 return false;
87 }
88 }
89
90 return true;
91}
92
93bool MemoryBlock::compare(const char *p1, const char *p2, const Size count)
94{
95 const char *ch1 = (const char *) p1;
96 const char *ch2 = (const char *) p2;
97 Size bytes = 0;
98
99 while (*ch1 == *ch2)
100 {
101 if (*ch1 == ZERO || *ch2 == ZERO)
102 {
103 break;
104 }
105
106 ch1++, ch2++, bytes++;
107
108 if (count != 0 && bytes >= count)
109 {
110 return true;
111 }
112 }
113
114 return *ch1 == *ch2;
115}
u32 sp
Definition ARMCore.h:2
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
static bool compare(const void *p1, const void *p2, const Size count)
Compare memory.
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43