Skip to content

Commit d1ac2c2

Browse files
authored
Merge pull request #2101 from CosmosOS/fix-interruptMemoryManager
Disable interrupt when touching memory
2 parents 7640672 + 3fdb336 commit d1ac2c2

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

source/Cosmos.Core/Memory/Heap.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,25 @@ public static unsafe void Init()
3939
/// <returns>Byte pointer to the start of the block.</returns>
4040
public static byte* Alloc(uint aSize)
4141
{
42+
CPU.DisableInterrupts();
43+
4244
if (aSize <= HeapSmall.mMaxItemSize)
4345
{
44-
return HeapSmall.Alloc((ushort)aSize);
46+
byte* ptr = HeapSmall.Alloc((ushort)aSize);
47+
CPU.EnableInterrupts();
48+
return ptr;
4549
}
4650
else if (aSize <= HeapMedium.MaxItemSize)
4751
{
48-
return HeapMedium.Alloc(aSize);
52+
byte* ptr = HeapMedium.Alloc(aSize);
53+
CPU.EnableInterrupts();
54+
return ptr;
4955
}
5056
else
5157
{
52-
return HeapLarge.Alloc(aSize);
58+
byte* ptr = HeapLarge.Alloc(aSize);
59+
CPU.EnableInterrupts();
60+
return ptr;
5361
}
5462
}
5563

@@ -103,6 +111,9 @@ public static void Free(void* aPtr)
103111
/// <returns>Number of objects freed</returns>
104112
public static int Collect()
105113
{
114+
//Disable interrupts: Prevent CPU exception when allocation is called from interrupt code
115+
CPU.DisableInterrupts();
116+
106117
// Mark and sweep objects from roots
107118
// 1. Check if a page is in use if medium/large mark and sweep object
108119
// 2. Go throught the SMT table for small objects and go through pages by size
@@ -222,6 +233,9 @@ public static int Collect()
222233

223234
rootSMTPtr = rootSMTPtr->LargerSize;
224235
}
236+
237+
//Enable interrupts back
238+
CPU.EnableInterrupts();
225239

226240
return freed;
227241
}

0 commit comments

Comments
 (0)