-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fe6612
commit 7fa3ca9
Showing
4 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
// Std imports | ||
import "std/data/vector"; | ||
import "std/type/error"; | ||
import "std/os/system"; | ||
|
||
// Own imports | ||
import "../util/memory"; | ||
|
||
type Base dyn; | ||
|
||
public type BlockAllocator<Base> struct { | ||
const MemoryManager& memoryManager | ||
IMemoryManager* memoryManager | ||
Vector<byte*> memoryBlocks | ||
Vector<Base*> allocatedObjects | ||
unsigned int blockSize | ||
unsigned int offsetInBlock = 0 | ||
} | ||
|
||
public p BlockAllocator.ctor(IMemoryManager* memoryManager, unsigned int blockSize = 0) { | ||
this.memoryManager = memoryManager; | ||
this.blockSize = blockSize == 0 ? getPageSize() : blockSize; | ||
// Allocate the first block | ||
this.allocateNewBlock(); | ||
} | ||
|
||
public p BlockAllocator.dtor() { | ||
// Destruct all objects | ||
foreach Base* ptr : this.allocatedObjects { | ||
sDelete(ptr); | ||
} | ||
this.allocatedObjects.clear(); | ||
|
||
// Free all memory blocks | ||
foreach byte* block : this.memoryBlocks { | ||
this.memoryManager.deallocate(block); | ||
} | ||
this.memoryBlocks.clear(); | ||
} | ||
|
||
public p BlockAllocator.allocateNewBlock() { | ||
// Allocate new block | ||
byte* ptr = this.memoryManager.allocate(this.blockSize); | ||
if ptr != nil<byte*> { | ||
String msg = "Could not allocate memory block for BlockAllocator. Already allocated " + this.memoryBlocks.size() + " blocks."; | ||
panic(Error(msg)); | ||
} | ||
|
||
// Store pointer and reset offset | ||
this.memoryBlocks.push(ptr); | ||
this.offsetInBlock = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters