Recent

Author Topic: TStringList.SetCapacity related  (Read 2778 times)

lagprogramming

  • Full Member
  • ***
  • Posts: 159
TStringList.SetCapacity related
« on: October 27, 2015, 08:19:25 pm »
TStringList.SetCapacity can be found inside stringl.inc.

Code: Pascal  [Select]
  1. Procedure TStringList.SetCapacity(NewCapacity: Integer);
  2. Var NewList : Pointer;
  3.     MSize : Longint;
  4. begin
  5.   If (NewCapacity<0) then
  6.      Error (SListCapacityError,NewCapacity);
  7.   If NewCapacity>FCapacity then
  8.     begin
  9.     GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  10.     If NewList=Nil then
  11.       Error (SListCapacityError,NewCapacity);
  12.     If Assigned(FList) then
  13.       begin
  14.       MSize:=FCapacity*Sizeof(TStringItem);
  15.       System.Move (FList^,NewList^,MSize);
  16.       FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*WordRatio, 0);//<--------------
  17.       FreeMem (Flist,MSize);
  18.       end;
  19. ...
  20.  

   I'd like the rationale to be checked. I think somebody tried to optimize the line
Code: Pascal  [Select]
  1. FillByte(Pchar(NewList)[MSize],(NewCapacity-FCapacity)*SizeOf(TStringItem),0);
with
Code: Pascal  [Select]
  1. FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*WordRatio, 0);

   The above lines don't fill with zeros the same size, the existing line fills only half the space difference. This is because classesh.inc has {$H+}, which makes SizeOf(TStringItem)=2*SizeOf(Pointer), while WordRatio = SizeOf(Pointer) Div SizeOf(Word).
   I don't think it's very important but, am I wrong?

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: TStringList.SetCapacity related
« Reply #1 on: October 28, 2015, 05:03:47 am »
   I'd like the rationale to be checked.

[...]

   The above lines don't fill with zeros the same size, the existing line fills only half the space difference.

[...] am I wrong?

No, you are right!

Edit:
Maybe FillDWord was meant to be used instead of FillWord?
« Last Edit: October 28, 2015, 05:07:37 am by engkin »

Bart

  • Hero Member
  • *****
  • Posts: 3518
    • Bart en Mariska's Webstek
Re: TStringList.SetCapacity related
« Reply #2 on: October 28, 2015, 12:20:04 pm »
File a bugreport, otherwise it probably won't get noticed.

Bart

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Re: TStringList.SetCapacity related
« Reply #3 on: October 28, 2015, 04:40:42 pm »
I know 16-bit is a valid target. For knowledge, will FillDWord work fine for users that want to target msdos i8086?
I agree with Bart, somebody might fill a Mantis bug entry. :)

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: TStringList.SetCapacity related
« Reply #4 on: October 28, 2015, 06:05:52 pm »
will FillDWord work fine for users that want to target msdos i8086?

It is in generic.inc file, which according to system.inc comment:
Quote
{ Include generic pascal only routines which are not defined in the processor
  specific include file }
{$I generic.inc}         

So it should work ok.

christian1987

  • New Member
  • *
  • Posts: 30
Re: TStringList.SetCapacity related
« Reply #5 on: October 29, 2015, 09:37:31 am »
I was trying to get this to crash... couldn't.

Here is why:
1132      Pointer(Flist^[Index].Fstring):=Nil;  // Needed to initialize.

TStringList.InsertItem sets it to nil AGAIN.

So I think maybe FillWord code is unneccessary? Not sure.