Attempt to write an iterator for the multiboot memory map list

This commit is contained in:
Eryn Wells 2016-03-19 12:01:01 -04:00
parent fad5a48e5b
commit ec74cb650c
2 changed files with 134 additions and 12 deletions

View file

@ -52,6 +52,71 @@ Information::setInformation(Information *info)
* Public * Public
*/ */
Information::MemoryMapIterator::MemoryMapIterator(uint32_t address,
uint32_t count)
: mCurrent(reinterpret_cast<MemoryChunk *>(address)),
mCount(count)
{ }
Information::MemoryMapIterator::MemoryMapIterator(const MemoryMapIterator& other)
: mCurrent(other.mCurrent),
mCount(other.mCount)
{ }
Information::MemoryMapIterator&
Information::MemoryMapIterator::operator++()
{
if (mCount > 0) {
auto p = reinterpret_cast<uint32_t *>(mCurrent);
// The size of the current struct is stored at (mCurrent - 4) bytes.
mCurrent = (mCurrent + p[-1]);
mCount--;
}
return *this;
}
Information::MemoryMapIterator
Information::MemoryMapIterator::operator++(int)
{
MemoryMapIterator tmp(reinterpret_cast<uint32_t>(mCurrent), mCount);
operator++();
return tmp;
}
MemoryChunk
Information::MemoryMapIterator::operator*()
{
if (mCount == 0) {
return MemoryChunk{0, 0, 0};
}
return *mCurrent;
}
bool
Information::MemoryMapIterator::operator==(const MemoryMapIterator& other)
const
{
if (mCount == 0 && other.mCount == 0) {
// If both iterators have 0 counts, it doesn't matter what the mCurrent members point at.
return true;
}
return mCurrent == other.mCurrent && mCount == other.mCount;
}
bool
Information::MemoryMapIterator::operator!=(const MemoryMapIterator& other)
const
{
return !(*this == other);
}
uint32_t uint32_t
Information::lowerMemoryKB() Information::lowerMemoryKB()
const const
@ -83,5 +148,35 @@ Information::commandLine()
} }
return reinterpret_cast<const char *>(mCommandLine); return reinterpret_cast<const char *>(mCommandLine);
} }
uint32_t
Information::memoryMapChunks()
const
{
if ((mFlags & Present::MemoryMap) == 0) {
return 0;
}
return memoryMapCount;
}
Information::MemoryMapIterator
Information::memoryMapBegin()
const
{
if ((mFlags & Present::MemoryMap) == 0) {
return memoryMapEnd();
}
return MemoryMapIterator(memoryMapAddress, memoryMapCount);
}
Information::MemoryMapIterator
Information::memoryMapEnd()
const
{
return {0, 0};
}
} /* namespace multiboot */ } /* namespace multiboot */

View file

@ -15,19 +15,51 @@
namespace multiboot { namespace multiboot {
struct PACKED MemoryChunk
{
/** Base address of the chunk of memory. */
uint64_t base;
/** Length of the chunk of memory, in bytes. */
uint64_t length;
/** Type of address range. 1 indicates available; all other values indicate reserved memory. */
uint32_t type;
};
/** /**
* The multiboot information struct. Defined by the multiboot spec. * The multiboot information struct. Defined by the multiboot spec.
* See http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Machine-state * See http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Machine-state
*/ */
struct PACKED Information struct PACKED Information
{ {
struct MemoryMapIterator
{
MemoryMapIterator(uint32_t address, uint32_t count);
MemoryMapIterator(const MemoryMapIterator& other);
MemoryMapIterator& operator++();
MemoryMapIterator operator++(int);
MemoryChunk operator*();
bool operator==(const MemoryMapIterator& other) const;
bool operator!=(const MemoryMapIterator& other) const;
private:
MemoryChunk *mCurrent;
uint32_t mCount;
};
static const Information *information(); static const Information *information();
static void setInformation(Information* info); static void setInformation(Information* info);
uint32_t lowerMemoryKB() const; uint32_t lowerMemoryKB() const;
uint32_t upperMemoryKB() const; uint32_t upperMemoryKB() const;
const char* commandLine() const; const char* commandLine() const;
uint32_t memoryMapChunks() const;
MemoryMapIterator memoryMapBegin() const;
MemoryMapIterator memoryMapEnd() const;
private: private:
/** /**
* Bit field of flags. Fields below are only defined if the appropriate * Bit field of flags. Fields below are only defined if the appropriate
@ -98,16 +130,16 @@ private:
} symbols; } symbols;
/** /**
* @defgroup Memory Map
* Points to a buffer containing a memory map of the machine provided by the * Points to a buffer containing a memory map of the machine provided by the
* BIOS. Defined only if `memoryMapPresent == true`. * BIOS. Defined only if `memoryMapPresent == true`.
* @{
*/ */
struct PACKED /** Number of memory map entries present. */
{ uint32_t memoryMapCount;
/** Number of memory map entries present. */ /** Pointer to start of memory map entry array. */
uint32_t count; uint32_t memoryMapAddress;
/** Pointer to start of memory map entry array. */ /** @} */
uint32_t address;
} memoryMap;
/** /**
* Points to a buffer containing a list of drive definitions provided by the * Points to a buffer containing a list of drive definitions provided by the
@ -152,11 +184,6 @@ struct PACKED Module
uint32_t reserved; uint32_t reserved;
}; };
// TODO: Define the MemoryMap struct
struct PACKED MemoryChunk
{
};
// TODO: Define the Drive struct // TODO: Define the Drive struct
struct PACKED Drive struct PACKED Drive
{ {