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