Skip to content

Commit 36ad4ea

Browse files
committed
Addresses #28
1 parent 58757b2 commit 36ad4ea

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

src/PasVulkan.Framework.pas

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6578,19 +6578,74 @@ destructor TpvVulkanAllocationManager.Destroy;
65786578
end;
65796579

65806580
function TpvVulkanAllocationManager.AllocationCallback(const Size:TVkSize;const Alignment:TVkSize;const Scope:TVkSystemAllocationScope):PVkVoid;
6581+
var Original,Aligned:pointer;
6582+
Mask:TpvPtrUInt;
6583+
Align,RealSize:TpvSizeUInt;
65816584
begin
6582-
GetMem(result,Size);
6585+
if Alignment<1 then begin
6586+
Align:=1;
6587+
end else begin
6588+
Align:=Alignment;
6589+
end;
6590+
if (Align and (Align-1))<>0 then begin
6591+
Align:=RoundUpToPowerOfTwoSizeUInt(Align);
6592+
end;
6593+
Mask:=Align-1;
6594+
RealSize:=Size+(Align shl 1)+SizeOf(Pointer)+(SizeOf(TpvSizeUInt)*2);
6595+
GetMem(Original,RealSize);
6596+
FillChar(Original^,RealSize,#0);
6597+
Aligned:=Pointer(TpvPtrUInt(TpvPtrUInt(Original)+SizeOf(Pointer)+(SizeOf(TpvSizeUInt)*2)));
6598+
if (Align>1) and ((TpvPtrUInt(Aligned) and Mask)<>0) then begin
6599+
inc(TpvPtrUInt(Aligned),TpvPtrUInt(TpvPtrUInt(Align)-(TpvPtrUInt(Aligned) and Mask)));
6600+
end;
6601+
Pointer(Pointer(TpvPtrUInt(TpvPtrUInt(Aligned)-SizeOf(Pointer)))^):=Original;
6602+
TpvSizeUInt(Pointer(TpvPtrUInt(TpvPtrUInt(Aligned)-(SizeOf(Pointer)+SizeOf(TpvSizeUInt))))^):=Size;
6603+
TpvSizeUInt(Pointer(TpvPtrUInt(TpvPtrUInt(Aligned)-(SizeOf(Pointer)+(SizeOf(TpvSizeUInt)*2))))^):=Align;
6604+
result:=Aligned;
65836605
end;
65846606

65856607
function TpvVulkanAllocationManager.ReallocationCallback(const Original:PVkVoid;const Size:TVkSize;const Alignment:TVkSize;const Scope:TVkSystemAllocationScope):PVkVoid;
6608+
var pp:pointer;
6609+
OldSize,OldAlign,Align:TpvSizeUInt;
65866610
begin
6587-
result:=Original;
6588-
ReallocMem(result,Size);
6611+
if assigned(Original) then begin
6612+
if Alignment<1 then begin
6613+
Align:=1;
6614+
end else begin
6615+
Align:=Alignment;
6616+
end;
6617+
if (Align and (Align-1))<>0 then begin
6618+
Align:=RoundUpToPowerOfTwoSizeUInt(Align);
6619+
end;
6620+
OldSize:=TpvSizeUInt(Pointer(TpvPtrUInt(TpvPtrUInt(Original)-(SizeOf(Pointer)+SizeOf(TpvSizeUInt))))^);
6621+
OldAlign:=TpvSizeUInt(Pointer(TpvPtrUInt(TpvPtrUInt(Original)-(SizeOf(Pointer)+(SizeOf(TpvSizeUInt)*2))))^);
6622+
if ((Align=OldAlign) or ((OldAlign and (Align-1))=0)) and (Size<=OldSize) then begin
6623+
TpvSizeUInt(Pointer(TpvPtrUInt(TpvPtrUInt(Original)-(SizeOf(Pointer)+SizeOf(TpvSizeUInt))))^):=Size;
6624+
TpvSizeUInt(Pointer(TpvPtrUInt(TpvPtrUInt(Original)-(SizeOf(Pointer)+(SizeOf(TpvSizeUInt)*2))))^):=Align;
6625+
result:=Original;
6626+
end else begin
6627+
result:=AllocationCallback(Size,Alignment,Scope);
6628+
if Size<OldSize then begin
6629+
Move(Original^,result^,Size);
6630+
end else begin
6631+
Move(Original^,result^,OldSize);
6632+
end;
6633+
pp:=Pointer(Pointer(TpvPtrUInt(TpvPtrUInt(Original)-SizeOf(Pointer)))^);
6634+
FreeMem(pp);
6635+
end;
6636+
end else begin
6637+
result:=AllocationCallback(Size,Alignment,Scope);
6638+
end;
65896639
end;
65906640

65916641
procedure TpvVulkanAllocationManager.FreeCallback(const Memory:PVkVoid);
6642+
var pp:pointer;
65926643
begin
6593-
FreeMem(Memory);
6644+
pp:=Memory;
6645+
if assigned(pp) then begin
6646+
pp:=Pointer(Pointer(TpvPtrUInt(TpvPtrUInt(pp)-SizeOf(Pointer)))^);
6647+
FreeMem(pp);
6648+
end;
65946649
end;
65956650

65966651
procedure TpvVulkanAllocationManager.InternalAllocationCallback(const Size:TVkSize;const Type_:TVkInternalAllocationType;const Scope:TVkSystemAllocationScope);

0 commit comments

Comments
 (0)