Skip to content

Commit

Permalink
Merge pull request #2101 from CosmosOS/fix-interruptMemoryManager
Browse files Browse the repository at this point in the history
Disable interrupt when touching memory
  • Loading branch information
quajak authored Feb 9, 2022
2 parents 7640672 + 3fdb336 commit d1ac2c2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions source/Cosmos.Core/Memory/Heap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,25 @@ public static unsafe void Init()
/// <returns>Byte pointer to the start of the block.</returns>
public static byte* Alloc(uint aSize)
{
CPU.DisableInterrupts();

if (aSize <= HeapSmall.mMaxItemSize)
{
return HeapSmall.Alloc((ushort)aSize);
byte* ptr = HeapSmall.Alloc((ushort)aSize);
CPU.EnableInterrupts();
return ptr;
}
else if (aSize <= HeapMedium.MaxItemSize)
{
return HeapMedium.Alloc(aSize);
byte* ptr = HeapMedium.Alloc(aSize);
CPU.EnableInterrupts();
return ptr;
}
else
{
return HeapLarge.Alloc(aSize);
byte* ptr = HeapLarge.Alloc(aSize);
CPU.EnableInterrupts();
return ptr;
}
}

Expand Down Expand Up @@ -103,6 +111,9 @@ public static void Free(void* aPtr)
/// <returns>Number of objects freed</returns>
public static int Collect()
{
//Disable interrupts: Prevent CPU exception when allocation is called from interrupt code
CPU.DisableInterrupts();

// Mark and sweep objects from roots
// 1. Check if a page is in use if medium/large mark and sweep object
// 2. Go throught the SMT table for small objects and go through pages by size
Expand Down Expand Up @@ -222,6 +233,9 @@ public static int Collect()

rootSMTPtr = rootSMTPtr->LargerSize;
}

//Enable interrupts back
CPU.EnableInterrupts();

return freed;
}
Expand Down

0 comments on commit d1ac2c2

Please sign in to comment.