unit RAMalocatorUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm5 = class(TForm)
    Button1: TButton;
    ListBox2: TListBox;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form5 : TForm5;

implementation


{$R *.dfm}

 type  TvectList = array of array of single;

var vectList:TvectList;
    Heap:array[0..9] of TvectList;
    nc, dataSize,sliceNbr,breakReadingPoint : longWord;

 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;

 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:single;
 a,P,b:longWord;
 OK:boolean;
 a1,b1:integer;
 Begin
  a:=sliceNbr;
  b:=0; P:=a;   OK:=true; step:=0;
  a1:=a; b1:=b;
  form5.ListBox2.Items.Add('Data RAM allocating process');
  form5.ListBox2.Items.Add(equStr('T|F',3)+equStr('NR',3)+equStr('Bottom',13)+equStr('mid',13)+equStr('Up',13));
  application.ProcessMessages;
  form5.ListBox2.Items.Add(equStr(' - ',3)+ equStr(intToStr(step),3)+ equStr(intToStr(b),13)+ equStr(intToStr(P),13)+ equStr(intToStr(a),13));
  application.ProcessMessages;
  repeat
   inc(step);
    try
     setLength(vectList,0,0);
     setLength(vectList, P+1,nc+1);
     begin
      a1:=a; b1:=b;
      OK:=true;
      b:=P;
      P:=(a+b) div 2;
      form5.ListBox2.Items.Add(equStr('T:',3)+ equStr(intToStr(step),3)+ equStr(intToStr(b),13)+ equStr(intToStr(P),13)+ equStr(intToStr(a),13));
      application.ProcessMessages;
     end
    except
     begin
      if step=1 then error10(P);
      setLength(vectList,0,0);
      a:=P;
      P:=(a+b) div 2;
      OK:=false;
      form5.ListBox2.Items.Add(equStr('F:',3)+ equStr(intToStr(step),3)+ equStr(intToStr(b),13)+ equStr(intToStr(P),13)+ equStr(intToStr(a),13));
      application.ProcessMessages;
     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
   i:=0;
   //streamPointerArr[checkedStreamNbr].Checked:=true;   //tick checkbox pointing choosen stream (here first one in data records)
   //if StreamsNbrs[StreamIdx]=0 then vectorsSize:=d_count-1 else vectorsSize:=d_count div 2; //odblokowano 040619 aby pełny sygnał, a nie tylko połowę, zamiana id parametersSize na vectorsSize
   //form5.edit35.Text:=intToStr(vectorsSize+1);
   //if form5.CheckBox36.Checked then tBrP:=BreakReadingPoint else tBrP:=sliceNbr; //take into account braekPoint when RAM allocating
   Repeat
   //try
    (* if checkedStreamNbr=0 then  //gdy do kwantowania wybrano sygnał vaw
      begin
       nc:=d_count;
       form5.edit1.text:=intToStr(nc);
       dataSize:=(sliceNbr+1)*d_count*sizeOf(vectList[0,0,0]);
       breakReadingPoint:=approx(tBrP,d_count)-1;
       dataSize:=(breakReadingPoint+1)*d_count*sizeOf(vectList[0,0,0]);
      end
     else *)
      begin
       (*if nc>d_count div 2 then
        begin
         nc:=d_count div 2;
         form5.edit1.text:=intToStr(nc)
        end;  *)
       //form5.edit1.text:=intToStr(nc);
       sliceNbr:=4000000; nc:=128;
       dataSize:=(sliceNbr+1)*nc*sizeOf(vectList[0,0]);
       breakReadingPoint:=approx(heap[i],sliceNbr,i,nc)-1;
       dataSize:=(breakReadingPoint)*nc*sizeOf(vectList[0,0]);
       form5.ListBox2.Items.Add('It can  be read up to '+intToStr(breakReadingPoint)+' events.');
       form5.ListBox2.Items.Add('This data sample size ='+intToStr(dataSize)+' bytes,');
       form5.ListBox2.Items.Add('i.e. 2^'+floatToStr(ln(dataSize)/ln(2)));
       application.ProcessMessages;
      end;
    (* except
      s:='dataRAMallocator'#13#10'Too much events! We will try 2-times smaller sample, i.e. '+intToStr((sliceNbr div 2)+1)+' instead of '+intToStr(sliceNbr+1);
     // writeln(reportFile,s+#13#10'end.');
      showMessage(s);
      try
       setLength(vectList[i],(sliceNbr div 2)+1,nc+1);
        BreakReadingPoint:=sliceNbr div 2;
      except
       s:='dataRAMallocator'#13#10'Still too much events...';
       ShowMessage(s);
       //writeln(reportFile,s+#13#10'end.');
      end;
     end;*)
    if breakReadingPoint<sliceNbr then
     begin
      //form5.CheckBox36.Checked:=false;
      //form5.CheckBox36.Checked:=true;
      //form5.label35.Visible:=true;
      //form5.edit29.Text:=intToStr(breakReadingPoint);
      //s:='dataRAMallocator. Data reading (due to current change will be broken at '+form5.edit29.Text+'-th slices.';
      //form5.label35.Caption:=s;
      //writeln(reportFile,s+#13#10'End.');
      application.ProcessMessages;
     end;
    inc(i);
   Until i>10;
  End;{dataRAMallocator}



end.
