{$MODE FPC}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
unit dpooledlist;

interface

uses
  dpool;

type
TPooledListDefaultStaticInfo = TPoolDefaultStaticInfo;

generic TPooledLists<TValue, TStaticInfo> = object
public type
  PListItem = ^TListItem;
  TListItem = packed record
    Value: TValue;
    Next: PListItem;
  end;
private
  FPool: specialize TPool<TListItem, TStaticInfo>;
public
  procedure Init;
  procedure Done;
  procedure AllocList(out List: PListItem);
      // List := nil
  procedure DeallocList(var List: PListItem);
      // Deallocates all items in the list and List := nil.
  procedure AddToList(var List: PListItem; const Value: TValue);
      // Adds the specified Value to the specified List. Value becomes first
      // item in the list.
end;

implementation

procedure TPooledLists.Init;
begin
  FPool.Init;
end;

procedure TPooledLists.Done;
begin
  FPool.Done;
end;

procedure TPooledLists.AllocList(out List: PListItem);
begin
  List := nil;
end;

procedure TPooledLists.DeallocList(var List: PListItem);
var
  Item, Next: PListItem;
begin
  Item := List;
  while Item <> nil do begin
    Next := Item^.Next;
    FPool.Dealloc(Item);
    Item := Next;
  end;
  List := nil;
end;

procedure TPooledLists.AddToList(var List: PListItem; const Value: TValue);
var
  Item: PListItem;
begin
  Item := FPool.Alloc;
  Item^.Value := Value;
  Item^.Next := List;
  List := Item;
end;

end.
