View Issue Details

IDProjectCategoryView StatusLast Update
0037523FPCRTLpublic2020-08-08 23:57
ReporterCudaText man Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityN/A
Status assignedResolutionopen 
PlatformUnix 
Product Version3.2.0 
Summary0037523: Optimize sysutils DiskSize DiskFree AddDisk
DescriptionOptimized sysutils's DiskFree+DiskSize
- use "case", more fast than "if..in..or"
- don't create string StrPas() in fpStatfs()-- "p" is already null-terminated
- DRY code
AddDisk: Changed int consts 4,26 to Low(..), High(..).
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

CudaText man

2020-08-08 21:36

reporter  

dsk.pas (1,544 bytes)   
{$mode objfpc}{$H+}

uses
  SysUtils, Unix, UnixType;

//dont take these
Const
  FixDriveStr : array[0..3] of pchar=(
    '.',
    '/fd0/.',
    '/fd1/.',
    '/.'
    );
var
  Drives   : byte = 4;
  DriveStr : array[4..26] of pchar;

//take this- only 3 funcs below are changed!!
Function DiskFree(Drive: Byte): int64;
var
  p : PChar;
  fs : TStatfs;
Begin
  case Drive of
    Low(FixDriveStr)..High(FixDriveStr):
      p := FixDriveStr[Drive];
    Low(DriveStr)..High(DriveStr):
      p := DriveStr[Drive];
    else
      p := nil;
  end;
  if (p<>nil) and (fpStatFS(p, @fs)<>-1) then
    DiskFree := int64(fs.bavail)*int64(fs.bsize)
  else
    DiskFree := -1;
End;

Function DiskSize(Drive: Byte): int64;
var
  p : PChar;
  fs : TStatfs;
Begin
  case Drive of
    Low(FixDriveStr)..High(FixDriveStr):
      p := FixDriveStr[Drive];
    Low(DriveStr)..High(DriveStr):
      p := DriveStr[Drive];
    else
      p := nil;
  end;
  if (p<>nil) and (fpStatFS(p, @fs)<>-1) then
    DiskSize := int64(fs.blocks)*int64(fs.bsize)
  else
    DiskSize := -1;
End;

Function AddDisk(const path: string): Byte;
begin
  if DriveStr[Drives]<>nil then
   FreeMem(DriveStr[Drives]);
  GetMem(DriveStr[Drives],length(Path)+1);
  StrPCopy(DriveStr[Drives],path);
  Result:=Drives;
  inc(Drives);
  if Drives>High(DriveStr) then
   Drives:=Low(DriveStr);
end;

var
  b: byte;
begin
  b:= AddDisk('/home/user/.');
  writeln('free: '+inttostr(DiskFree(b) div 1024 div 1024 div 1024));
  writeln('total: '+inttostr(DiskSize(b) div 1024 div 1024 div 1024));
end.
dsk.pas (1,544 bytes)   

Issue History

Date Modified Username Field Change
2020-08-08 21:36 CudaText man New Issue
2020-08-08 21:36 CudaText man File Added: dsk.pas
2020-08-08 23:57 Michael Van Canneyt Assigned To => Michael Van Canneyt
2020-08-08 23:57 Michael Van Canneyt Status new => assigned