1 | unit UMemory;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TMemory }
|
---|
13 |
|
---|
14 | TMemory = class
|
---|
15 | private
|
---|
16 | FData: Pointer;
|
---|
17 | FOwner: Boolean;
|
---|
18 | FSize: Integer;
|
---|
19 | procedure SetData(AValue: Pointer);
|
---|
20 | procedure SetSize(AValue: Integer);
|
---|
21 | public
|
---|
22 | property Owner: Boolean read FOwner write FOwner;
|
---|
23 | property Data: Pointer read FData write SetData;
|
---|
24 | property Size: Integer read FSize write SetSize;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | { TMemoryStream }
|
---|
28 |
|
---|
29 | TMemoryPos = class(TMemory)
|
---|
30 | Position: Pointer;
|
---|
31 | function ReadByte: Byte;
|
---|
32 | function ReadWord: Word;
|
---|
33 | procedure WriteByte(Value: Byte);
|
---|
34 | procedure WriteWord(Value: Word);
|
---|
35 | end;
|
---|
36 |
|
---|
37 | implementation
|
---|
38 |
|
---|
39 | { TMemoryStream }
|
---|
40 |
|
---|
41 | function TMemoryPos.ReadByte: Byte;
|
---|
42 | begin
|
---|
43 | Result := PByte(Position)^;
|
---|
44 | Inc(Position, SizeOf(Byte));
|
---|
45 | end;
|
---|
46 |
|
---|
47 | function TMemoryPos.ReadWord: Word;
|
---|
48 | begin
|
---|
49 | Result := PWord(Position)^;
|
---|
50 | Inc(Position, SizeOf(Word));
|
---|
51 | end;
|
---|
52 |
|
---|
53 | procedure TMemoryPos.WriteByte(Value: Byte);
|
---|
54 | begin
|
---|
55 | PByte(Position)^ := Value;
|
---|
56 | Inc(Position, SizeOf(Byte));
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TMemoryPos.WriteWord(Value: Word);
|
---|
60 | begin
|
---|
61 | PWord(Position)^ := Value;
|
---|
62 | Inc(Position, SizeOf(Word));
|
---|
63 | end;
|
---|
64 |
|
---|
65 | { TMemory }
|
---|
66 |
|
---|
67 | procedure TMemory.SetSize(AValue: Integer);
|
---|
68 | begin
|
---|
69 | if FSize = AValue then Exit;
|
---|
70 | FSize := AValue;
|
---|
71 | FData := ReAllocMem(FData, FSize);
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TMemory.SetData(AValue: Pointer);
|
---|
75 | begin
|
---|
76 | if FData = AValue then Exit;
|
---|
77 | if FOwner then FreeMem(FData);
|
---|
78 | FData := AValue;
|
---|
79 | if Assigned(FData) then FSize := MemSize(FData)
|
---|
80 | else FSize := 0;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | end.
|
---|
84 |
|
---|