source: trunk/Packages/Common/Memory.pas

Last change on this file was 75, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File size: 2.6 KB
Line 
1unit Memory;
2
3interface
4
5uses
6 Classes, SysUtils;
7
8type
9
10 { TMemory }
11
12 TMemory = class
13 private
14 FData: PByte;
15 FSize: Integer;
16 function GetItem(Index: Integer): Byte;
17 procedure SetItem(Index: Integer; AValue: Byte);
18 procedure SetSize(AValue: Integer); virtual;
19 public
20 procedure Clear(Value: Byte = 0);
21 procedure Assign(Source: TMemory);
22 constructor Create;
23 destructor Destroy; override;
24 procedure WriteMemory(Position: Integer; Memory: TMemory);
25 procedure ReadMemory(Position: Integer; Memory: TMemory);
26 property Data: PByte read FData;
27 property Size: Integer read FSize write SetSize;
28 property Items[Index: Integer]: Byte read GetItem write SetItem; default;
29 end;
30
31 { TPositionMemory }
32
33 TPositionMemory = class(TMemory)
34 private
35 FPosition: Integer;
36 protected
37 procedure SetSize(AValue: Integer); override;
38 public
39 procedure WriteByte(Value: Byte);
40 function ReadByte: Byte;
41 property Position: Integer read FPosition write FPosition;
42 end;
43
44
45implementation
46
47{ TPositionMemory }
48
49procedure TPositionMemory.SetSize(AValue: Integer);
50begin
51 inherited;
52 if FPosition > FSize then FPosition := FSize;
53end;
54
55procedure TPositionMemory.WriteByte(Value: Byte);
56begin
57 if FPosition >= Size then Size := FPosition + 1;
58 Items[FPosition] := Value;
59 Inc(FPosition);
60end;
61
62function TPositionMemory.ReadByte: Byte;
63begin
64 if FPosition >= Size then Size := FPosition + 1;
65 Result := Items[FPosition];
66 Inc(FPosition);
67end;
68
69{ TMemory }
70
71procedure TMemory.SetSize(AValue: Integer);
72begin
73 if FSize = AValue then Exit;
74 FSize := AValue;
75 FData := ReAllocMem(FData, FSize);
76end;
77
78function TMemory.GetItem(Index: Integer): Byte;
79begin
80 Result := PByte(FData + Index)^;
81end;
82
83procedure TMemory.SetItem(Index: Integer; AValue: Byte);
84begin
85 PByte(FData + Index)^ := AValue;
86end;
87
88procedure TMemory.Clear(Value: Byte);
89begin
90 FillChar(FData^, Size, Value);
91end;
92
93procedure TMemory.Assign(Source: TMemory);
94begin
95 Size := Source.Size;
96 Move(Source.Data^, FData^, Size);
97end;
98
99constructor TMemory.Create;
100begin
101 FData := nil;
102 FSize := 0;
103end;
104
105destructor TMemory.Destroy;
106begin
107 Size := 0;
108 inherited;
109end;
110
111procedure TMemory.WriteMemory(Position: Integer; Memory: TMemory);
112begin
113 Move(Memory.FData, PByte(PByte(@FData) + Position)^, Memory.Size);
114end;
115
116procedure TMemory.ReadMemory(Position: Integer; Memory: TMemory);
117begin
118 Move(PByte(PByte(@FData) + Position)^, Memory.FData, Memory.Size);
119end;
120
121end.
Note: See TracBrowser for help on using the repository browser.