Max OS 0.3
Loading...
Searching...
No Matches
memoryIO.h File Reference

Defines classes for memory input and output operations of various bit widths (8, 16, 32, 64 bits). More...

#include <cstdint>
#include <cstddef>

Go to the source code of this file.

Classes

class  MaxOS::memory::MemIO
 base class for all memory IO More...
 
class  MaxOS::memory::MemIO8Bit
 Handles 8 bit memory IO. More...
 
class  MaxOS::memory::MemIO16Bit
 Handles 16 bit memory IO. More...
 
class  MaxOS::memory::MemIO32Bit
 Handles 32 bit memory IO. More...
 
class  MaxOS::memory::MemIO64Bit
 Handles 64 bit memory IO. More...
 

Functions

void * memcpy (void *destination, const void *source, uint64_t num)
 Copies a block of memory from one location to another.
 
void * memset (void *ptr, uint32_t value, uint64_t num)
 Fills a block of memory with a specified value.
 
void * memmove (void *destination, const void *source, uint64_t num)
 Copies a block of memory from one location to another.
 
int memcmp (const void *ptr1, const void *ptr2, uint64_t num)
 Compares two blocks of memory.
 

Detailed Description

Defines classes for memory input and output operations of various bit widths (8, 16, 32, 64 bits).

Date
29th November 2022
Author
Max Tyson

Definition in file memoryIO.h.

Function Documentation

◆ memcmp()

int memcmp ( const void *  ptr1,
const void *  ptr2,
uint64_t  num 
)

Compares two blocks of memory.

Parameters
ptr1The m_first_memory_chunk block of memory
ptr2The second block of memory
numThe number of bytes to compare
Returns
0 if the blocks of memory are equal, -1 if ptr1 < ptr2, 1 if ptr1 > ptr2

Definition at line 265 of file memoryIO.cpp.

265 {
266
267 // Make sure the pointers exist
268 if (ptr1 == nullptr || ptr2 == nullptr)
269 return 0;
270
271 const auto* p1 = (const unsigned char*) ptr1;
272 const auto* p2 = (const unsigned char*) ptr2;
273 for (size_t i = 0; i < num; i++) {
274 if (p1[i] < p2[i])
275 return -1;
276 if (p1[i] > p2[i])
277 return 1;
278 }
279 return 0;
280}

◆ memcpy()

void * memcpy ( void *  destination,
const void *  source,
uint64_t  num 
)

Copies a block of memory from one location to another.

See also
https://wiki.osdev.org/Meaty_Skeleton#memcpy.28.29
Parameters
destinationThe destination to copy to
sourceThe source to copy from
numThe number of bytes to copy
Returns
The destination

Definition at line 165 of file memoryIO.cpp.

165 {
166
167 // Make sure the source and destination are not the same
168 if (destination == source)
169 return destination;
170
171 // Make sure they exist
172 if (destination == nullptr || source == nullptr)
173 return destination;
174
175 // Get the source and destination
176 auto* dst = (unsigned char*) destination;
177 const auto* src = (const unsigned char*) source;
178
179 // Copy the data
180 for (size_t i = 0; i < num; i++)
181 dst[i] = src[i];
182
183 // Usefully for easier code writing
184 return destination;
185}

Referenced by MaxOS::common::Buffer::copy_from(), MaxOS::common::Buffer::copy_from(), MaxOS::common::Buffer::copy_to(), MaxOS::common::Buffer::copy_to(), MaxOS::processes::SharedMessageEndpoint::read(), MaxOS::common::Buffer::resize(), and MaxOS::processes::Thread::Thread().

◆ memmove()

void * memmove ( void *  destination,
const void *  source,
uint64_t  num 
)

Copies a block of memory from one location to another.

Parameters
destinationThe destination to copy to
sourceThe source to copy from
numThe number of bytes to copy
Returns
The destination

Definition at line 235 of file memoryIO.cpp.

235 {
236
237 // Make sure the source and destination are not the same
238 if (destination == source)
239 return destination;
240
241 // Make sure they exist
242 if (destination == nullptr || source == nullptr)
243 return destination;
244
245 auto* dst = (unsigned char*) destination;
246 const auto* src = (const unsigned char*) source;
247 if (dst < src) {
248 for (size_t i = 0; i < num; i++)
249 dst[i] = src[i];
250 } else {
251 for (size_t i = num; i != 0; i--)
252 dst[i - 1] = src[i - 1];
253 }
254 return destination;
255}

Referenced by MaxOS::drivers::console::VESABootConsole::scroll_up().

◆ memset()

void * memset ( void *  ptr,
uint32_t  value,
uint64_t  num 
)

Fills a block of memory with a specified value.

Parameters
ptrThe pointer to the block of memory
valueThe value to fill the block of memory with
numThe number of bytes to fill
Returns
The pointer to the block of memory

Definition at line 215 of file memoryIO.cpp.

215 {
216
217 // Make sure the pointer exists
218 if (ptr == nullptr)
219 return ptr;
220
221 auto* dst = (uint32_t*) ptr;
222 for (size_t i = 0; i < num; i++)
223 dst[i] = value;
224 return ptr;
225}

Referenced by MaxOS::common::Buffer::clear(), MaxOS::common::Buffer::full(), and MaxOS::drivers::console::VESABootConsole::print_logo().