refactoring

This commit is contained in:
Michael Schimmel
2025-05-24 16:08:11 +02:00
parent 46f7b594d9
commit 46264ea041
2 changed files with 245 additions and 86 deletions
+21 -9
View File
@@ -148,7 +148,7 @@ procedure TSList.Free;
var
tempSelf: Pointer;
begin
tempSelf := Pointer(@Self);
tempSelf := Pointer(@Self); // @Self is valid here as Free is an instance method of a record
FreeMemAligned(tempSelf);
end;
@@ -178,25 +178,32 @@ end;
class operator TMycAtomicStack<T>.Initialize(out Dest: TMycAtomicStack<T>);
begin
FSList := TSList.Create;
// Use Dest to access fields of the record instance being initialized
Dest.FSList := TSList.Create;
end;
class operator TMycAtomicStack<T>.Finalize(var Dest: TMycAtomicStack<T>);
var
item: PItem;
begin
item := PopPtr;
// Use Dest to access fields and call instance methods
// on the record instance being finalized
item := Dest.PopPtr; // Call instance method PopPtr via Dest
while item <> nil do
begin
item.Data := Default(T);
FreeMemAligned(item);
item := PopPtr;
FreeMemAligned(item); // Global procedure
item := Dest.PopPtr; // Call instance method PopPtr via Dest
end;
if Dest.FSList <> nil then // Check if FSList was initialized
begin
Dest.FSList.Free; // Call instance method Free on FSList via Dest
end;
FSList.Free;
end;
function TMycAtomicStack<T>.Alloc: PItem;
begin
// This is an instance method, FSList is accessed directly (implicitly Self.FSList)
GetMemAligned(Pointer(Result), SizeOf(TItem));
FillChar(Result^, SizeOf(TItem), 0);
Result.Data := Default(T);
@@ -204,6 +211,7 @@ end;
function TMycAtomicStack<T>.Pop: T;
begin
// Instance method
if not TryPop(Result) then
begin
Result := Default(T);
@@ -212,6 +220,7 @@ end;
function TMycAtomicStack<T>.PopPtr: PItem;
begin
// Instance method, FSList is Self.FSList
Result := PItem(FSList.Pop);
end;
@@ -219,13 +228,15 @@ procedure TMycAtomicStack<T>.Push(const Data: T);
var
item: PItem;
begin
item := Alloc;
// Instance method
item := Alloc; // Calls Self.Alloc
item.Data := Data;
PushPtr(item);
PushPtr(item); // Calls Self.PushPtr
end;
procedure TMycAtomicStack<T>.PushPtr(Item: PItem);
begin
// Instance method, FSList is Self.FSList
FSList.Push(PSListEntry(Item));
end;
@@ -233,7 +244,8 @@ function TMycAtomicStack<T>.TryPop(out Item: T): Boolean;
var
currentPItem: PItem;
begin
currentPItem := PopPtr;
// Instance method
currentPItem := PopPtr; // Calls Self.PopPtr
Result := currentPItem <> nil;
if Result then
begin