Ograniczenia funkcji TMask

0

Używam w swoim programie funkcji TMask, do porównania masek filtrów z wynikami.

Ku mojemu zdziwieniu przy zdefiniowaniu więcej niż ~50 masek filtrów, funkcja TMask zwiększa zajętą pamięć o ~64kB/sekundę do rozmiarów rzędu 22MB po porównaniu na dysku ~15.000 plików.

Doszedłem w końcu do wniosku, że winna jest właśnie funkcja TMask.

Jestem na etapie stwierdzenia, że winą może być też błędna maska (używam wszystkich możliwych znaków dostępnych dla TMask: ?, *, [, ], -, !).

Funkcja która odpowiada za porównanie czy plik jest zgodny z listą masek:

function IsFileInclude(FileName: String): Boolean;
var
 i: SmallInt;
 VerifMask: TMask;
 MustInclude: Boolean;
begin
 i := 0;
 MustInclude := False;

 while (not MustInclude) and (i < IncludeFilters.Count) do
   begin
    VerifMask   := TMask.Create(IncludeFilters.Strings[i]);
    MustInclude := VerifMask.Matches(FileName);
    Inc(i);

    VerifMask.Free;
   end;

 Result := MustInclude;
end;

Cały program sprawdzony na obecność wycieków pamięci EurekaLog 6.0.21.

Gdzie leży problem ze zwiększaniem pamięci ?
Dodam, iż jeśli program zminimalizuję i przywrócę okno, pamięć programu maleje do ~5MB, po czym dalej wzrasta średnio 64kB na sekundę.

0

Rozwiązanie: Author: Aaron Murgatroyd
http://www.swissdelphicenter.ch/torry/showcode.php?id=2245

// The Masks unit has a bug when specifying
// masks that start with with a set, this bug
// even occurrs is a * is first and then a set.

// This is due to a small memory freeing issue
// If you step through the masks unit you
// will find it. Here is a replacement unit for
// the Masks unit which has the fixes in it.

{ *************************************************************************** }
{                                                                             }
{ Kylix and Delphi Cross-Platform Visual Component Library                    }
{                                                                             }
{ Copyright (c) 1995, 2001 Borland Software Corporation                       }
{                                                                             }
{ *************************************************************************** }


unit Masks2;

interface

uses SysUtils;

type
  EMaskException = class(Exception);

  TMask = class
  private
    FMask: Pointer;
    FSize: Integer;
  public
    constructor Create(const MaskValue: string);
    destructor Destroy; override;
    function Matches(const FileName: string): Boolean;
  end;

function MatchesMask(const FileName, Mask: string): Boolean;

implementation

uses RTLConsts;


const
  MaxCards = 10000;

type
  PMaskSet    = ^TMaskSet;
  TMaskSet    = set of Char;
  TMaskStates = (msLiteral, msAny, msSet, msMBCSLiteral);
  TMaskState  = record
    SkipTo: Boolean;
    case State: TMaskStates of
      msLiteral: (Literal: Char);
      msAny: ();
      msSet: (Negate: Boolean;
        CharSet: PMaskSet);
      msMBCSLiteral: (LeadByte, TrailByte: Char);
  end;
  PMaskStateArray = ^TMaskStateArray;
  TMaskStateArray = array[0..128] of TMaskState;

function InitMaskStates(const Mask: string;
  var MaskStates: array of TMaskState; bDontAllocate: Boolean = False): Integer;
