This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcontainer.memorybuffer.pas
More file actions
241 lines (201 loc) · 6.65 KB
/
Copy pathcontainer.memorybuffer.pas
File metadata and controls
241 lines (201 loc) · 6.65 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(******************************************************************************)
(* libPasC-Algorithms *)
(* delphi and object pascal library of common data structures and algorithms *)
(* *)
(* *)
(* Copyright (c) 2020 Ivan Semenkov *)
(* https://github.com/isemenkov/libpasc-algorithms ivan@semenkov.pro *)
(* Ukraine *)
(******************************************************************************)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"), *)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included in *)
(* all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(******************************************************************************)
unit container.memorybuffer;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
{$IFOPT D+}
{$DEFINE DEBUG}
{$ENDIF}
interface
uses
SysUtils;
type
{ A TMemoryBuffer is a useful data structure for storing arbitrary sized
blocks of memory. It is guarantees deletion of the memory block when the
object is destroyed.
This class based on wxWidgets wxMemoryBuffer api interface
https://docs.wxwidgets.org/trunk/classwx_memory_buffer.html }
PMemoryBuffer = ^TMemoryBuffer;
TMemoryBuffer = class
public
constructor Create (ASize : Int64 = 1024);
destructor Destroy; override;
{ Append a single byte to the buffer. }
procedure AppendByte (AData : Byte);
{ Append a data block to the buffer. }
procedure AppendData (const AData; ASize : Int64);
{ Clear the buffer contents. }
procedure Clear;
{ Ensure that the buffer is big enough and return a pointer to the start of
the empty space in the buffer.
This pointer can be used to directly write data into the buffer, this new
data will be appended to the existing data. }
function GetAppendBuffer (ASizeNeeded : Int64) : Pointer;
{ Sets the length of the data stored in the buffer.
Mainly useful for truncating existing data. }
procedure SetBufferDataSize (ASizeUsed : Int64);
{ Ensure the buffer is big enough and return a pointer to the buffer which
can be used to directly write into the buffer up to sizeNeeded bytes. }
function GetWriteBuffer (ASizeNeeded : Int64) : Pointer;
{ Ensures the buffer has at least size bytes available. }
procedure SetBufferAllocSize (ASize : Int64);
{ Returns the size of the buffer. }
function GetBufferAllocSize : Int64;
{ Returns the length of the valid data in the buffer. }
function GetBufferDataSize : Int64;
{ Return a pointer to the data in the buffer. }
function GetBufferData : Pointer;
{ Returns true if the buffer contains no data. }
function IsEmpty : Boolean;
protected
{ Reallocate the array to the new size }
function Enlarge : Boolean;
protected
FData : array of Byte;
FLength : Int64;
FAlloced : Int64;
end;
implementation
constructor TMemoryBuffer.Create (ASize : Int64);
begin
if ASize = 0 then
begin
ASize := 1024;
end;
SetLength(FData, ASize);
FAlloced := ASize;
FLength := 0;
end;
destructor TMemoryBuffer.Destroy;
begin
SetLength(FData, 0);
inherited Destroy;
end;
function TMemoryBuffer.Enlarge : Boolean;
var
NewSize : Int64;
begin
{ Double the allocated size }
NewSize := FAlloced * 2;
{ Reallocate the array to the new size }
SetLength(FData, NewSize);
FAlloced := NewSize;
Result := True;
end;
procedure TMemoryBuffer.AppendByte (AData : Byte);
begin
if FLength + 1 > FAlloced then
begin
if not Enlarge then
begin
Exit;
end;
end;
FData[FLength] := AData;
Inc(FLength);
end;
procedure TMemoryBuffer.AppendData (const AData; ASize : Int64);
begin
while FLength + ASize > FAlloced do
begin
if not Enlarge then
begin
Exit;
end;
end;
Move(AData, FData[FLength], ASize);
Inc(FLength, ASize);
end;
procedure TMemoryBuffer.Clear;
begin
FLength := 0;
end;
function TMemoryBuffer.GetAppendBuffer (ASizeNeeded : Int64) : Pointer;
begin
while FLength + ASizeNeeded > FAlloced do
begin
if not Enlarge then
begin
Exit(nil);
end;
end;
Result := @FData[FLength];
end;
procedure TMemoryBuffer.SetBufferDataSize (ASizeUsed : Int64);
begin
FLength := ASizeUsed;
end;
function TMemoryBuffer.GetWriteBuffer (ASizeNeeded : Int64) : Pointer;
begin
while ASizeNeeded > FAlloced do
begin
if not Enlarge then
begin
Exit(nil);
end;
end;
Result := @FData[0];
end;
procedure TMemoryBuffer.SetBufferAllocSize (ASize : Int64);
begin
if ASize < FAlloced then
begin
SetLength(FData, ASize);
FAlloced := ASize;
FLength := ASize;
end else
begin
while ASize < FAlloced do
begin
if not Enlarge then
begin
Exit;
end;
end;
end;
end;
function TMemoryBuffer.GetBufferAllocSize : Int64;
begin
Result := FAlloced;
end;
function TMemoryBuffer.GetBufferDataSize : Int64;
begin
Result := FLength;
end;
function TMemoryBuffer.GetBufferData : Pointer;
begin
Result := @FData[0];
end;
function TMemoryBuffer.IsEmpty : Boolean;
begin
Result := (FLength = 0);
end;
end.