Debian Codegraph @

plugins/wcx/zip/src/fparchive/abdfbase.pas

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is TurboPower Abbrevia
15 *
16 * The Initial Developer of the Original Code is
17 * TurboPower Software
18 *
19 * Portions created by the Initial Developer are Copyright (C) 1997-2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * ***** END LICENSE BLOCK ***** *)
25
26 {*********************************************************}
27 {* ABBREVIA: AbDfBase.pas *}
28 {*********************************************************}
29 {* Deflate base unit *}
30 {*********************************************************}
31
32 unit AbDfBase;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 SysUtils,
40 Classes;
41
42 type
43 PAbDfLongintList = ^TAbDfLongintList;
44 TAbDfLongintList =
45 array [0..pred(MaxInt div sizeof(longint))] of longint;
46
47 const
48 dfc_CodeLenCodeLength = 7;
49 dfc_LitDistCodeLength = 15;
50 dfc_MaxCodeLength = 15;
51
52 const
53 dfc_MaxMatchLen = 258; {lengths are 3..258 for deflate}
54 dfc_MaxMatchLen64 = 64 * 1024; {lengths are 3..65536 for deflate64}
55
56 const
57 dfc_LitExtraOffset = 257;
58 dfc_LitExtraBits : array [0..30] of byte =
59 (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
60 4, 4, 4, 4, 5, 5, 5, 5, 16, 99, 99);
61 { note: the last two are required to avoid going beyond the end}
62 { of the array when generating static trees}
63
64 dfc_DistExtraOffset = 0;
65 dfc_DistExtraBits : array [0..31] of byte =
66 (0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
67 10, 10, 11, 11, 12, 12, 13, 13, 14, 14);
68 { note: the last two are only use for deflate64}
69
70 dfc_LengthBase : array [0..28] of word =
71 (3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43,
72 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 3);
73 { note: the final 3 is correct for deflate64; for symbol 285,}
74 { lengths are stored as (length - 3)}
75 { for deflate it's very wrong, but there's special code in}
76 { the (de)compression code to cater for this}
77
78 dfc_DistanceBase : array [0..31] of word =
79 (1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257,
80 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289,
81 16385, 24577, 32769, 49153);
82
83 dfc_CodeLengthIndex : array [0..18] of byte =
84 (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
85
86 const
87 dfc_CanUseStored = $01;
88 dfc_CanUseStatic = $02;
89 dfc_CanUseDynamic = $04;
90 dfc_UseLazyMatch = $08;
91 dfc_UseDeflate64 = $10;
92 dfc_UseAdler32 = $20;
93 dfc_CanUseHuffman = dfc_CanUseStatic or dfc_CanUseDynamic;
94 dfc_TestOnly = $40000000;
95
96 type
97 TAbProgressStep = procedure (aPercentDone : integer) of object;
98 {-progress metering of deflate/inflate; abort with AbortProgress}
99
100 TAbDeflateHelper = class
101 private
102 FAmpleLength : longint;
103 FChainLength : longint;
104 FLogFile : string;
105 FMaxLazy : longint;
106 FOnProgressStep : TAbProgressStep;
107 FOptions : longint;
108 FPartSize : Int64;
109 FSizeCompressed : Int64;
110 FSizeNormal : Int64;
111 FStreamSize : Int64;
112 FWindowSize : longint;
113 FZipOption : AnsiChar;
114 protected
115 procedure dhSetAmpleLength(aValue : longint);
116 procedure dhSetChainLength(aValue : longint);
117 procedure dhSetLogFile(const aValue : string);
118 procedure dhSetMaxLazy(aValue : longint);
119 procedure dhSetOnProgressStep(aValue : TAbProgressStep);
120 procedure dhSetOptions(aValue : longint);
121 procedure dhSetWindowSize(aValue : longint);
122 procedure dhSetZipOption(aValue : AnsiChar);
123 public
124 constructor Create;
125
126 procedure Assign(aHelper : TAbDeflateHelper);
127
128 property AmpleLength : longint
129 read FAmpleLength write dhSetAmpleLength;
130 property ChainLength : longint
131 read FChainLength write dhSetChainLength;
132 property LogFile : string
133 read FLogFile write dhSetLogFile;
134 property MaxLazyLength : longint
135 read FMaxLazy write dhSetMaxLazy;
136 property Options : longint
137 read FOptions write dhSetOptions;
138 property PartialSize : Int64
139 read FPartSize write FPartSize;
140 property PKZipOption : AnsiChar
141 read FZipOption write dhSetZipOption;
142 property StreamSize : Int64
143 read FStreamSize write FStreamSize;
144 property WindowSize : longint
145 read FWindowSize write dhSetWindowSize;
146
147 property CompressedSize : Int64
148 read FSizeCompressed write FSizeCompressed;
149 property NormalSize : Int64
150 read FSizeNormal write FSizeNormal;
151
152 property OnProgressStep : TAbProgressStep
153 read FOnProgressStep write dhSetOnProgressStep;
154 end;
155
156 type
157 TAbLineDelimiter = (ldCRLF, ldLF);
158
159 TAbLogger = class(TStream)
160 private
161 FBuffer : PAnsiChar;
162 FCurPos : PAnsiChar;
163 FLineDelim : TAbLineDelimiter;
164 FStream : TFileStream;
165 protected
166 function logWriteBuffer : boolean;
167 public
168 constructor Create(const aLogName : string);
169 destructor Destroy; override;
170
171 function Read(var Buffer; Count : longint) : longint; override;
172 function Seek(const Offset : Int64; Origin : TSeekOrigin) : Int64; override;
173 function Write(const Buffer; Count : longint) : longint; override;
174 procedure WriteLine(const S : string);
175 procedure WriteStr(const S : string);
176
177 property LineDelimiter : TAbLineDelimiter
178 read FLineDelim write FLineDelim;
179 end;
180
181 type
182 TAbNodeManager = class
183 private
184 FFreeList : pointer;
185 FNodeSize : cardinal;
186 FNodesPerPage : cardinal;
187 FPageHead : pointer;
188 FPageSize : cardinal;
189 protected
190 function nmAllocNewPage : pointer;
191 public
192 constructor Create(aNodeSize : cardinal);
193 destructor Destroy; override;
194
195 function AllocNode : pointer;
196 function AllocNodeClear : pointer;
197 procedure FreeNode(aNode : pointer);
198 end;
199
200 {---exception classes---}
201 type
202 EAbAbortProgress = class(Exception);
203 EAbPartSizedInflate = class(Exception);
204 EAbInflatePasswordError = class(Exception);
205 EAbInternalInflateError = class(Exception);
206 EAbInflateError = class(Exception)
207 public
208 constructor Create(const aMsg : string);
209 constructor CreateUnknown(const aMsg : string;
210 const aErrorMsg : string);
211 end;
212 EAbInternalDeflateError = class(Exception);
213 EAbDeflateError = class(Exception)
214 public
215 constructor Create(const aMsg : string);
216 constructor CreateUnknown(const aMsg : string;
217 const aErrorMsg : string);
218 end;
219
220 {---aborting a process---}
221 procedure AbortProgress;
222
223 {---calculation of checksums---}
224 procedure AbUpdateAdlerBuffer(var aAdler : longint;
225 var aBuffer; aCount : integer);
226 procedure AbUpdateCRCBuffer(var aCRC : longint;
227 var aBuffer; aCount : integer);
228
229
230 implementation
231
232 uses
233 AbUtils;
234
235 {===TAbDeflateHelper=================================================}
236 constructor TAbDeflateHelper.Create;
237 begin
238 inherited Create;
239 FAmpleLength := 8;
240 FChainLength := 32;
241 {FLogFile := '';}
242 FMaxLazy := 16;
243 {FOnProgressStep := nil;}
244 FOptions := $F;
245 {FStreamSize := 0;}
246 FWindowSize := 32 * 1024;
247 FZipOption := 'n';
248 end;
249 {--------}
250 procedure TAbDeflateHelper.Assign(aHelper : TAbDeflateHelper);
251 begin
252 FAmpleLength := aHelper.FAmpleLength;
253 FChainLength := aHelper.FChainLength;
254 FLogFile := aHelper.FLogFile;
255 FMaxLazy := aHelper.FMaxLazy;
256 FOnProgressStep := aHelper.FOnProgressStep;
257 FOptions := aHelper.FOptions;
258 FPartSize := aHelper.FPartSize;
259 FStreamSize := aHelper.FStreamSize;
260 FWindowSize := aHelper.FWindowSize;
261 FZipOption := aHelper.FZipOption;
262 end;
263 {--------}
264 procedure TAbDeflateHelper.dhSetAmpleLength(aValue : longint);
265 begin
266 if (aValue <> AmpleLength) then begin
267 if (aValue <> -1) and (aValue < 4) then
268 aValue := 4;
269 FAmpleLength := aValue;
270 FZipOption := '?';
271 end;
272 end;
273 {--------}
274 procedure TAbDeflateHelper.dhSetChainLength(aValue : longint);
275 begin
276 if (aValue <> ChainLength) then begin
277 if (aValue <> -1) and (aValue < 4) then
278 aValue := 4;
279 FChainLength := aValue;
280 FZipOption := '?';
281 end;
282 end;
283 {--------}
284 procedure TAbDeflateHelper.dhSetLogFile(const aValue : string);
285 begin
286 FLogFile := aValue;
287 end;
288 {--------}
289 procedure TAbDeflateHelper.dhSetMaxLazy(aValue : longint);
290 begin
291 if (aValue <> MaxLazyLength) then begin
292 if (aValue <> -1) and (aValue < 4) then
293 aValue := 4;
294 FMaxLazy := aValue;
295 FZipOption := '?';
296 end;
297 end;
298 {--------}
299 procedure TAbDeflateHelper.dhSetOnProgressStep(aValue : TAbProgressStep);
300 begin
301 FOnProgressStep := aValue;
302 end;
303 {--------}
304 procedure TAbDeflateHelper.dhSetOptions(aValue : longint);
305 begin
306 if (aValue <> Options) then begin
307 FOptions := aValue;
308 FZipOption := '?';
309 end;
310 end;
311 {--------}
312 procedure TAbDeflateHelper.dhSetWindowSize(aValue : longint);
313 var
314 NewValue : longint;
315 begin
316 if (aValue <> WindowSize) then begin
317 {calculate the window size rounded to nearest 1024 bytes}
318 NewValue := ((aValue + 1023) div 1024) * 1024;
319 {if the new window size is greater than 32KB...}
320 if (NewValue > 32 * 1024) then
321 {if the Deflate64 option is set, force to 64KB}
322 if ((Options and dfc_UseDeflate64) <> 0) then
323 NewValue := 64 * 1024
324 {otherwise, force to 32KB}
325 else
326 NewValue := 32 * 1024;
327 {set the new window size}
328 FWindowSize := NewValue;
329 end;
330 end;
331 {--------}
332 procedure TAbDeflateHelper.dhSetZipOption(aValue : AnsiChar);
333 begin
334 {notes:
335 The original Abbrevia code used the following table for
336 setting the equivalent values:
337 Good Lazy Chain UseLazy Option
338 4 4 4 N s ^
339 4 5 8 N |
340 4 6 32 N f faster
341 4 4 16 Y slower
342 8 16 32 Y n |
343 8 16 128 Y |
344 8 32 256 Y |
345 32 128 1024 Y |
346 32 258 4096 Y x V
347 The new Abbrevia 3 code follows these values to a certain extent.
348 }
349
350 {force to lower case}
351 if ('A' <= aValue) and (aValue <= 'Z') then
352 aValue := AnsiChar(ord(aValue) + ord('a') - ord('A'));
353
354 {if the value has changed...}
355 if (aValue <> PKZipOption) then begin
356
357 {switch on the new value...}
358 case aValue of
359 '0' : {no compression}
360 begin
361 FZipOption := aValue;
362 FOptions := (FOptions and (not $0F)) or dfc_CanUseStored;
363 FAmpleLength := 8; { not actually needed}
364 FChainLength := 32; { not actually needed}
365 FMaxLazy := 16; { not actually needed}
366 end;
367 '2' : {hidden option: Abbrevia 2 compatibility}
368 begin
369 FZipOption := aValue;
370 FOptions := FOptions or $0F;
371 FAmpleLength := 8;
372 FChainLength := 32;
373 FMaxLazy := 16;
374 end;
375 'f' : {fast compression}
376 begin
377 FZipOption := aValue;
378 FOptions := FOptions or $07; { no lazy matching}
379 FAmpleLength := 4;
380 FChainLength := 32;
381 FMaxLazy := 6;
382 end;
383 'n' : {normal compression}
384 begin
385 FZipOption := aValue;
386 FOptions := FOptions or $0F;
387 FAmpleLength := 16;
388 FChainLength := 32;
389 FMaxLazy := 24;
390 end;
391 's' : {super fast compression}
392 begin
393 FZipOption := aValue;
394 FOptions := FOptions or $07; { no lazy matching}
395 FAmpleLength := 4;
396 FChainLength := 4;
397 FMaxLazy := 4;
398 end;
399 'x' : {maximum compression}
400 begin
401 FZipOption := aValue;
402 FOptions := FOptions or $0F;
403 FAmpleLength := 64;{32;}
404 FChainLength := 4096;
405 FMaxLazy := 258;
406 end;
407 end;
408 end;
409 end;
410 {====================================================================}
411
412
413 {===TAbLogger========================================================}
414 const
415 LogBufferSize = 4096;
416 {--------}
417 constructor TAbLogger.Create(const aLogName : string);
418 begin
419 Assert(aLogName <> '',
420 'TAbLogger.Create: a filename must be provided for the logger');
421
422 {create the ancestor}
423 inherited Create;
424
425 {set the default line terminator}
426 {$IFDEF MSWINDOWS}
427 FLineDelim := ldCRLF;
428 {$ENDIF}
429 {$IFDEF UNIX}
430 FLineDelim := ldLF;
431 {$ENDIF}
432
433 {create and initialize the buffer}
434 GetMem(FBuffer, LogBufferSize);
435 FCurPos := FBuffer;
436
437 {create the log file}
438 FStream := TFileStream.Create(aLogName, fmCreate);
439 end;
440 {--------}
441 destructor TAbLogger.Destroy;
442 begin
443 {if there is a buffer ensure that it is flushed before freeing it}
444 if (FBuffer <> nil) then begin
445 if (FCurPos <> FBuffer) then
446 logWriteBuffer;
447 FreeMem(FBuffer, LogBufferSize);
448 end;
449
450 {free the stream}
451 FStream.Free;
452
453 {destroy the ancestor}
454 inherited Destroy;
455 end;
456 {--------}
457 function TAbLogger.logWriteBuffer : boolean;
458 var
459 BytesToWrite : longint;
460 BytesWritten : longint;
461 begin
462 BytesToWrite := FCurPos - FBuffer;
463 BytesWritten := FStream.Write(FBuffer^, BytesToWrite);
464 if (BytesWritten = BytesToWrite) then begin
465 Result := true;
466 FCurPos := FBuffer;
467 end
468 else begin
469 Result := false;
470 if (BytesWritten <> 0) then begin
471 Move(FBuffer[BytesWritten], FBuffer^, BytesToWrite - BytesWritten);
472 FCurPos := FBuffer + (BytesToWrite - BytesWritten);
473 end;
474 end;
475 end;
476 {--------}
477 function TAbLogger.Read(var Buffer; Count : longint) : longint;
478 begin
479 Assert(false, 'TAbLogger.Read: loggers are write-only, no reading allowed');
480 Result := 0;
481 end;
482 {--------}
483 function TAbLogger.Seek(const Offset : Int64; Origin : TSeekOrigin) : Int64;
484 begin
485 case Origin of
486 soBeginning :
487 begin
488 end;
489 soCurrent :
490 if (Offset = 0) then begin
491 Result := FStream.Position + (FCurPos - FBuffer);
492 Exit;
493 end;
494 soEnd :
495 if (Offset = 0) then begin
496 Result := FStream.Position + (FCurPos - FBuffer);
497 Exit;
498 end;
499 end;
500
501 Assert(false, 'TAbLogger.Seek: loggers are write-only, no seeking allowed');
502 Result := 0;
503 end;
504 {--------}
505 function TAbLogger.Write(const Buffer; Count : longint) : longint;
506 var
507 UserBuf : PAnsiChar;
508 BytesToGo : longint;
509 BytesToWrite : longint;
510 begin
511 {reference the user's buffer as a PChar}
512 UserBuf := @Buffer;
513
514 {start the counter for the number of bytes written}
515 Result := 0;
516
517 {if needed, empty the internal buffer into the underlying stream}
518 if (LogBufferSize = FCurPos - FBuffer) then
519 if not logWriteBuffer then
520 Exit;
521
522 {calculate the number of bytes to copy this time from the user's
523 buffer to the internal buffer}
524 BytesToGo := Count;
525 BytesToWrite := LogBufferSize - (FCurPos - FBuffer);
526 if (BytesToWrite > BytesToGo) then
527 BytesToWrite := BytesToGo;
528
529 {copy the bytes}
530 Move(UserBuf^, FCurPos^, BytesToWrite);
531
532 {adjust the counters}
533 inc(FCurPos, BytesToWrite);
534 dec(BytesToGo, BytesToWrite);
535 inc(Result, BytesToWrite);
536
537 {while there are still more bytes to copy, do so}
538 while (BytesToGo <> 0) do begin
539 {advance the user's buffer}
540 inc(UserBuf, BytesToWrite);
541
542 {empty the internal buffer into the underlying stream}
543 if not logWriteBuffer then
544 Exit;
545
546 {calculate the number of bytes to copy this time from the user's
547 buffer to the internal buffer}
548 BytesToWrite := LogBufferSize;
549 if (BytesToWrite > BytesToGo) then
550 BytesToWrite := BytesToGo;
551
552 {copy the bytes}
553 Move(UserBuf^, FCurPos^, BytesToWrite);
554
555 {adjust the counters}
556 inc(FCurPos, BytesToWrite);
557 dec(BytesToGo, BytesToWrite);
558 inc(Result, BytesToWrite);
559 end;
560 end;
561 {--------}
562 procedure TAbLogger.WriteLine(const S : string);
563 const
564 cLF : AnsiChar = ^J;
565 cCRLF : array [0..1] of AnsiChar = ^M^J;
566 begin
567 if (length(S) > 0) then
568 Write(S[1], length(S));
569 case FLineDelim of
570 ldLF : Write(cLF, sizeof(cLF));
571 ldCRLF : Write(cCRLF, sizeof(cCRLF));
572 end;
573 end;
574 {--------}
575 procedure TAbLogger.WriteStr(const S : string);
576 begin
577 if (length(S) > 0) then
578 Write(S[1], length(S));
579 end;
580 {====================================================================}
581
582
583 {===Calculate checksums==============================================}
584 procedure AbUpdateAdlerBuffer(var aAdler : longint;
585 var aBuffer; aCount : integer);
586 var
587 S1 : LongWord;
588 S2 : LongWord;
589 i : integer;
590 Buffer : PAnsiChar;
591 BytesToUse : integer;
592 begin
593 {Note: this algorithm will *only* work if the buffer is 4KB or less,
594 which is why we go to such lengths to chop up the user buffer
595 into usable chunks of 4KB.
596
597 However, for Delphi 3 there is no proper 32-bit longword.
598 Although the additions pose no problems in this situation,
599 the mod operations below (especially for S2) will be signed
600 integer divisions, producing an (invalid) signed result. In
601 this case, the buffer is chopped up into 2KB chunks to avoid
602 any signed problems.}
603
604 {split the current Adler checksum into its halves}
605 S1 := LongWord(aAdler) and $FFFF;
606 S2 := LongWord(aAdler) shr 16;
607
608 {reference the user buffer as a PChar: it makes it easier}
609 Buffer := @aBuffer;
610
611 {while there's still data to checksum...}
612 while (aCount <> 0) do begin
613
614 {calculate the number of bytes to checksum this time}
615 {$IFDEF HasLongWord}
616 BytesToUse := 4096;
617 {$ELSE}
618 BytesToUse := 2048;
619 {$ENDIF}
620 if (BytesToUse > aCount) then
621 BytesToUse := aCount;
622
623 {checksum the bytes}
624 for i := 0 to pred(BytesToUse) do begin
625 inc(S1, ord(Buffer^));
626 inc(S2, S1);
627 inc(Buffer);
628 end;
629
630 {recalibrate the Adler checksum halves}
631 S1 := S1 mod 65521;
632 S2 := S2 mod 65521;
633
634 {calculate the number of bytes still to go}
635 dec(aCount, BytesToUse);
636 end;
637
638 {join the halves to produce the complete Adler checksum}
639 aAdler := longint((S2 shl 16) or S1);
640 end;
641 {--------}
642 procedure AbUpdateCRCBuffer(var aCRC : longint;
643 var aBuffer; aCount : integer);
644 var
645 i : integer;
646 CRC : LongWord;
647 Buffer : PAnsiChar;
648 begin
649 {$R-}{$Q-}
650 {reference the user buffer as a PChar: it makes it easier}
651 Buffer := @aBuffer;
652
653 {get the current CRC as a local variable, it's faster}
654 CRC := aCRC;
655
656 {checksum the bytes in the buffer}
657 for i := 0 to pred(aCount) do begin
658 CRC := AbCrc32Table[byte(CRC) xor byte(Buffer^)] xor (CRC shr 8);
659 inc(Buffer);
660 end;
661
662 {return the new CRC}
663 aCRC := CRC;
664 {$R+}{$Q+}
665 end;
666 {====================================================================}
667
668
669 {===EAbInflateError==================================================}
670 constructor EAbInflateError.Create(const aMsg : string);
671 begin
672 inherited Create(
673 'Abbrevia inflate error, possibly a corrupted compressed stream. ' +
674 '(Internal cause: ' + aMsg + ')');
675 end;
676 {--------}
677 constructor EAbInflateError.CreateUnknown(const aMsg : string;
678 const aErrorMsg : string);
679 begin
680 inherited Create(aMsg + ': ' + aErrorMsg);
681 end;
682 {====================================================================}
683
684
685 {===EAbDeflateError==================================================}
686 constructor EAbDeflateError.Create(const aMsg : string);
687 begin
688 inherited Create(
689 'Abbrevia deflate error. ' +
690 '(Internal cause: ' + aMsg + ')');
691 end;
692 {--------}
693 constructor EAbDeflateError.CreateUnknown(const aMsg : string;
694 const aErrorMsg : string);
695 begin
696 inherited Create(aMsg + ': ' + aErrorMsg);
697 end;
698 {====================================================================}
699
700
701 {===Node manager=====================================================}
702 const
703 PageSize = 8 * 1024;
704 type
705 PGenericNode = ^TGenericNode;
706 TGenericNode = packed record
707 gnNext : PGenericNode;
708 gnData : record end;
709 end;
710 {--------}
711 constructor TAbNodeManager.Create(aNodeSize : cardinal);
712 const
713 Gran = sizeof(pointer);
714 Mask = not (Gran - 1);
715 begin
716 {create the ancestor}
717 inherited Create;
718
719 {save the node size rounded to nearest 4 bytes}
720 if (aNodeSize <= sizeof(pointer)) then
721 aNodeSize := sizeof(pointer)
722 else
723 aNodeSize := (aNodeSize + Gran - 1) and Mask;
724 FNodeSize := aNodeSize;
725
726 {calculate the page size (default 1024 bytes) and the number of
727 nodes per page; if the default page size is not large enough for
728 two or more nodes, force a single node per page}
729 FNodesPerPage := (PageSize - sizeof(pointer)) div aNodeSize;
730 if (FNodesPerPage > 1) then
731 FPageSize := PageSize
732 else begin
733 FNodesPerPage := 1;
734 FPagesize := aNodeSize + sizeof(pointer);
735 end;
736 end;
737 {--------}
738 destructor TAbNodeManager.Destroy;
739 var
740 Temp : pointer;
741 begin
742 {dispose of all the pages, if there are any}
743 while (FPageHead <> nil) do begin
744 Temp := PGenericNode(FPageHead)^.gnNext;
745 FreeMem(FPageHead, FPageSize);
746 FPageHead := Temp;
747 end;
748
749 {destroy the ancestor}
750 inherited Destroy;
751 end;
752 {--------}
753 function TAbNodeManager.AllocNode : pointer;
754 begin
755 Result := FFreeList;
756 if (Result = nil) then
757 Result := nmAllocNewPage
758 else
759 FFreeList := PGenericNode(Result)^.gnNext;
760 end;
761 {--------}
762 function TAbNodeManager.AllocNodeClear : pointer;
763 begin
764 Result := FFreeList;
765 if (Result = nil) then
766 Result := nmAllocNewPage
767 else
768 FFreeList := PGenericNode(Result)^.gnNext;
769 FillChar(Result^, FNodeSize, 0);
770 end;
771 {--------}
772 procedure TAbNodeManager.FreeNode(aNode : pointer);
773 begin
774 {add the node (if non-nil) to the top of the free list}
775 if (aNode <> nil) then begin
776 PGenericNode(aNode)^.gnNext := FFreeList;
777 FFreeList := aNode;
778 end;
779 end;
780 {--------}
781 function TAbNodeManager.nmAllocNewPage : pointer;
782 var
783 NewPage : PAnsiChar;
784 i : integer;
785 FreeList : pointer;
786 NodeSize : integer;
787 begin
788 {allocate a new page and add it to the front of the page list}
789 GetMem(NewPage, FPageSize);
790 PGenericNode(NewPage)^.gnNext := FPageHead;
791 FPageHead := NewPage;
792
793 {now split up the new page into nodes and push them all onto the
794 free list; note that the first 4 bytes of the page is a pointer to
795 the next page, so remember to skip over it}
796 inc(NewPage, sizeof(pointer));
797 FreeList := FFreeList;
798 NodeSize := FNodeSize;
799 for i := 0 to pred(FNodesPerPage) do begin
800 PGenericNode(NewPage)^.gnNext := FreeList;
801 FreeList := NewPage;
802 inc(NewPage, NodeSize);
803 end;
804
805 {return the top of the list}
806 Result := FreeList;
807 FFreeList := PGenericNode(Result)^.gnNext;
808 end;
809 {====================================================================}
810
811
812 {====================================================================}
813 procedure AbortProgress;
814 begin
815 raise EAbAbortProgress.Create('Abort');
816 end;
817 {====================================================================}
818
819 end.