Refactored code to use {} initialization instead of dynamic allocation with 'new' in disk manager class

This commit is contained in:
Alex 2023-04-10 06:24:44 +03:00
parent 41dafe32fb
commit 78f4bdd6a8
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 45 additions and 45 deletions

View File

@ -42,11 +42,11 @@ namespace Disk
for (unsigned char ItrPort = 0; ItrPort < this->AvailablePorts; ItrPort++) for (unsigned char ItrPort = 0; ItrPort < this->AvailablePorts; ItrPort++)
{ {
Drive *drive = new Drive; Drive drive{};
sprintf(drive->Name, "sd%ld-%d", DriverUID, this->AvailablePorts); sprintf(drive.Name, "sd%ld-%d", DriverUID, this->AvailablePorts);
debug("Drive Name: %s", drive->Name); debug("Drive Name: %s", drive.Name);
// TODO: Implement disk type detection. Very useful in the future. // TODO: Implement disk type detection. Very useful in the future.
drive->MechanicalDisk = true; drive.MechanicalDisk = true;
memset(RWBuffer, 0, this->BytesPerSector); memset(RWBuffer, 0, this->BytesPerSector);
callback.Reason = ReceiveReason; callback.Reason = ReceiveReason;
@ -58,17 +58,17 @@ namespace Disk
.Write = false, .Write = false,
}; };
DriverManager->IOCB(DriverUID, (void *)&callback); DriverManager->IOCB(DriverUID, (void *)&callback);
memcpy(&drive->Table, RWBuffer, sizeof(PartitionTable)); memcpy(&drive.Table, RWBuffer, sizeof(PartitionTable));
/* /*
TODO: Add to devfs the disk TODO: Add to devfs the disk
*/ */
if (drive->Table.GPT.Signature == GPT_MAGIC) if (drive.Table.GPT.Signature == GPT_MAGIC)
{ {
drive->Style = GPT; drive.Style = GPT;
uint32_t Entries = 512 / drive->Table.GPT.EntrySize; uint32_t Entries = 512 / drive.Table.GPT.EntrySize;
uint32_t Sectors = drive->Table.GPT.PartCount / Entries; uint32_t Sectors = drive.Table.GPT.PartCount / Entries;
for (uint32_t Block = 0; Block < Sectors; Block++) for (uint32_t Block = 0; Block < Sectors; Block++)
{ {
memset(RWBuffer, 0, this->BytesPerSector); memset(RWBuffer, 0, this->BytesPerSector);
@ -87,17 +87,17 @@ namespace Disk
GUIDPartitionTablePartition GPTPartition = reinterpret_cast<GUIDPartitionTablePartition *>(RWBuffer)[e]; GUIDPartitionTablePartition GPTPartition = reinterpret_cast<GUIDPartitionTablePartition *>(RWBuffer)[e];
if (GPTPartition.TypeLow || GPTPartition.TypeHigh) if (GPTPartition.TypeLow || GPTPartition.TypeHigh)
{ {
Partition *partition = new Partition; Partition partition{};
memcpy(partition->Label, GPTPartition.Label, sizeof(partition->Label)); memcpy(partition.Label, GPTPartition.Label, sizeof(partition.Label));
partition->StartLBA = GPTPartition.StartLBA; partition.StartLBA = GPTPartition.StartLBA;
partition->EndLBA = GPTPartition.EndLBA; partition.EndLBA = GPTPartition.EndLBA;
partition->Sectors = partition->EndLBA - partition->StartLBA; partition.Sectors = partition.EndLBA - partition.StartLBA;
partition->Port = ItrPort; partition.Port = ItrPort;
partition->Flags = Present; partition.Flags = Present;
partition->Style = GPT; partition.Style = GPT;
if (GPTPartition.Attributes & 1) if (GPTPartition.Attributes & 1)
partition->Flags |= EFISystemPartition; partition.Flags |= EFISystemPartition;
partition->Index = drive->Partitions.size(); partition.Index = drive.Partitions.size();
// why there is NUL (\0) between every char????? // why there is NUL (\0) between every char?????
char PartName[72]; char PartName[72];
memcpy(PartName, GPTPartition.Label, 72); memcpy(PartName, GPTPartition.Label, 72);
@ -105,56 +105,56 @@ namespace Disk
if (PartName[i] == '\0') if (PartName[i] == '\0')
PartName[i] = ' '; PartName[i] = ' ';
PartName[71] = '\0'; PartName[71] = '\0';
trace("GPT partition \"%s\" found with %lld sectors", PartName, partition->Sectors); trace("GPT partition \"%s\" found with %lld sectors", PartName, partition.Sectors);
drive->Partitions.push_back(partition); drive.Partitions.push_back(partition);
char *PartitionName = new char[64]; // char *PartitionName = new char[64];
sprintf(PartitionName, "sd%ldp%ld", drives.size() - 1, partition->Index); // sprintf(PartitionName, "sd%ldp%ld", drives.size() - 1, partition.Index);
/* /*
TODO: Add to devfs the disk TODO: Add to devfs the disk
*/ */
delete[] PartitionName; // delete[] PartitionName;
} }
} }
} }
trace("%d GPT partitions found.", drive->Partitions.size()); trace("%d GPT partitions found.", drive.Partitions.size());
} }
else if (drive->Table.MBR.Signature[0] == MBR_MAGIC0 && drive->Table.MBR.Signature[1] == MBR_MAGIC1) else if (drive.Table.MBR.Signature[0] == MBR_MAGIC0 && drive.Table.MBR.Signature[1] == MBR_MAGIC1)
{ {
drive->Style = MBR; drive.Style = MBR;
for (size_t p = 0; p < 4; p++) for (size_t p = 0; p < 4; p++)
if (drive->Table.MBR.Partitions[p].LBAFirst != 0) if (drive.Table.MBR.Partitions[p].LBAFirst != 0)
{ {
Partition *partition = new Partition; Partition partition{};
partition->StartLBA = drive->Table.MBR.Partitions[p].LBAFirst; partition.StartLBA = drive.Table.MBR.Partitions[p].LBAFirst;
partition->EndLBA = drive->Table.MBR.Partitions[p].LBAFirst + drive->Table.MBR.Partitions[p].Sectors; partition.EndLBA = drive.Table.MBR.Partitions[p].LBAFirst + drive.Table.MBR.Partitions[p].Sectors;
partition->Sectors = drive->Table.MBR.Partitions[p].Sectors; partition.Sectors = drive.Table.MBR.Partitions[p].Sectors;
partition->Port = ItrPort; partition.Port = ItrPort;
partition->Flags = Present; partition.Flags = Present;
partition->Style = MBR; partition.Style = MBR;
partition->Index = drive->Partitions.size(); partition.Index = drive.Partitions.size();
trace("Partition \"%#llx\" found with %lld sectors.", drive->Table.MBR.UniqueID, partition->Sectors); trace("Partition \"%#llx\" found with %lld sectors.", drive.Table.MBR.UniqueID, partition.Sectors);
drive->Partitions.push_back(partition); drive.Partitions.push_back(partition);
char *PartitionName = new char[64]; // char *PartitionName = new char[64];
sprintf(PartitionName, "sd%ldp%ld", drives.size() - 1, partition->Index); // sprintf(PartitionName, "sd%ldp%ld", drives.size() - 1, partition.Index);
/* /*
TODO: Add to devfs the disk TODO: Add to devfs the disk
*/ */
delete[] PartitionName; // delete[] PartitionName;
} }
trace("%d MBR partitions found.", drive->Partitions.size()); trace("%d MBR partitions found.", drive.Partitions.size());
} }
else else
warn("No partition table found on port %d!", ItrPort); warn("No partition table found on port %d!", ItrPort);
drives.push_back(drive); drives.push_back(drive);
} }
KernelAllocator.FreePages(RWBuffer, TO_PAGES(this->BytesPerSector + 1)); KernelAllocator.FreePages(RWBuffer, TO_PAGES(this->BytesPerSector + 1));
} }

View File

@ -138,7 +138,7 @@ namespace Disk
uint8_t *Buffer = nullptr; uint8_t *Buffer = nullptr;
PartitionTable Table; PartitionTable Table;
PartitionStyle Style = PartitionStyle::Unknown; PartitionStyle Style = PartitionStyle::Unknown;
std::vector<Partition *> Partitions; std::vector<Partition> Partitions;
bool MechanicalDisk = false; bool MechanicalDisk = false;
size_t UniqueIdentifier = 0xdeadbeef; size_t UniqueIdentifier = 0xdeadbeef;
@ -170,7 +170,7 @@ namespace Disk
unsigned char AvailablePorts = 0; unsigned char AvailablePorts = 0;
int BytesPerSector = 0; int BytesPerSector = 0;
std::vector<Drive *> drives; std::vector<Drive> drives;
public: public:
void FetchDisks(unsigned long DriverUID); void FetchDisks(unsigned long DriverUID);