FGL: Expand() must consider MaxListSize

TFPSList.Expand don't consider global const MaxListSize

const
  MaxListSize = Maxint div 16;

so it can expand Capacity to bigger value. Bad.

function TFPSList.Expand: TFPSList;
var
  IncSize : Longint;

begin
  if FCount < FCapacity then exit;
  IncSize := 4;
  if FCapacity > 3 then IncSize := IncSize + 4;
  if FCapacity > 8 then IncSize := IncSize + 8;
  if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);
  SetCapacity(FCapacity + IncSize);
  Result := Self;
end; 

I suggest the following change

function TFPSList.Expand: TFPSList;
var
  IncSize : Longint;
  NewVal: Int64;
begin
  if FCount < FCapacity then exit;
  IncSize := 4;
  if FCapacity > 3 then IncSize := IncSize + 4;
  if FCapacity > 8 then IncSize := IncSize + 8;
  if FCapacity > 127 then Inc(IncSize, FCapacity shr 2);

  if FCapacity >= MaxListSize then
    Error(SListCapacityError, FCapacity);

  NewVal := Int64(FCapacity) + IncSize;
  if NewVal > MaxListSize then
    NewVal := MaxListSize;

  SetCapacity(NewVal);
  Result := Self;
end;

It is done in my local copy and works ok.

Edited by Alexey Torgashin