home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
beehive
/
bbs
/
picsuncr.arc
/
TPHEAP.DOC
< prev
next >
Wrap
Text File
|
1991-08-11
|
6KB
|
147 lines
TURB╧ PASCA╠ HEA╨ MANAGEMENT
W« BRIMHAL╠ 13DEC87
ZNOD┼ 5▓ (602)996-8739
OVERVIEW
Thi≤ filσ contain≤ informatioε t∩ supplemen⌠ thσ Dynamiπ Storagσ ì
sectioε oµ thσ CP/═ 8░ Turb∩ Pasca∞ ver≤ 3.░ referencσ manua∞ ì
whicΦ doe≤ no⌠ adequatel∙ cove≥ thesσ topics:
1⌐ Changinτ thσ addres≤ oµ thσ heap.
2⌐ Preservinτ thσ hea≡ durinτ ß prograφ chain.
┴ cal∞ t∩ BorlanΣ technica∞ suppor⌠ diΣ no⌠ improvσ thσ situatioε ì
s∩ thσ followinτ informatioε wa≤ obtaineΣ froφ severa∞ hour≤ oµ ì
disassembl∙ anΣ debugging.
Firs⌠ oµ al∞ ß CO═ filσ produceΣ b∙ Turb∩ Pasca∞ follow≤ thi≤ ì
genera∞ memor∙ map:
HIG╚ MEMORY ---------------------------------
bdos
---------------------------------
variablσ area
---------------------------------
prograφ stacδ (growinτ down)
---------------------------------
recursioε stacδ (growinτ down)
hea≡ (growinτ up)
---------------------------------
prograφ code
---------------------------------
runtimσ library
---------------------------------
pagσ ░ vector≤ ª pointers
LO╫ MEMOR┘ ---------------------------------
Turb∩ maintain≤ thesσ ▓ hea≡ pointer≤ iε pagσ 0:
00C┤ HEAPPTR║ d≈ ░ ;pointe≥ t∩ star⌠ oµ freσ memor∙ a⌠ to≡ oµ heap
00D┤ HEAPFRE║ d≈ ░ ;pointe≥ t∩ 1s⌠ blocδ oµ freσ memor∙ iε heap
HeapPt≥ i≤ predefineΣ anΣ caε bσ accesseΣ likσ an∙ intege≥ ì
variable« HeapFrσ i≤ no⌠ eveε mentioneΣ iε thσ Turb∩ Referencσ ì
manual« I⌠ mus⌠ bσ declareΣ a≤ aε absolutσ variablσ beforσ i⌠ can
bσ accessed:
Var
HeapFrσ ║ intege≥ absolutσ $00de;
Wheε ß Turb∩ prograφ run≤ o≥ execute≤ HeapPtr ª HeapFre arσ botΦ ì
se⌠ t∩ thσ firs⌠ bytσ pas⌠ thσ enΣ oµ thσ prograφ code« Thi≤ i≤ ì
wherσ thσ hea≡ begins« Thσ firs⌠ ┤ byte≤ oµ thσ hea≡ arσ theε ì
cleareΣ t∩ zero«
è
Wheε memor∙ i≤ firs⌠ allocateΣ oε thσ hea≡ witΦ thσ NE╫ procedurσ ì
i⌠ wil∞ bσ placeΣ a⌠ thσ locatioε addresseΣ b∙ HeapPtr« Therσ i≤ ì
ß ┤ bytσ overheaΣ fo≥ eacΦ blocδ tha⌠ i≤ allocated¼ s∩ bytes¼ ì
chars¼ ª integer≤ wil∞ al∞ usσ u≡ ┤ byte≤ oµ storagσ oε thσ heap« ì
Thσ ┤ byte≤ followinτ thσ newl∙ allocateΣ blocδ wil∞ alway≤ bσ ì
zeroeΣ t∩ marδ thσ to≡ oµ thσ heap.
Wheε storagσ i≤ reclaimeΣ witΦ thσ DISPOS┼ procedurσ ß linkeΣ ì
lis⌠ i≤ createΣ startinτ witΦ thσ lowes⌠ freσ blocδ iε thσ ì
heap« Thσ addres≤ oµ thi≤ blocδ i≤ placeΣ iε HeapFre« Thσ firs⌠ ┤ ì
byte≤ oµ eacΦ freσ blocδ iε thσ hea≡ contaiε linδ pointer≤ iε ì
thi≤ format:
d≈ nex⌠ ╗addres≤ oµ nex⌠ freσ block
d≈ sizσ ;sizσ oµ thi≤ freσ block
Thσ to≡ oµ thσ hea≡ alway≤ contain≤ ┤ byte≤ oµ ░ whicΦ marδ the
enΣ oµ thσ linδ list.
Thσ actua∞ pointe≥ variable≤ arσ storeΣ a≤ integers« Iµ the∙ arσ ì
declareΣ globall∙ the∙ wil∞ bσ placeΣ iε thσ variablσ areß whicΦ ì
i≤ no⌠ affecteΣ b∙ chain≤ o≥ execute≤ a≤ lonτ a≤ thσ chaineΣ ì
program≤ don'⌠ overwritσ tha⌠ sectioε oµ memory.
T╧ CHANG┼ TH┼ HEA╨ ADDRESS
Thσ Turb∩ Referencσ manua∞ give≤ thσ impressioε tha⌠ thσ hea≡ ì
addres≤ caε bσ changeΣ simpl∙ b∙ assigninτ ß ne≈ valuσ t∩ ì
HeapPtr« Thi≤ i≤ no⌠ correctí T∩ changσ thσ hea≡ addres≤ t∩ thσ ì
valuσ Θ yo⌡ must:
1⌐ Declarσ followinτ variable≤:ì
Var
Θ ║ integer;
HeapFrσ ║ intege≥ absolutσ $00de;
2⌐ Rese⌠ HeapPtr¼ HeapFrσ ª firs⌠ ┤ byte≤ oµ heap:
HeapPtr:=i; √ Se⌠ hea≡ pointe≥ t∩ addres≤ ne≈ hea≡ }
HeapFre:=i╗ √ Se⌠ freσ meφ pt≥ t∩ addres≤ ne≈ heap}
fo≥ i:=░ t∩ │ do
mem[HeapPtr+i]:=0╗ √ Zer∩ 1s⌠ ┤ byte≤ oµ ne≈ hea≡ }
RecurPt≥ i≤ anothe≥ predefineΣ variablσ tha⌠ addresse≤ thσ ì
recursioε stack« Makσ surσ HeapPt≥ i≤ neve≥ greate≥ thaε RecurPt≥ ì
anΣ tha⌠ HeapPtr i≤ alway≤ addressinτ ß freσ memor∙ area!
.PAè
T╧ PRESERV┼ TH┼ HEA╨ DURIN╟ ┴ CHAIN
Thσ Turb∩ loade≥ overwrite≤ u≡ t∩ 12╕ byte≤ oµ raφ abovσ thσ ì
prograφ codσ witΦ ß CO═ filσ wheε i⌠ i≤ loadeΣ froφ disδ witΦ thσ ì
executσ command« Thi≤ overwritteε memor∙ i≤ thσ firs⌠ 12╕ byte≤ ì
oµ thσ heap« T∩ preservσ thσ hea≡ durinτ ß chaiε thi≤ blocδ mus⌠ ì
bσ saveΣ alonτ witΦ thσ hea≡ pointers« Thesσ value≤ caε theε bσ ì
restoreΣ a≤ follow≤ afte≥ thσ chaiε o≥ executσ i≤ performed:
1⌐ Declarσ followinτ globa∞ variable≤ anΣ makσ surσ al∞ ì
ááááápointe≥ variable≤ arσ global:
Var
heap,heap_ptr,heap_frσ ║ integer;
HeapFrσ ║ intege≥ absolutσ $00de;
heap_byte≤ ║ arra∙[0..127▌ oµ byte;
ááááá2⌐ Savσ hea≡ addres≤ wheε prograφ starts.ì
heap:=HeapPtr;
3⌐ Savσ 1s⌠ 128 byte≤ oµ heap¼ HeapPt≥ ª HeapFrσ jus⌠ beforσ ì
áááááchaining«
heap_fre:=HeapFre;
heap_ptr:=HeapPtr;
fo≥ i:=░ t∩ 12╖ do
heap_bytes[i]:=mem[heap+i]);
4⌐ Chaiε t∩ othe≥ prograφ anΣ executσ bacδ t∩ maiε program« ì
áááááMakσ surσ thσ chaineΣ prograφ doe≤ no⌠ overwritσ an∙ othe≥ ì
áááááarea≤ oµ thσ hea≡ o≥ variablσ area!
5⌐ Restorσ saveΣ value≤ beforσ accessinτ heap:
HeapPtr:=heap_ptr;
HeapFre:=heap_fre;
fo≥ i:╜ ░ t∩ 127 do
mem[heap+i]:=heap_bytes[i];
ááááá6⌐ Thσ hea≡ i≤ no≈ completel∙ restoreΣ anΣ caε no≈ bσ ì
áááááaccesseΣ a≤ thougΦ thσ chaiε neve≥ tooδ place«