-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUMSTREAM.PAS
200 lines (178 loc) · 5.03 KB
/
UMSTREAM.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
{
UMStream Unit
A stream that stores data using conventional memory
2022 LRT
}
unit
umstream;
interface
uses
uexc, uclasses, types, locale, uobject, ustream, math;
const
C_BLOCK_SIZE = 60000; { maximum size of each memory block }
C_BLOCK_COUNT = 9; { maximum number of blocks }
type
TMemoryBlocks = array[0 .. C_BLOCK_COUNT - 1] of pointer;
{
using the default values of 9 blocks of 60000 bytes each, that
means TMemoryStream can hold up to 540KB
}
const
C_MAX_POSSIBLE_SIZE = C_BLOCK_COUNT * C_BLOCK_SIZE;
type
PMemoryStream = ^TMemoryStream;
TMemoryStream = object (TStream)
public
constructor initWithSize(memorySize: longint);
constructor initWithBuffer(buffer: pointer; bufferSize: word);
destructor done; virtual;
function read(buffer: pointer; count: word): word; virtual;
procedure write(buffer: pointer; count: word); virtual;
procedure seek(pos: longint); virtual;
function getPosition: longint; virtual;
function isEOF: boolean; virtual;
function getSize: longint; virtual;
function getMaximumSize: longint;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_blocks: TMemoryBlocks;
_memorySize: longint;
_position: longint;
_maxPosition: longint;
_mustFreeMemoryBlocks: boolean;
end;
implementation
{ TMemoryStream }
constructor TMemoryStream.initWithSize(memorySize: longint);
var
remaining: longint;
block: byte;
begin
inherited init;
iassert(memorySize <= C_MAX_POSSIBLE_SIZE, @self, 0, S_ERR_NOT_ENOUGH_MEMORY);
FillChar(_blocks, SizeOf(_blocks), 0);
_memorySize := memorySize;
_position := 0;
_maxPosition := 0;
block := 0;
remaining := _memorySize;
while remaining >= C_BLOCK_SIZE do
begin
GetMem(_blocks[block], C_BLOCK_SIZE);
inc(block);
dec(remaining, C_BLOCK_SIZE);
end;
if remaining > 0 then GetMem(_blocks[block], remaining);
_mustFreeMemoryBlocks := true;
end;
constructor TMemoryStream.initWithBuffer(buffer: pointer; bufferSize: word);
begin
inherited init;
_blocks[0] := buffer;
_memorySize := bufferSize;
_position := 0;
_maxPosition := bufferSize;
_mustFreeMemoryBlocks := false;
end;
destructor TMemoryStream.done;
var
remaining: longint;
block: byte;
begin
if _mustFreeMemoryBlocks then
begin
block := 0;
remaining := _memorySize;
while remaining >= C_BLOCK_SIZE do
begin
FreeMem(_blocks[block], C_BLOCK_SIZE);
inc(block);
dec(remaining, C_BLOCK_SIZE);
end;
if remaining > 0 then FreeMem(_blocks[block], remaining);
end;
inherited done;
end;
function TMemoryStream.read(buffer: pointer; count: word): word;
var
remaining: longint;
block: byte;
c, offset: word;
p: pointer;
begin
if isEOF then
begin
read := 0;
exit;
end;
if (_memorySize - _position) < count then count := _memorySize - _position;
read := count;
block := _position div C_BLOCK_SIZE;
offset := _position mod C_BLOCK_SIZE;
c := minword(count, C_BLOCK_SIZE - offset);
p := incptr(_blocks[block], offset);
Move(p^, buffer^, c);
remaining := count - c;
if remaining > 0 then
begin
buffer := incptr(buffer, c);
Move(_blocks[block+1]^, buffer^, remaining);
end;
inc(_position, count);
end;
procedure TMemoryStream.write(buffer: pointer; count: word);
var
block: byte;
offset: word;
c: word;
remainder: word;
p: pointer;
begin
if not (_memorySize - _position >= count) then exit;
block := _position div C_BLOCK_SIZE;
offset := _position mod C_BLOCK_SIZE;
c := minword(C_BLOCK_SIZE - offset, count);
p := incptr(_blocks[block], offset);
Move(buffer^, p^, c);
if c < count then
begin
remainder := count - c;
p := _blocks[block+1];
buffer := incptr(buffer, c);
Move(buffer^, p^, remainder);
end;
inc(_position, count);
_maxPosition := maxlong(_maxPosition, _position);
end;
procedure TMemoryStream.seek(pos: longint);
begin
iassert(pos < _memorySize, @self, 0, S_ERR_INVALID_BOUNDS);
_position := pos;
end;
function TMemoryStream.getClassName: string;
begin
getClassName := 'TMemoryStream';
end;
function TMemoryStream.getClassId: word;
begin
getClassId := C_CLASS_ID_MemoryStream;
end;
function TMemoryStream.getPosition: longint;
begin
getPosition := _position;
end;
function TMemoryStream.isEOF: boolean;
begin
isEOF := _position = _memorySize;
end;
function TMemoryStream.getSize: longint;
begin
getSize := _maxPosition;
end;
function TMemoryStream.getMaximumSize: longint;
begin
getMaximumSize := _memorySize;
end;
{ Other }
end.