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 #pragma clang optimize off
19 #pragma GCC push_options
20 #pragma GCC optimize ("O0")
21 
22 #include "Macros.h"
23 #include "MemoryBlock.h"
24 
25 void * MemoryBlock::set(void *dest, int ch, unsigned count)
26 {
27  char *temp;
28 
29  for(temp = (char *) dest; count != 0; count--)
30  {
31  *temp++ = ch;
32  }
33  return (dest);
34 }
35 
36 Size MemoryBlock::copy(void *dest, const void *src, Size count)
37 {
38  const char *sp = (const char *)src;
39  char *dp = (char *)dest;
40 
41  for(Size i = count; i != 0; i--)
42  *dp++ = *sp++;
43 
44  return (count);
45 }
46 
47 Size MemoryBlock::copy(char *dst, char *src, Size count)
48 {
49  char *d = dst;
50  const char *s = src;
51  unsigned n = count;
52 
53  // Copy as many bytes as will fit
54  if (n != 0) {
55  while (--n != 0) {
56  if ((*d++ = *s++) == '\0')
57  break;
58  }
59  }
60 
61  // Not enough room in dst, add NUL and traverse rest of src
62  if (n == 0) {
63  if (count != 0)
64  *d = '\0';
65  while (*s++)
66  ;
67  }
68  // Count does not include NUL
69  return(s - src - 1);
70 }
71 
72 bool MemoryBlock::compare(const void *p1, const void *p2, const Size count)
73 {
74  const char *ch1 = (const char *) p1;
75  const char *ch2 = (const char *) p2;
76 
77  for (Size i = 0; i < count; i++)
78  {
79  if (*ch1++ != *ch2++)
80  {
81  return false;
82  }
83  }
84 
85  return true;
86 }
87 
88 bool MemoryBlock::compare(const char *p1, const char *p2, const Size count)
89 {
90  const char *ch1 = (const char *) p1;
91  const char *ch2 = (const char *) p2;
92  Size bytes = 0;
93 
94  while (*ch1 == *ch2)
95  {
96  if (*ch1 == ZERO || *ch2 == ZERO)
97  {
98  break;
99  }
100 
101  ch1++, ch2++, bytes++;
102 
103  if (count != 0 && bytes >= count)
104  {
105  return true;
106  }
107  }
108 
109  return *ch1 == *ch2;
110 }
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
Macros.h
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
sp
u32 sp
Definition: ARMCore.h:233
MemoryBlock.h
MemoryBlock::compare
static bool compare(const void *p1, const void *p2, const Size count)
Compare memory.
Definition: MemoryBlock.cpp:72
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43