Fix block size calculation
This commit is contained in:
parent
8e0b742748
commit
254b881da6
@ -28,7 +28,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the new allocated memory.
|
* Returns: Pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(size_t size) shared nothrow @nogc;
|
void[] allocate(in size_t size) shared nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deallocates a memory block.
|
* Deallocates a memory block.
|
||||||
@ -49,7 +49,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the allocated memory.
|
* Returns: Pointer to the allocated memory.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, size_t size) shared nothrow @nogc;
|
bool reallocate(ref void[] p, in size_t size) shared nothrow @nogc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,7 +58,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the new allocated memory.
|
* Returns: Pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(size_t size) shared nothrow @nogc
|
void[] allocate(in size_t size) shared nothrow @nogc
|
||||||
{
|
{
|
||||||
if (!size)
|
if (!size)
|
||||||
{
|
{
|
||||||
@ -90,11 +90,11 @@ final class MmapPool : Allocator
|
|||||||
* into two blocks if the block is too large.
|
* into two blocks if the block is too large.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* size = Minimum size the block should have.
|
* size = Minimum size the block should have (aligned).
|
||||||
*
|
*
|
||||||
* Returns: Data the block points to or $(D_KEYWORD null).
|
* Returns: Data the block points to or $(D_KEYWORD null).
|
||||||
*/
|
*/
|
||||||
private void* findBlock(size_t size) shared nothrow @nogc
|
private void* findBlock(in ref size_t size) shared nothrow @nogc
|
||||||
{
|
{
|
||||||
Block block1;
|
Block block1;
|
||||||
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
||||||
@ -122,11 +122,15 @@ final class MmapPool : Allocator
|
|||||||
block2.size = block1.size - blockEntrySize - size;
|
block2.size = block1.size - blockEntrySize - size;
|
||||||
block2.region = block1.region;
|
block2.region = block1.region;
|
||||||
|
|
||||||
|
if (block1.next !is null)
|
||||||
|
{
|
||||||
|
block1.next.prev = block2;
|
||||||
|
}
|
||||||
block1.next = block2;
|
block1.next = block2;
|
||||||
block1.size = size;
|
block1.size = size;
|
||||||
}
|
}
|
||||||
block1.free = false;
|
block1.free = false;
|
||||||
atomicOp!"+="(block1.region.blocks, 1);
|
block1.region.blocks = block1.region.blocks + 1;
|
||||||
|
|
||||||
return cast(void*) block1 + blockEntrySize;
|
return cast(void*) block1 + blockEntrySize;
|
||||||
}
|
}
|
||||||
@ -170,34 +174,31 @@ final class MmapPool : Allocator
|
|||||||
return VirtualFree(cast(void*) block.region, 0, MEM_RELEASE) == 0;
|
return VirtualFree(cast(void*) block.region, 0, MEM_RELEASE) == 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Merge blocks if neigbours are free.
|
||||||
|
if (block.next !is null && block.next.free)
|
||||||
|
{
|
||||||
|
block.size = block.size + blockEntrySize + block.next.size;
|
||||||
|
if (block.next.next !is null)
|
||||||
|
{
|
||||||
|
block.next.next.prev = block;
|
||||||
|
}
|
||||||
|
block.next = block.next.next;
|
||||||
|
}
|
||||||
|
if (block.prev !is null && block.prev.free)
|
||||||
|
{
|
||||||
|
block.prev.size = block.prev.size + blockEntrySize + block.size;
|
||||||
|
if (block.next !is null)
|
||||||
|
{
|
||||||
|
block.next.prev = block.prev;
|
||||||
|
}
|
||||||
|
block.prev.next = block.next;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Merge blocks if neigbours are free.
|
block.free = true;
|
||||||
if (block.next !is null && block.next.free)
|
|
||||||
{
|
|
||||||
block.size = blockEntrySize + block.next.size;
|
|
||||||
if (block.next.next !is null)
|
|
||||||
{
|
|
||||||
block.next.next.prev = block;
|
|
||||||
}
|
|
||||||
block.next = block.next.next;
|
|
||||||
}
|
|
||||||
if (block.prev !is null && block.prev.free)
|
|
||||||
{
|
|
||||||
block.size = blockEntrySize + block.size;
|
|
||||||
if (block.next !is null)
|
|
||||||
{
|
|
||||||
block.next.prev = block.prev;
|
|
||||||
}
|
|
||||||
block.prev.next = block.next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
block.free = true;
|
|
||||||
}
|
|
||||||
atomicOp!"-="(block.region.blocks, 1);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
block.region.blocks = block.region.blocks - 1;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -217,7 +218,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the reallocation was successful.
|
* Returns: Whether the reallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, size_t size) shared nothrow @nogc
|
bool reallocate(ref void[] p, in size_t size) shared nothrow @nogc
|
||||||
{
|
{
|
||||||
void[] reallocP;
|
void[] reallocP;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user