Changeset 387 for Common/UMemory.pas


Ignore:
Timestamp:
Jul 17, 2012, 10:25:56 PM (12 years ago)
Author:
chronos
Message:
  • Modified: URegistry unit moved from Common package to separated RegistryPkg.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/UMemory.pas

    r290 r387  
    99
    1010type
    11 
    1211  { TMemory }
    1312
     
    1514  private
    1615    FData: PByte;
    17     FSize: Integer;
    1816    function GetItem(Index: Integer): Byte;
    1917    procedure SetItem(Index: Integer; AValue: Byte);
     18    function GetSize: Integer; virtual;
    2019    procedure SetSize(AValue: Integer); virtual;
    2120  public
    22     procedure Clear(Value: Byte = 0);
     21    procedure Fill(Value: Byte = 0);
    2322    procedure Assign(Source: TMemory);
    2423    constructor Create;
    2524    destructor Destroy; override;
    2625    property Data: PByte read FData;
    27     property Size: Integer read FSize write SetSize;
     26    property Size: Integer read GetSize write SetSize;
    2827    property Items[Index: Integer]: Byte read GetItem write SetItem; default;
    2928  end;
     
    4241  end;
    4342
     43  { TMemoryRec }
     44
     45  TMemoryRec = record
     46  private
     47    FData: PByte;
     48    function GetItem(Index: Integer): Byte; inline;
     49    procedure SetItem(Index: Integer; AValue: Byte); inline;
     50    function GetSize: Integer; inline;
     51    procedure SetSize(AValue: Integer); inline;
     52  public
     53    procedure Fill(Value: Byte = 0); inline;
     54    procedure Assign(Source: TMemoryRec); inline;
     55    property Data: PByte read FData;
     56    property Size: Integer read GetSize write SetSize;
     57    property Items[Index: Integer]: Byte read GetItem write SetItem; default;
     58  end;
     59
     60  { TBitMemoryRec }
     61
     62  TBitMemoryRec = record
     63  private
     64    FMemory: TMemoryRec;
     65    FSize: Cardinal;
     66    function GetItem(Index: Cardinal): Boolean; inline;
     67    function GetSize: Cardinal; inline;
     68    procedure SetItem(Index: Cardinal; AValue: Boolean); inline;
     69    procedure SetSize(AValue: Cardinal); inline;
     70  public
     71    procedure WriteItems(Addr: Cardinal; Items: TBitMemoryRec);
     72    procedure Fill(Value: Boolean = False);
     73    procedure Assign(Source: TBitMemoryRec); inline;
     74    property Memory: TMemoryRec read FMemory;
     75    property Size: Cardinal read GetSize write SetSize;
     76    property Items[Index: Cardinal]: Boolean read GetItem write SetItem; default;
     77  end;
     78
     79
    4480implementation
    4581
     82{ TBitMemoryRec }
     83
     84function TBitMemoryRec.GetItem(Index: Cardinal): Boolean;
     85begin
     86  Result := Boolean((FMemory[Index shr 3] shr (Index and 7)) and 1);
     87end;
     88
     89function TBitMemoryRec.GetSize: Cardinal;
     90begin
     91  Result := FSize;
     92end;
     93
     94procedure TBitMemoryRec.SetItem(Index: Cardinal; AValue: Boolean);
     95begin
     96  FMemory[Index shr 3] := (FMemory[Index shr 3] and ($ff xor (1 shl (Index and 7)))) or
     97    (Byte(AValue) shl (Index and 7));
     98end;
     99
     100procedure TBitMemoryRec.SetSize(AValue: Cardinal);
     101begin
     102  FSize := AValue;
     103  FMemory.Size := (AValue - 1) shr 3 + 1;
     104end;
     105
     106procedure TBitMemoryRec.WriteItems(Addr: Cardinal; Items: TBitMemoryRec);
     107begin
     108
     109end;
     110
     111procedure TBitMemoryRec.Fill(Value: Boolean);
     112begin
     113  FMemory.Fill($ff * Byte(Value));
     114end;
     115
     116procedure TBitMemoryRec.Assign(Source: TBitMemoryRec);
     117begin
     118  Size := Source.Size;
     119  FMemory.Assign(Source.Memory);
     120end;
     121
     122{ TMemoryRec }
     123
     124function TMemoryRec.GetItem(Index: Integer): Byte;
     125begin
     126  Result := PByte(FData + Index)^;
     127end;
     128
     129procedure TMemoryRec.SetItem(Index: Integer; AValue: Byte);
     130begin
     131  PByte(FData + Index)^ := AValue;
     132end;
     133
     134function TMemoryRec.GetSize: Integer;
     135begin
     136  Result := MemSize(FData);
     137end;
     138
     139procedure TMemoryRec.SetSize(AValue: Integer);
     140begin
     141  FData := ReAllocMem(FData, AValue);
     142end;
     143
     144procedure TMemoryRec.Fill(Value: Byte);
     145begin
     146  FillChar(FData^, Size, Value);
     147end;
     148
     149procedure TMemoryRec.Assign(Source: TMemoryRec);
     150begin
     151  Size := Source.Size;
     152  Move(Source.Data^, FData^, Size);
     153end;
     154
    46155{ TPositionMemory }
    47156
     
    49158begin
    50159  inherited SetSize(AValue);
    51   if FPosition > FSize then FPosition := FSize;
     160  if FPosition > Size then FPosition := Size;
    52161end;
    53162
     
    70179procedure TMemory.SetSize(AValue: Integer);
    71180begin
    72   if FSize = AValue then Exit;
    73   FSize := AValue;
    74   FData := ReAllocMem(FData, FSize);
     181  FData := ReAllocMem(FData, AValue);
    75182end;
    76183
     
    85192end;
    86193
    87 procedure TMemory.Clear(Value: Byte);
     194function TMemory.GetSize: Integer;
     195begin
     196  Result := MemSize(FData);
     197end;
     198
     199procedure TMemory.Fill(Value: Byte);
    88200begin
    89201  FillChar(FData^, Size, Value);
     
    99211begin
    100212  FData := nil;
    101   FSize := 0;
     213  Size := 0;
    102214end;
    103215
Note: See TracChangeset for help on using the changeset viewer.