unit Unit1;

interface
uses System.SysUtils,Vcl.Forms,Vcl.Dialogs;
var sliceNbr : longWord;  nc, nbrOfLayers:byte;
procedure dataRAMallocator;

implementation
uses unit2;
 type  TvectList = array of array of single;

var vectList:TvectList;
    Heap:array[0..9] of TvectList;
    dataSize,NbrOfEvents : longWord;
    reportFile : textFile;

 procedure error10(P:longWord);
 var s:string;
 Begin
  s:= 'Error10'#13#10'The "Out of memory" error occured, because a bite of the size of '+intToStr(((P+1)*nc*sizeOf(vectList[0,0])))+
       ' bytes (for registering of '+intToStr(P)+' events) is too big.'#13#10+
       'Now we will try to find the bigest reachable (due to this 32 bytes software) bite in an iteration process.'+
       #13#10'It can at least give you an estimate how to prepare adequate data sample';
  showMessage(s);
  writeln(reportFile,s+#13#10'end.');
 End;

  procedure ReportFileOpen;
  var  reportFileDir, reportDate : shortString;
   Begin
    reportFileDir:=ExtractFileDir(application.ExeName)+'\HeapReport.txt';
    reportDate:=dateToStr(now)+' '+timeToStr(now);
    assignFile(reportFile,reportFileDir);
    Rewrite(reportFile);
    Writeln(reportFile,'Report:     ',reportFileDir);
    Writeln(reportFile,'ReportDate: ',reportDate);
    form2.label1.caption:='Find results in "'+reportFileDir+'"';
    flush(reportFile);
   End;{ReportFileOpen}


 function equStr(str:string; l:word):string;
  begin
   while length(str)<l do
   begin
    str:=' '+str;
   end;
    result:=str;
   end;

function approx(vectList:TvectList; sliceNbr:longWord; i, nc:word):int64;
//not overcome a hidden treshold = reachable RAM cluster
 var step : word; S:string;
 a,P,b:longWord;
 OK:boolean;
 a1,b1:integer;

 procedure ListCaption;
  Begin
   s:='...................................................';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s); flush(reportFile);
   s:='Data RAM allocating process';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s); flush(reportFile);
   application.ProcessMessages;
   s:='Lay = Heap Layer, Itr = Iterations number';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s);  flush(reportFile);
   application.ProcessMessages;
   s:='T|F = True, False;';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s);  flush(reportFile);
   application.ProcessMessages;
   s:= 'T - for operations in the "try" part,';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s);  flush(reportFile);
   application.ProcessMessages;
   s:= 'F- for operations in the "except" part';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s);  flush(reportFile);
   application.ProcessMessages;
   s:='Bottom - UP = encirclement, mid = center';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s);  flush(reportFile);
   application.ProcessMessages;
   s:='___________________________________________________';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s); flush(reportFile);
   s:=equStr('Lay',3)+equStr('Itr',4)+equStr('T|F',4)+equStr('Bottom',13)+equStr('mid',13)+equStr('Up',13);
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s);  flush(reportFile);
   s:='___________________________________________________';
   form2.ListBox2.Items.Add(s);
   Writeln(reportFile,s); flush(reportFile);
   application.ProcessMessages;
  End;

 procedure Adline(TF:shortString);
  Begin
    s:=equStr(intToStr(i),3)+ equStr(intToStr(step),4)+equStr(TF,4)+ equStr(intToStr(b),13)+ equStr(intToStr(P),13)+ equStr(intToStr(a),13);
    form2.ListBox2.Items.Add(s);
    Writeln(reportFile,s);  flush(reportFile);
    application.ProcessMessages;
  End;{Adline}

 Begin
  a:=sliceNbr;
  b:=0; P:=a;   OK:=true; step:=0;
  a1:=a; b1:=b;
  ListCaption;
  Adline(' - ');
  repeat
   inc(step);
    try
     setLength(vectList,0,0);
     setLength(vectList, P,nc);
     begin
      a1:=a; b1:=b;
      OK:=true;
      b:=P;
      P:=(a+b) div 2;
      AdLine('T:');
     end
    except
     begin
      //if step=1 then error10(P);
      setLength(vectList,0,0);
      a:=P;
      P:=(a+b) div 2;
      OK:=false;
      AdLine('F:');
     end;
    end;{except}
  until OK and ((b1=b) or (b=P) or (step=1));
  result:=P;
 End;{approx}

procedure dataRAMallocator;
  var  s:string;     i:word;
  Begin
   form2.label5.Visible:=false;
   ReportFileOpen;
   form2.Label2.caption:='UP='+intToStr(sliceNbr)+'; Click';
   form2.Label3.caption:='Vect='+intToStr(nc)+'; Click';
   form2.label4.Caption:='Layers='+intToStr(nbrOfLayers)+'; Click';
   i:=0;
   Repeat
       dataSize:=(sliceNbr)*nc*sizeOf(vectList[0,0]);
       NbrOfEvents:=approx(heap[i],sliceNbr,i,nc);
       dataSize:=(NbrOfEvents)*nc*sizeOf(vectList[0,0]);
       s:='==================================================';
       form2.ListBox2.Items.Add(s);
       Writeln(reportFile,s); flush(reportFile);
       application.ProcessMessages;
       s:='It can be read up to '+intToStr(NbrOfEvents)+' events.';
       form2.ListBox2.Items.Add(s);
       Writeln(reportFile,s); flush(reportFile);
       s:='This data sample size ='+intToStr(dataSize)+' bytes,';
       form2.ListBox2.Items.Add(s);
       Writeln(reportFile,s); flush(reportFile);
       s:='i.e. 2^'+floatToStr(ln(dataSize)/ln(2));
       form2.ListBox2.Items.Add(s);
       Writeln(reportFile,s); flush(reportFile);
       application.ProcessMessages;
    inc(i);
   Until i>=nbrOfLayers;
   closeFile(reportFile);
   form2.label5.Visible:=true;
   form2.button2.SetFocus;
   beep;
  End;{dataRAMallocator}

end.