var
  I: Integer;
  SkipTo: Boolean;
  Literal: Char;
  LeadByte, TrailByte: Char;
  P: PChar;
  Negate: Boolean;
  CharSet: TMaskSet;
  Cards: Integer;

  procedure InvalidMask;
  begin
    raise EMaskException.CreateResFmt(@SInvalidMask, [Mask,
      P - PChar(Mask) + 1]);
  end;

  procedure Reset;
  begin
    SkipTo  := False;
    Negate  := False;
    CharSet := [];
  end;

  procedure WriteScan(MaskState: TMaskStates);
  begin
    if I <= High(MaskStates) then
    begin
      if SkipTo then
      begin
        Inc(Cards);
        if Cards > MaxCards then InvalidMask;
      end;
      MaskStates[I].SkipTo := SkipTo;
      MaskStates[I].State  := MaskState;
      case MaskState of
        msLiteral: MaskStates[I].Literal := UpCase(Literal);
        msSet:
          begin
            MaskStates[I].Negate := Negate;
            if not bDontAllocate then
            begin
              New(MaskStates[I].CharSet);
              MaskStates[I].CharSet^ := CharSet;
            end 
            else
              MaskStates[I].CharSet := nil;
          end;
        msMBCSLiteral:
          begin
            MaskStates[I].LeadByte  := LeadByte;
            MaskStates[I].TrailByte := TrailByte;
          end;
      end;
    end;
    Inc(I);
    Reset;
  end;

  procedure ScanSet;
  var
    LastChar: Char;
    C: Char;
  begin
    Inc(P);
    if P^ = '!' then
    begin
      Negate := True;
      Inc(P);
    end;
    LastChar := #0;
    while not (P^ in [#0, ']']) do
    begin
      // MBCS characters not supported in msSet!
      if P^ in LeadBytes then
        Inc(P)
      else
        case P^ of
          '-':
            if LastChar = #0 then InvalidMask
            else
              begin
                Inc(P);
                for C := LastChar to UpCase(P^) do Include(CharSet, C);
              end;
            else
              LastChar := UpCase(P^);
              Include(CharSet, LastChar);
        end;
      Inc(P);
    end;
    if (P^ <> ']') or (CharSet = []) then InvalidMask;
    WriteScan(msSet);
  end;
begin
  P     := PChar(Mask);
  I     := 0;
  Cards := 0;
  Reset;
  while P^ <> #0 do
  begin
    case P^ of
      '*': SkipTo := True;
      '?': if not SkipTo then WriteScan(msAny);
      '[': ScanSet;
      else
        if P^ in LeadBytes then
        begin
          LeadByte := P^;
          Inc(P);
          TrailByte := P^;
          WriteScan(msMBCSLiteral);
        end
        else
          begin
            Literal := P^;
            WriteScan(msLiteral);
          end;
    end;
    Inc(P);
  end;
  Literal := #0;
  WriteScan(msLiteral);
  Result := I;
end;

function MatchesMaskStates(const FileName: string;
  const MaskStates: array of TMaskState): Boolean;
type
  TStackRec = record
    sP: PChar;
    sI: Integer;
  end;
var
  T: Integer;
  S: array[0..MaxCards - 1] of TStackRec;
  I: Integer;
  P: PChar;

  procedure Push(P: PChar; I: Integer);
  begin
    with S[T] do
    begin
      sP := P;
      sI := I;
    end;
    Inc(T);
  end;

  function Pop(var P: PChar; var I: Integer): Boolean;
  begin
    if T = 0 then
      Result := False
    else
    begin
      Dec(T);
      with S[T] do
      begin
        P := sP;
        I := sI;
      end;
      Result := True;
    end;
  end;

  function Matches(P: PChar; Start: Integer): Boolean;
  var
    I: Integer;
  begin
    Result := False;
    for I := Start to High(MaskStates) do
      with MaskStates[I] do
      begin
        if SkipTo then
        begin
          case State of
            msLiteral:
              while (P^ <> #0) and (UpperCase(P^) <> Literal) do Inc(P);
            msSet:
              while (P^ <> #0) and not (Negate xor (UpCase(P^) in CharSet^)) do Inc(P);
            msMBCSLiteral:
              while (P^ <> #0) do
              begin
                if (P^ <> LeadByte) then Inc(P, 2)
                else
                begin
                  Inc(P);
                  if (P^ = TrailByte) then Break;
                  Inc(P);
                end;
              end;
          end;
          if P^ <> #0 then Push(@P[1], I);
        end;
        case State of
          msLiteral: if UpperCase(P^) <> Literal then Exit;
          msSet: if not (Negate xor (UpCase(P^) in CharSet^)) then Exit;
          msMBCSLiteral:
            begin
              if P^ <> LeadByte then Exit;
              Inc(P);
              if P^ <> TrailByte then Exit;
            end;
        end;
        Inc(P);
      end;
    Result := True;
  end;
begin
  Result := True;
  T      := 0;
  P      := PChar(FileName);
  I      := Low(MaskStates);
  repeat
    if Matches(P, I) then Exit;
  until not Pop(P, I);
  Result := False;
end;

procedure DoneMaskStates(var MaskStates: array of TMaskState);
var
  I: Integer;
begin
  for I := Low(MaskStates) to High(MaskStates) do
    if MaskStates[I].State = msSet then Dispose(MaskStates[I].CharSet);
end;

{ TMask }

constructor TMask.Create(const MaskValue: string);
var
  A: array[0..0] of TMaskState;
begin
  FSize := InitMaskStates(MaskValue, A, True);
  FMask := AllocMem(FSize * SizeOf(TMaskState));
  InitMaskStates(MaskValue, Slice(PMaskStateArray(FMask)^, FSize));
end;

destructor TMask.Destroy;
begin
  if FMask <> nil then
  begin
    DoneMaskStates(Slice(PMaskStateArray(FMask)^, FSize));
    FreeMem(FMask, FSize * SizeOf(TMaskState));
  end;
end;

function TMask.Matches(const FileName: string): Boolean;
begin
  Result := MatchesMaskStates(FileName, Slice(PMaskStateArray(FMask)^, FSize));
end;

function MatchesMask(const FileName, Mask: string): Boolean;
var
  CMask: TMask;
begin
  CMask := TMask.Create(Mask);
  try
    Result := CMask.Matches(FileName);
  finally
    CMask.Free;
  end;
end;

end.

1 użytkowników online, w tym zalogowanych: 0, gości: 1