1 | unit Memory;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
45 | implementation
|
---|
46 |
|
---|
47 | { TPositionMemory }
|
---|
48 |
|
---|
49 | procedure TPositionMemory.SetSize(AValue: Integer);
|
---|
50 | begin
|
---|
51 | inherited;
|
---|
52 | if FPosition > FSize then FPosition := FSize;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | procedure TPositionMemory.WriteByte(Value: Byte);
|
---|
56 | begin
|
---|
57 | if FPosition >= Size then Size := FPosition + 1;
|
---|
58 | Items[FPosition] := Value;
|
---|
59 | Inc(FPosition);
|
---|
60 | end;
|
---|
61 |
|
---|
62 | function TPositionMemory.ReadByte: Byte;
|
---|
63 | begin
|
---|
64 | if FPosition >= Size then Size := FPosition + 1;
|
---|
65 | Result := Items[FPosition];
|
---|
66 | Inc(FPosition);
|
---|
67 | end;
|
---|
68 |
|
---|
69 | { TMemory }
|
---|
70 |
|
---|
71 | procedure TMemory.SetSize(AValue: Integer);
|
---|
72 | begin
|
---|
73 | if FSize = AValue then Exit;
|
---|
74 | FSize := AValue;
|
---|
75 | FData := ReAllocMem(FData, FSize);
|
---|
76 | end;
|
---|
77 |
|
---|
78 | function TMemory.GetItem(Index: Integer): Byte;
|
---|
79 | begin
|
---|
80 | Result := PByte(FData + Index)^;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | procedure TMemory.SetItem(Index: Integer; AValue: Byte);
|
---|
84 | begin
|
---|
85 | PByte(FData + Index)^ := AValue;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TMemory.Clear(Value: Byte);
|
---|
89 | begin
|
---|
90 | FillChar(FData^, Size, Value);
|
---|
91 | end;
|
---|
92 |
|
---|
93 | procedure TMemory.Assign(Source: TMemory);
|
---|
94 | begin
|
---|
95 | Size := Source.Size;
|
---|
96 | Move(Source.Data^, FData^, Size);
|
---|
97 | end;
|
---|
98 |
|
---|
99 | constructor TMemory.Create;
|
---|
100 | begin
|
---|
101 | FData := nil;
|
---|
102 | FSize := 0;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | destructor TMemory.Destroy;
|
---|
106 | begin
|
---|
107 | Size := 0;
|
---|
108 | inherited;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TMemory.WriteMemory(Position: Integer; Memory: TMemory);
|
---|
112 | begin
|
---|
113 | Move(Memory.FData, PByte(PByte(@FData) + Position)^, Memory.Size);
|
---|
114 | end;
|
---|
115 |
|
---|
116 | procedure TMemory.ReadMemory(Position: Integer; Memory: TMemory);
|
---|
117 | begin
|
---|
118 | Move(PByte(PByte(@FData) + Position)^, Memory.FData, Memory.Size);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | end.
|
---|