Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
fix: realloc ne copiait pas le contenu du pointeur
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainTHD committed May 13, 2021
1 parent a2a328c commit b20b815
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions libc/stdlib/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
namespace stdlib {
namespace {
/**
* Combine deux segments libres et évite la fragmentation
*
* Merge two free segments and avoids segmentation
* @param segA Segment A
* @param segB Segment B
*/
Expand All @@ -29,8 +28,7 @@ namespace stdlib {
segB->nextSegment->prevSegment = segA;
segB->nextSegment->prevFreeSegment = segA;
segB->nextFreeSegment->prevFreeSegment = segA;
}
else {
} else {
segB->length += segA->length + sizeof(MemorySegmentHeader);
segB->nextSegment = segA->nextSegment;
segB->nextFreeSegment = segA->nextFreeSegment;
Expand All @@ -42,7 +40,7 @@ namespace stdlib {
}

/**
* 1er segment
* 1st segment
*/
MemorySegmentHeader* _firstFreeSegment;
}
Expand All @@ -58,7 +56,7 @@ namespace stdlib {
}

void* malloc(size_t size) {
// Alignement de la RAM, utile pour meilleures performances sur hardware 64 bits
// RAM alignment, useful for better performances on 64 bits hardware

u64 remainder = size % 8;
size -= remainder;
Expand All @@ -71,12 +69,12 @@ namespace stdlib {

while (true) {
if (currentMemorySegment->length >= size) {
// On a trouvé un segment libre
// Free segment found

if (currentMemorySegment->length > size + sizeof(MemorySegmentHeader)) {
// On va split le segment pour n'allouer que ce qu'il faut
// Split the segment to allocate only what is needed

MemorySegmentHeader* newSegment = (MemorySegmentHeader*) ((u64) currentMemorySegment + sizeof(MemorySegmentHeader) + size);
auto* newSegment = (MemorySegmentHeader*) ((u64) currentMemorySegment + sizeof(MemorySegmentHeader) + size);
newSegment->isFree = true;
newSegment->length = (u64) currentMemorySegment->length - (sizeof(MemorySegmentHeader) + size);
newSegment->nextSegment = currentMemorySegment->nextSegment;
Expand All @@ -89,20 +87,20 @@ namespace stdlib {
currentMemorySegment->length = size;
}

// Special case when 1st segment
if (currentMemorySegment == _firstFreeSegment) {
// Cas spécial quand 1er segment
_firstFreeSegment = currentMemorySegment->nextFreeSegment;
}

currentMemorySegment->isFree = false;

// Change free segments inside the linked list

if (currentMemorySegment->prevFreeSegment != nullptr) {
// Changement des segments libres de la liste chainée
currentMemorySegment->prevFreeSegment->nextFreeSegment = currentMemorySegment->nextFreeSegment;
}

if (currentMemorySegment->nextFreeSegment != nullptr) {
// Changement des segments libres de la liste chainée
currentMemorySegment->nextFreeSegment->prevFreeSegment = currentMemorySegment->prevFreeSegment;
}

Expand All @@ -117,8 +115,8 @@ namespace stdlib {
return currentMemorySegment + 1;
}

// No more available memory. Shouldn't happen, but if it happens we should do additional paging
if (currentMemorySegment->nextFreeSegment == nullptr) {
// Plus de mémoire restante. Normalement impossible, si on arrive ici on doit refaire du paging
return nullptr;
}

Expand All @@ -130,7 +128,7 @@ namespace stdlib {
return memset<byte>(ptr, value, nb);
}

void* calloc(size_t nb, size_t size) {
[[maybe_unused]] void* calloc(size_t nb, size_t size) {
return memset(malloc(nb*size), 0, nb);
}

Expand All @@ -149,31 +147,33 @@ namespace stdlib {
}

if (currentMemorySegment->prevFreeSegment != nullptr) {
if (currentMemorySegment->prevFreeSegment->nextFreeSegment > currentMemorySegment)
if (currentMemorySegment->prevFreeSegment->nextFreeSegment > currentMemorySegment) {
currentMemorySegment->prevFreeSegment->nextFreeSegment = currentMemorySegment;
}
}

if (currentMemorySegment->nextSegment != nullptr) {
currentMemorySegment->nextSegment->prevSegment = currentMemorySegment;

if (currentMemorySegment->nextSegment->isFree) {
// Évite la fragmentation
// Avoids fragmentation
combineFreeSegments(currentMemorySegment, currentMemorySegment->nextSegment);
}
}

if (currentMemorySegment->prevSegment != nullptr) {
currentMemorySegment->prevSegment->nextSegment = currentMemorySegment;
if (currentMemorySegment->prevSegment->isFree) {
// Évite la fragmentation
// Avoids fragmentation
combineFreeSegments(currentMemorySegment, currentMemorySegment->prevSegment);
}
}
}

void* realloc(_Inout_ void* ptr, size_t size) {
void* newPtr = malloc(size);
memcpy<byte>(newPtr, ptr, size);
free(ptr);
return malloc(size);
return newPtr;
}

void* memcpy(_Out_ void* dest, _In_ const void* src, size_t length) {
Expand Down

0 comments on commit b20b815

Please sign in to comment.