STDMEM.A 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  1. (* Release 3.10 *)
  2. (*-------------------------------------------------------------------------*
  3. * *
  4. * STDMEM.A - Standard memory management for OS/2 and Extender *
  5. * *
  6. * COPYRIGHT (C) 1991..1992 Clarion Software Corporation. *
  7. * All Rights Reserved *
  8. * *
  9. *--------------------------------------------------------------------------*)
  10. include "corelib.inc"
  11. include "stdmem.inc"
  12. _HeapCheck = 0 (* Set to 1 to generate checking version *)
  13. module StdMem
  14. segment _DATA(DATA,28H)
  15. public __MaxReallocHugeSegments: dw 16
  16. (* __MaxReallocHugeSegments sets the maximum number of 64k
  17. segments that the current allocation may be reallocated to.
  18. It may be altered at run time.
  19. The default maximum reallocation size is set to 1 megabyte.
  20. This default will consume valuable resources so,set to 0 if
  21. reallocation is not required *)
  22. segment _DATA(DATA,48H)
  23. public StdMem@HugeShift : dw 0
  24. (*%T _DLL *)
  25. (* ***** Dummy's to satisfy Dynalink Library ***** *)
  26. public __firstfree : db 0
  27. public __firstheap : db 0
  28. segment _BSS(BSS,48H)
  29. public __fheapsem : db 0
  30. public __fheaptop : db 0
  31. segment _TEXT(CODE,48H)
  32. public __heapwalk :
  33. public __heapset :
  34. public __heapchk :
  35. public __osfreer :
  36. public __memavl :
  37. (*%T _fcall *)
  38. public __fheapwalk :
  39. public __fheapset :
  40. public __fheapchk :
  41. ret far 0
  42. (*%E *)
  43. (*%F _fcall *)
  44. ret 0
  45. (*%E *)
  46. (*%E _DLL *)
  47. section
  48. (***********************************************************************)
  49. (* $InitHeap - Initialize a new heaplette *)
  50. (* *)
  51. (* All Memory Models *)
  52. (* *)
  53. (* Internal Procedure - Near Call *)
  54. (* Parameters: *)
  55. (* ax = Size of heap *)
  56. (* di = Base of heap *)
  57. (* es = Heap Selector *)
  58. (* Trashed: *)
  59. (* none *)
  60. (* Return: *)
  61. (* ax = Largest Allocation *)
  62. (* *)
  63. (***********************************************************************)
  64. segment _TEXT(CODE,48H)
  65. public $InitHeap :
  66. push bx
  67. mov bx, memSegOverhead
  68. sub ax, bx
  69. add bx, di
  70. mov word es:[di][memFreeSize], 0 (* Dummy Size *)
  71. mov es:[di][memNextFree], bx (* First Free *)
  72. mov es:[di][memLargestFree], ax (* Largest *)
  73. mov es:[bx][memFreeSize], ax (* 1st Segment Size *)
  74. mov word es:[bx][memNextFree], 0 (* Next Free *)
  75. pop bx
  76. ret 0
  77. section
  78. (***********************************************************************)
  79. (* $AllocHeap - Allocate memory from a heaplette *)
  80. (* *)
  81. (* All Memory Models *)
  82. (* *)
  83. (* Internal Procedure - Near Call *)
  84. (* Parameters: *)
  85. (* ax - Requested Size *)
  86. (* di - Heap Base *)
  87. (* dx - Heap Selector *)
  88. (* Trashes: *)
  89. (* none *)
  90. (* Returns: *)
  91. (* ax - Allocation Offset (0 if failed) *)
  92. (* bx - New maximum block size in heap *)
  93. (* dx - unchanged *)
  94. (* Assumption: *)
  95. (* Since the calling procedure must check, this procedure assumes *)
  96. (* that the block it is searching contains a block large enough *)
  97. (* to fill the request. *)
  98. (* *)
  99. (***********************************************************************)
  100. segment _TEXT(CODE,48H)
  101. extrn __FatalError
  102. extrn __FatalErrorPos
  103. public $AllocHeap :
  104. push ds
  105. push si
  106. mov ds, dx
  107. mov bx, di
  108. $$AllocLoop: (* Find large enough free block *)
  109. mov si, bx
  110. mov bx, [bx][memNextFree]
  111. (*%T _HeapCheck *)
  112. cmp si, bx
  113. jae $$HeapCorrupt
  114. (*%E *)
  115. cmp [bx][memFreeSize], ax
  116. jb $$AllocLoop (* bx = block | si = Prev block *)
  117. je $$Unlink
  118. add [si][memNextFree], ax (* Increment prev Block pointer *)
  119. mov si, [si][memNextFree]
  120. sub ax, [bx][memFreeSize]
  121. neg ax
  122. mov [si][memFreeSize], ax
  123. $$Unlink:
  124. mov ax, [bx][memNextFree]
  125. mov [si][memNextFree], ax
  126. mov ax, [di][memLargestFree]
  127. cmp ax, [bx][memFreeSize]
  128. jne $$AllocExit
  129. mov si, [di][memNextFree]
  130. jmp $$ScanStart
  131. $$ScanLoop:
  132. cmp ax, [si][memFreeSize]
  133. ja $$ScanContinue
  134. $$ScanStart:
  135. mov ax, [si][memFreeSize]
  136. $$ScanContinue:
  137. mov si, [si][memNextFree]
  138. or si, si
  139. jnz $$ScanLoop
  140. mov [di][memLargestFree], ax
  141. $$AllocExit:
  142. xchg ax, bx
  143. pop si
  144. pop ds
  145. ret 0
  146. (*%T _HeapCheck *)
  147. $$HeapCorrupt:
  148. call far __FatalErrorPos
  149. mov cx, ERROR_HEAPCORRUPT
  150. push cx
  151. push dx
  152. push ax
  153. call far __FatalError
  154. (*%E HeapCheck*)
  155. section
  156. (***********************************************************************)
  157. (* $ResizeHeap - Resize Allocated memory from a heaplette *)
  158. (* *)
  159. (* All Memory Models *)
  160. (* *)
  161. (* Internal Procedure - Near Call *)
  162. (* Parameters: *)
  163. (* ax - Allocation Size *)
  164. (* bx - Allocation Offset *)
  165. (* cx - Heap Base *)
  166. (* dx - Requested Size *)
  167. (* ds - Heap Selector *)
  168. (* Trashes: *)
  169. (* es *)
  170. (* Returns: *)
  171. (* ax - New maximum block size in heap or 0 if failed *)
  172. (* bx - New Allocation Offset *)
  173. (* *)
  174. (***********************************************************************)
  175. segment _TEXT(CODE,48H)
  176. public $ResizeHeap :
  177. push si
  178. push di
  179. push cx
  180. mov si, cx
  181. $$ResizeLoop:
  182. mov di, [si][memNextFree]
  183. or di, di
  184. jz $$ResizeFound
  185. cmp di, bx
  186. ja $$ResizeFound
  187. mov si, di
  188. jmp $$ResizeLoop
  189. $$ResizeFound:
  190. mov cx, bx
  191. add cx, ax (* cx = ax + bx *)
  192. cmp ax, dx
  193. jb $$Expand
  194. push bx
  195. add bx, dx
  196. cmp cx, di
  197. jne $$NoAttach
  198. mov cx, ax
  199. sub cx, dx
  200. add cx, [di][memFreeSize]
  201. mov [bx][memFreeSize], cx
  202. mov cx, [di][memNextFree]
  203. jmp $$Exit
  204. $$NoAttach:
  205. mov cx, ax
  206. sub cx, dx
  207. mov [bx][memFreeSize], cx
  208. mov cx, [si][memNextFree]
  209. $$Exit:
  210. mov [bx][memNextFree], cx
  211. mov [si][memNextFree], bx
  212. pop bx
  213. jmp near $$FindLargest
  214. $$Expand:
  215. cmp cx, di (* is following block free? *)
  216. jne $$PrependTest (* if not, try to prepend *)
  217. mov cx, ax (* cx = ax + di.size (available block) *)
  218. add cx, [di][memFreeSize]
  219. push cx
  220. cmp cx, dx (* is it enough? *)
  221. jae $$Append (* if so, use it *)
  222. mov cx, si
  223. add cx, [si][memFreeSize]
  224. cmp cx, bx (* is previous block free? *)
  225. pop cx
  226. jne near $$ResizeFailed (* if not, return failed *)
  227. add cx, [si][memFreeSize]
  228. cmp cx, dx (* is available space large enough? *)
  229. jb $$ResizeFailed
  230. db 06AH, 000H (*I push 0 *)
  231. jmp $$AppendAll
  232. $$Append:
  233. jbe $$AppendAll
  234. push ax
  235. sub cx, dx (* cx = new di free block size *)
  236. mov ax, [di][memNextFree]
  237. mov di, bx
  238. add di, dx
  239. mov [di][memFreeSize], cx
  240. mov [di][memNextFree], ax
  241. pop ax
  242. jmp $$AppendExit
  243. $$AppendAll:
  244. sub dx, [di][memFreeSize]
  245. mov di, [di][memNextFree]
  246. $$AppendExit:
  247. mov [si][memNextFree], di
  248. pop cx
  249. jcxz $$PrependAllTest
  250. jmp $$FindLargest
  251. $$PrependTest:
  252. mov cx, si
  253. add cx, [si][memFreeSize]
  254. cmp cx, bx (* is previous block free? *)
  255. jne $$ResizeFailed (* if not, return failed *)
  256. $$PrependAllTest:
  257. mov cx, ax
  258. add cx, [si][memFreeSize]
  259. cmp cx, dx (* is available space large enough? *)
  260. jb $$ResizeFailed
  261. jne $$Prepend
  262. pop di
  263. push di
  264. jmp $$PrependStart
  265. $$PrependLoop:
  266. mov di, [di][memNextFree]
  267. $$PrependStart:
  268. cmp si, [di][memNextFree]
  269. jne $$PrependLoop
  270. mov cx, [si][memNextFree]
  271. mov [di][memNextFree], cx
  272. xchg si, bx
  273. jmp $$PrependAll
  274. $$Prepend:
  275. mov cx, dx
  276. sub cx, ax
  277. sub [si][memFreeSize], cx
  278. mov si, bx
  279. sub bx, cx
  280. $$PrependAll:
  281. mov di, bx
  282. mov cx, ax
  283. shr cx, 1
  284. push ds
  285. pop es
  286. jnc $$ShiftEven
  287. movsb
  288. $$ShiftEven:
  289. rep; movsw
  290. $$FindLargest:
  291. pop di
  292. mov si, di
  293. xor ax, ax
  294. $$FindLoop:
  295. mov di, [di][memNextFree]
  296. or di, di
  297. jz $$LargestFound
  298. cmp ax, [di][memFreeSize]
  299. jae $$FindLoop
  300. mov ax, [di][memFreeSize]
  301. jmp $$FindLoop
  302. $$LargestFound:
  303. mov [si][memLargestFree], ax
  304. $$Return:
  305. pop di
  306. pop si
  307. ret 0
  308. $$ResizeFailed:
  309. pop ax
  310. xor ax, ax
  311. jmp $$Return
  312. section
  313. (***********************************************************************)
  314. (* $ReallocHeap - Re-Allocates Allocated memory from a heaplette. *)
  315. (* Will not move the block in memory *)
  316. (* *)
  317. (* All Memory Models *)
  318. (* *)
  319. (* Internal Procedure - Near Call *)
  320. (* Parameters: *)
  321. (* ax - Allocation Size *)
  322. (* bx - Allocation Offset *)
  323. (* cx - Heap Base *)
  324. (* dx - Requested Size *)
  325. (* ds - Heap Selector *)
  326. (* Trashes: *)
  327. (* none *)
  328. (* Returns: *)
  329. (* ax - New maximum block size in heap or 0 if failed *)
  330. (* bx - New Allocation Offset *)
  331. (* *)
  332. (***********************************************************************)
  333. segment _TEXT(CODE,48H)
  334. public $ReallocHeap :
  335. push si
  336. push di
  337. push cx
  338. mov si, cx
  339. $$ResizeLoop:
  340. mov di, [si][memNextFree]
  341. or di, di
  342. jz $$ResizeFound
  343. cmp di, bx
  344. ja $$ResizeFound
  345. mov si, di
  346. jmp $$ResizeLoop
  347. $$ResizeFound:
  348. mov cx, bx
  349. add cx, ax (* cx = ax + bx *)
  350. cmp ax, dx
  351. jb $$Expand
  352. push bx
  353. add bx, dx
  354. cmp cx, di
  355. jne $$NoAttach
  356. mov cx, ax
  357. sub cx, dx
  358. add cx, [di][memFreeSize]
  359. mov [bx][memFreeSize], cx
  360. mov cx, [di][memNextFree]
  361. jmp $$Exit
  362. $$NoAttach:
  363. mov cx, ax
  364. sub cx, dx
  365. mov [bx][memFreeSize], cx
  366. mov cx, [si][memNextFree]
  367. $$Exit:
  368. mov [bx][memNextFree], cx
  369. mov [si][memNextFree], bx
  370. pop bx
  371. jmp $$FindLargest
  372. $$Expand:
  373. cmp cx, di (* is following block free? *)
  374. jne $$ResizeFailed (* if not, return failed *)
  375. mov cx, ax (* cx = ax + di.size (available block) *)
  376. add cx, [di][memFreeSize]
  377. cmp cx, dx (* is it enough? *)
  378. je $$AppendAll (* if exactly, use it all *)
  379. jb $$ResizeFailed (* if not, return failed *)
  380. sub cx, dx (* if so, use it *)
  381. mov ax, [di][memNextFree]
  382. mov di, bx
  383. add di, dx
  384. mov [di][memFreeSize], cx
  385. mov [di][memNextFree], ax
  386. jmp $$AppendExit
  387. $$AppendAll:
  388. mov di, [di][memNextFree]
  389. $$AppendExit:
  390. mov [si][memNextFree], di
  391. $$FindLargest:
  392. pop di
  393. mov si, di
  394. xor ax, ax
  395. $$FindLoop:
  396. mov di, [di][memNextFree]
  397. or di, di
  398. jz $$LargestFound
  399. cmp ax, [di][memFreeSize]
  400. jae $$FindLoop
  401. mov ax, [di][memFreeSize]
  402. jmp $$FindLoop
  403. $$LargestFound:
  404. mov [si][memLargestFree], ax
  405. $$Return:
  406. pop di
  407. pop si
  408. ret 0
  409. $$ResizeFailed:
  410. pop ax
  411. xor ax, ax
  412. jmp $$Return
  413. section
  414. (***********************************************************************)
  415. (* $FreeHeap - Deallocate memory from a heaplette *)
  416. (* *)
  417. (* All Memory Models *)
  418. (* *)
  419. (* Internal Procedure - Near Call *)
  420. (* Parameters: *)
  421. (* ax - Allocated Size *)
  422. (* bx - Heap Offset *)
  423. (* cx - Heap Base *)
  424. (* ds - Heap Selector *)
  425. (* Trashes: *)
  426. (* none *)
  427. (* Returns: *)
  428. (* ax - New maximum block size in heap *)
  429. (* *)
  430. (***********************************************************************)
  431. segment _TEXT(CODE,48H)
  432. extrn __FatalError
  433. extrn __FatalErrorPos
  434. public $FreeHeap :
  435. push si
  436. push di
  437. mov word [bx][memNextFree], 0
  438. mov [bx][memFreeSize], ax
  439. mov di, cx (* equ = mov di, memFirstFree *)
  440. $$FreeLoop:
  441. mov si, [di][memNextFree]
  442. or si, si
  443. jz $$PrependBlock (* end of chain *)
  444. cmp si, bx
  445. ja $$AppendBlock (* Insert before si *)
  446. mov di, si
  447. jmp $$FreeLoop
  448. $$AppendBlock:
  449. mov ax, [bx][memFreeSize]
  450. add ax, bx
  451. cmp ax, si (* Check for Merge Up *)
  452. (*%T _HeapCheck *)
  453. ja $$HeapCorrupt
  454. (*%E *)
  455. jne $$NoAppend
  456. mov ax, [si][memFreeSize]
  457. add [bx][memFreeSize], ax
  458. mov ax, [si][memNextFree]
  459. mov [bx][memNextFree], ax
  460. jmp $$PrependBlock
  461. $$NoAppend:
  462. mov [bx][memNextFree], si
  463. $$PrependBlock:
  464. mov ax, [di][memFreeSize]
  465. add ax, di
  466. cmp ax, bx (* Check for Merge Down *)
  467. (*%T _HeapCheck *)
  468. ja $$HeapCorrupt
  469. (*%E *)
  470. jne $$NoPrepend
  471. mov ax, [bx][memFreeSize]
  472. add [di][memFreeSize], ax
  473. mov ax, [bx][memNextFree]
  474. mov [di][memNextFree], ax
  475. jmp $$FreeExit
  476. $$NoPrepend:
  477. mov [di][memNextFree], bx
  478. mov di, bx
  479. $$FreeExit:
  480. mov si, cx
  481. mov ax, [si][memLargestFree]
  482. mov bx, [di][memFreeSize]
  483. cmp ax, bx
  484. jae $$NotLargest
  485. mov [si][memLargestFree], bx
  486. mov ax, bx
  487. $$NotLargest:
  488. pop di
  489. pop si
  490. ret 0
  491. (*%T _HeapCheck *)
  492. $$HeapCorrupt:
  493. call far __FatalErrorPos
  494. mov cx, ERROR_HEAPCORRUPT
  495. push cx
  496. push dx
  497. push ax
  498. call far __FatalError
  499. (*%E _HeapCheck*)
  500. section
  501. (***********************************************************************)
  502. (* $MarkHeap - Return Current Heap Free State *)
  503. (* *)
  504. (* All Memory Models *)
  505. (* *)
  506. (* Internal Procedure - Near Call *)
  507. (* Parameters: *)
  508. (* bx - Heap Base *)
  509. (* ds - Heap Selector *)
  510. (* Trashes: *)
  511. (* none *)
  512. (* Returns: *)
  513. (* bx - Mark Offset *)
  514. (* Notes: *)
  515. (* Cannot corrupt es *)
  516. (* *)
  517. (***********************************************************************)
  518. segment _TEXT(CODE,48H)
  519. public $MarkHeap :
  520. mov bx, [bx][memNextFree]
  521. cmp word [bx][memNextFree], 0
  522. jnz $MarkHeap
  523. ret 0
  524. section
  525. (***********************************************************************)
  526. (* $ReleaseHeap - Return Heap to Previous State *)
  527. (* *)
  528. (* All Memory Models *)
  529. (* *)
  530. (* Internal Procedure - Near Call *)
  531. (* Parameters: *)
  532. (* ax - Heap Mark Offset *)
  533. (* bx - Heap Base *)
  534. (* ds - Heap Selector *)
  535. (* Trashes: *)
  536. (* none *)
  537. (* Returns: *)
  538. (* ax - Largest Free *)
  539. (* bx - Available *)
  540. (* *)
  541. (***********************************************************************)
  542. segment _TEXT(CODE,48H)
  543. public $ReleaseHeap :
  544. push si
  545. push di
  546. push cx
  547. mov di, bx
  548. $$FindPrev:
  549. mov si, di
  550. mov di, [si][memNextFree]
  551. or di, di
  552. jz $$PrevFound
  553. cmp ax, di
  554. ja $$FindPrev
  555. je $$FindEnd
  556. $$PrevFound:
  557. mov cx, si
  558. add cx, [si][memFreeSize]
  559. cmp ax, cx
  560. je $$Concat
  561. mov [si][memNextFree], ax
  562. jmp $$FindEndStart
  563. $$Concat:
  564. mov ax, si
  565. jmp $$FindEndStart
  566. $$FindEnd:
  567. mov di, [di][memNextFree]
  568. or di, di
  569. jz $$FindLargestStart
  570. $$FindEndStart:
  571. cmp word [di][memNextFree], 0
  572. jnz $$FindEnd
  573. mov si, ax
  574. mov cx, di
  575. sub cx, ax
  576. add cx, [di][memFreeSize]
  577. mov [si][memFreeSize], cx
  578. mov word [si][memNextFree], 0
  579. $$FindLargestStart:
  580. mov di, bx
  581. mov si, bx
  582. xor ax, ax
  583. mov bx, ax
  584. $$FindLargest:
  585. mov di, [di][memNextFree]
  586. or di, di
  587. jz $$LargestFound
  588. add bx, [di][memFreeSize]
  589. cmp ax, [di][memFreeSize]
  590. jae $$FindLargest
  591. mov ax, [di][memFreeSize]
  592. jmp $$FindLargest
  593. $$LargestFound:
  594. mov [si][memLargestFree], ax
  595. pop cx
  596. pop di
  597. pop si
  598. ret 0
  599. section
  600. (*%F _DLL *)
  601. (***********************************************************************)
  602. (* StdMem$InitNear - Standard Module Near Heap Initialization *)
  603. (* *)
  604. (* All Memory Models *)
  605. (* *)
  606. (* Trashes: *)
  607. (* es *)
  608. (* *)
  609. (* Automatic Initialization Procedure *)
  610. (* Brought in by reference to @NearHeapAvail *)
  611. (* *)
  612. (***********************************************************************)
  613. segment _DATA(DATA,48H)
  614. segment _INIT(INIT_CODE,28H)
  615. segment _TEXT(CODE,48H)
  616. extrn $InitHeap
  617. extrn __heap_size
  618. extrn __near_heap_start (* Magic label *)
  619. select _TEXT
  620. $$DoInit:
  621. select _INIT
  622. Init: dw INIT_MAGIC
  623. db initPri1
  624. (*%T _fcall *)
  625. dd $$DoInit
  626. (*%E *)
  627. (*%F _fcall *)
  628. dw $$DoInit
  629. (*%E *)
  630. select _DATA
  631. public @NearHeapLen : dw 0
  632. public @NearHeapBase : dw 0
  633. public @NearHeapAvail : dw 0, Init
  634. select _TEXT
  635. public StdMem$InitNear :
  636. push ax
  637. push di
  638. (*%T _fdata *)
  639. push ds
  640. mov ax, _DATA
  641. mov ds, ax
  642. (*%E *)
  643. (*%F _fdata *)
  644. mov ax, ds
  645. (*%E *)
  646. mov es, ax
  647. mov ax, __near_heap_start
  648. add ax, 3
  649. and al, 0FEH
  650. mov di, ax
  651. mov [@NearHeapBase], di
  652. add ax, [__heap_size]
  653. jnc $$SizeSet
  654. mov ax, 0FFFDH
  655. jmp $$NearHeapGood
  656. $$SizeSet:
  657. dec ax
  658. dec ax
  659. push ax
  660. sub ax, 10H
  661. cmp ax, di
  662. pop ax
  663. ja $$NearHeapGood
  664. xor ax, ax
  665. jmp $$NoNearHeap
  666. $$NearHeapGood:
  667. sub ax, di
  668. mov [@NearHeapLen], ax
  669. (* Near Heap Initialization *)
  670. or ax, ax
  671. jz $$NoNearHeap
  672. call $InitHeap
  673. $$NoNearHeap:
  674. mov [@NearHeapAvail], ax
  675. (*%T _fdata *)
  676. pop ds
  677. (*%E *)
  678. pop di
  679. pop ax
  680. (*%T _fcall *)
  681. ret far 0
  682. (*%E *)
  683. (*%F _fcall *)
  684. ret 0
  685. (*%E *)
  686. (*%E _DLL *)
  687. section
  688. (*%F _DLL *)
  689. (***********************************************************************)
  690. (* StdMem$AllocNear - Standard Near Heap Memory Allocation *)
  691. (* *)
  692. (* PROCEDURE AllocNear(Size:CARDINAL):NearADDRESS *)
  693. (* *)
  694. (* All Memory Models *)
  695. (* *)
  696. (* Internal Procedure *)
  697. (* Parameters: *)
  698. (* ax - Allocation Size Request *)
  699. (* Trashes: *)
  700. (* none *)
  701. (* Return: *)
  702. (* ax - Allocation Address *)
  703. (* Aliases: *)
  704. (* StdMem$AllocMem in Near Pointer Models *)
  705. (* *)
  706. (***********************************************************************)
  707. segment _DATA(DATA,48H)
  708. segment _TEXT(CODE,48H)
  709. extrn @NearHeapBase
  710. extrn @NearHeapAvail
  711. extrn $AllocHeap
  712. (*%F _fptr *)
  713. public StdMem$AllocMem :
  714. (*%E *)
  715. public StdMem$AllocNear :
  716. push bx
  717. push dx
  718. push di
  719. (*%T _fdata *)
  720. push ds
  721. mov dx, _DATA
  722. mov ds, dx
  723. (*%E *)
  724. (*%F _fdata *)
  725. mov dx, ds
  726. (*%E *)
  727. add ax, 3
  728. and al, 0FCH
  729. push ax
  730. mov di, [@NearHeapBase]
  731. cmp ax, [di][memLargestFree]
  732. ja $$NoMemory
  733. call $AllocHeap
  734. pop bx
  735. sub [@NearHeapAvail], bx
  736. $$AllocExit:
  737. (*%T _fdata *)
  738. pop ds
  739. (*%E *)
  740. pop di
  741. pop dx
  742. pop bx
  743. (*%T _fcall *)
  744. ret far 0
  745. (*%E *)
  746. (*%F _fcall *)
  747. ret 0
  748. (*%E *)
  749. $$NoMemory:
  750. pop ax
  751. xor ax, ax
  752. jmp $$AllocExit
  753. (*%E _DLL *)
  754. section
  755. (*%F _DLL *)
  756. (***********************************************************************)
  757. (* StdMem$ResizeNear - Standard Near Heap Memory Reallocation *)
  758. (* *)
  759. (* PROCEDURE ResizeNear(Addr:NearADDRESS;OldSize,NewSize:CARDINAL):BOOLEAN*)
  760. (* *)
  761. (* All Memory Models *)
  762. (* *)
  763. (* Internal Procedure *)
  764. (* Parameters: *)
  765. (* ax - Reallocation Address *)
  766. (* bx - Original Block Size *)
  767. (* cx - Requested Block Size *)
  768. (* Trashes: *)
  769. (* es *)
  770. (* Return: *)
  771. (* ax - New Reallocation Address (0 = Failed) *)
  772. (* Aliases: *)
  773. (* StdMem$ResizeMem in Near Pointer Models *)
  774. (* *)
  775. (***********************************************************************)
  776. segment _DATA(DATA,48H)
  777. segment _TEXT(CODE,48H)
  778. extrn @NearHeapBase
  779. extrn @NearHeapAvail
  780. extrn $ResizeHeap
  781. (*%F _fptr *)
  782. public StdMem$ResizeMem :
  783. (*%E *)
  784. public StdMem$ResizeNear :
  785. push dx
  786. (*%T _fdata *)
  787. push ds
  788. mov dx, _DATA
  789. mov ds, dx
  790. (*%E *)
  791. add bx, 3 (* Round Sizes up to nearest 4 *)
  792. and bl, 0FCH
  793. add cx, 3
  794. and cl, 0FCH
  795. cmp bx, cx
  796. je $$Return
  797. push cx
  798. push bx
  799. xchg ax, bx
  800. mov dx, cx
  801. mov cx, [@NearHeapBase]
  802. call $ResizeHeap
  803. or ax, ax
  804. jz $$ResizeFailed
  805. pop ax
  806. add [@NearHeapAvail], ax
  807. pop ax
  808. sub [@NearHeapAvail], ax
  809. mov ax, bx
  810. $$Return:
  811. (*%T _fdata *)
  812. pop ds
  813. (*%E *)
  814. pop dx
  815. (*%T _fcall *)
  816. ret far 0
  817. (*%E *)
  818. (*%F _fcall *)
  819. ret 0
  820. (*%E *)
  821. $$ResizeFailed:
  822. add sp, 4
  823. jmp $$Return
  824. (*%E _DLL *)
  825. section
  826. (*%F _DLL *)
  827. (***********************************************************************)
  828. (* StdMem$ReallocNear - Standard Near Heap Memory Reallocation *)
  829. (* *)
  830. (* PROCEDURE ReallocNear(Addr:NearADDRESS;OldSize,NewSize:CARDINAL):BOOLEAN*)
  831. (* *)
  832. (* All Memory Models *)
  833. (* *)
  834. (* Internal Procedure *)
  835. (* Parameters: *)
  836. (* ax - Reallocation Address *)
  837. (* bx - Original Block Size *)
  838. (* cx - Requested Block Size *)
  839. (* Trashes: *)
  840. (* none *)
  841. (* Return: *)
  842. (* ax - New Reallocation Address (0 = Failed) *)
  843. (* Aliases: *)
  844. (* StdMem$ReallocMem in Near Pointer Models *)
  845. (* *)
  846. (***********************************************************************)
  847. segment _DATA(DATA,48H)
  848. segment _TEXT(CODE,48H)
  849. extrn @NearHeapBase
  850. extrn @NearHeapAvail
  851. extrn $ReallocHeap
  852. (*%F _fptr *)
  853. public StdMem$ReallocMem :
  854. (*%E *)
  855. public StdMem$ReallocNear :
  856. push dx
  857. (*%T _fdata *)
  858. push ds
  859. mov si, _DATA
  860. mov ds, si
  861. (*%E *)
  862. add bx, 3 (* Round Sizes up to nearest 4 *)
  863. and bl, 0FCH
  864. add cx, 3
  865. and cl, 0FCH
  866. cmp bx, cx
  867. je $$Return
  868. push cx
  869. push bx
  870. xchg ax, bx
  871. mov dx, cx
  872. mov cx, [@NearHeapBase]
  873. call $ReallocHeap
  874. or ax, ax
  875. jz $$ResizeFailed
  876. pop ax
  877. add [@NearHeapAvail], ax
  878. pop ax
  879. sub [@NearHeapAvail], ax
  880. mov ax, bx
  881. $$Return:
  882. (*%T _fcall *)
  883. pop ds
  884. (*%E *)
  885. pop dx
  886. (*%T _fcall *)
  887. ret far 0
  888. (*%E *)
  889. (*%F _fcall *)
  890. ret 0
  891. (*%E *)
  892. $$ResizeFailed:
  893. add sp, 4
  894. jmp $$Return
  895. (*%E _DLL *)
  896. section
  897. (*%F _DLL *)
  898. (***********************************************************************)
  899. (* StdMem$DeallocNear - Standard Near Heap Memory Deallocation *)
  900. (* *)
  901. (* PROCEDURE DeallocNear(Addr:NearADDRESS;Size:CARDINAL):BOOLEAN *)
  902. (* *)
  903. (* All Memory Models *)
  904. (* *)
  905. (* Internal Procedure *)
  906. (* Parameters: *)
  907. (* ax - Deallocation Address *)
  908. (* bx - Deallocation Size Request *)
  909. (* Trashes: *)
  910. (* none *)
  911. (* Return: *)
  912. (* ax - Success/Failure *)
  913. (* Aliases: *)
  914. (* StdMem$DeallocMem in Far Pointer Models *)
  915. (* *)
  916. (***********************************************************************)
  917. segment _DATA(DATA,48H)
  918. segment _TEXT(CODE,48H)
  919. extrn @NearHeapBase
  920. extrn @NearHeapAvail
  921. extrn $FreeHeap
  922. (*%F _fptr *)
  923. public StdMem$DeallocMem :
  924. (*%E *)
  925. public StdMem$DeallocNear :
  926. push cx
  927. xchg ax, bx
  928. (*%T _fdata *)
  929. push ds
  930. mov cx, _DATA
  931. mov ds, cx
  932. (*%E *)
  933. add ax, 3
  934. and al, 0FCH
  935. push ax
  936. mov cx, [@NearHeapBase]
  937. call $FreeHeap
  938. pop bx
  939. add [@NearHeapAvail], bx
  940. mov ax, 1
  941. (*%T _fdata *)
  942. pop ds
  943. (*%E *)
  944. pop cx
  945. (*%T _fcall *)
  946. ret far 0
  947. (*%E *)
  948. (*%F _fcall *)
  949. ret 0
  950. (*%E *)
  951. (*%E _DLL *)
  952. section
  953. (*%F _DLL *)
  954. (***********************************************************************)
  955. (* StdMem$AvailNear - Standard Near Heap Memory Available *)
  956. (* *)
  957. (* PROCEDURE AvailNear():CARDINAL *)
  958. (* *)
  959. (* All Memory Models *)
  960. (* *)
  961. (* Internal Procedure *)
  962. (* Parameters: *)
  963. (* none *)
  964. (* Trashes: *)
  965. (* none *)
  966. (* Return: *)
  967. (* ax - Near Heap Available (bytes) *)
  968. (* *)
  969. (***********************************************************************)
  970. segment _DATA(DATA,48H)
  971. segment _TEXT(CODE,48H)
  972. extrn @NearHeapAvail
  973. public StdMem$AvailNear :
  974. (*%T _fdata *)
  975. push ds
  976. mov ax, _DATA
  977. mov ds, ax
  978. (*%E *)
  979. mov ax, [@NearHeapAvail]
  980. (*%T _fdata *)
  981. pop ds
  982. (*%E *)
  983. (*%T _fcall *)
  984. ret far 0
  985. (*%E *)
  986. (*%F _fcall *)
  987. ret 0
  988. (*%E *)
  989. (*%E _DLL *)
  990. section
  991. (*%F _DLL *)
  992. (***********************************************************************)
  993. (* StdMem$LargestNear - Standard Near Heap Memory Largest Block *)
  994. (* Available. *)
  995. (* *)
  996. (* PROCEDURE LargestNear():CARDINAL *)
  997. (* *)
  998. (* All Memory Models *)
  999. (* *)
  1000. (* Internal Procedure *)
  1001. (* Parameters: *)
  1002. (* none *)
  1003. (* Trashes: *)
  1004. (* none *)
  1005. (* Return: *)
  1006. (* ax - Near Heap Largest Block Available (bytes) *)
  1007. (* *)
  1008. (***********************************************************************)
  1009. segment _DATA(DATA,48H)
  1010. segment _TEXT(CODE,48H)
  1011. extrn @NearHeapBase
  1012. public StdMem$LargestNear :
  1013. push bx
  1014. (*%T _fdata *)
  1015. push ds
  1016. mov ax, _DATA
  1017. mov ds, ax
  1018. (*%E *)
  1019. mov bx, [@NearHeapBase]
  1020. mov ax, [bx][memLargestFree]
  1021. (*%T _fdata *)
  1022. pop ds
  1023. (*%E *)
  1024. pop bx
  1025. (*%T _fcall *)
  1026. ret far 0
  1027. (*%E *)
  1028. (*%F _fcall *)
  1029. ret 0
  1030. (*%E *)
  1031. (*%E _DLL *)
  1032. section
  1033. (*%F _DLL *)
  1034. (***********************************************************************)
  1035. (* StdMem$MarkNear - Pascal Near Heap Memory Marker *)
  1036. (* *)
  1037. (* PROCEDURE MarkNear(VAR p:ADDRESS) *)
  1038. (* *)
  1039. (* All Memory Models *)
  1040. (* *)
  1041. (* Internal Procedure *)
  1042. (* Parameters: *)
  1043. (* ax - pointer to mark *)
  1044. (* Trashes: *)
  1045. (* none *)
  1046. (* Return: *)
  1047. (* none *)
  1048. (* Aliases: *)
  1049. (* TURBOSYS$MARK - in Near Models *)
  1050. (* *)
  1051. (***********************************************************************)
  1052. segment _DATA(DATA,48H)
  1053. segment _TEXT(CODE,48H)
  1054. extrn $MarkHeap
  1055. extrn @NearHeapBase
  1056. (*%F _fptr *)
  1057. public TURBOSYS$MARK :
  1058. (*%E *)
  1059. public StdMem$MarkNear :
  1060. push bx
  1061. (*%T _fdata *)
  1062. push ds
  1063. mov bx, _DATA
  1064. mov ds, bx
  1065. (*%E *)
  1066. mov bx, [@NearHeapBase]
  1067. call $MarkHeap
  1068. xchg ax, bx
  1069. mov [bx], ax
  1070. (*%T _fdata *)
  1071. pop ds
  1072. (*%E *)
  1073. pop bx
  1074. (*%T _fcall *)
  1075. ret far 0
  1076. (*%E *)
  1077. (*%F _fcall *)
  1078. ret 0
  1079. (*%E *)
  1080. (*%E _DLL *)
  1081. section
  1082. (*%F _DLL *)
  1083. (***********************************************************************)
  1084. (* StdMem$ReleaseNear - Pascal Near Heap Memory Mark Releaser *)
  1085. (* *)
  1086. (* PROCEDURE ReleaseNear(VAR p:ADDRESS) *)
  1087. (* *)
  1088. (* All Memory Models *)
  1089. (* *)
  1090. (* Internal Procedure *)
  1091. (* Parameters: *)
  1092. (* ax - pointer to mark *)
  1093. (* Trashes: *)
  1094. (* none *)
  1095. (* Return: *)
  1096. (* none *)
  1097. (* Aliases: *)
  1098. (* TURBOSYS$RELEASE - in Near models *)
  1099. (* *)
  1100. (***********************************************************************)
  1101. segment _DATA(DATA,48H)
  1102. segment _TEXT(CODE,48H)
  1103. extrn $ReleaseHeap
  1104. extrn @NearHeapBase
  1105. extrn @NearHeapAvail
  1106. (*%F _fptr *)
  1107. public TURBOSYS$RELEASE :
  1108. (*%E *)
  1109. public StdMem$ReleaseNear :
  1110. push bx
  1111. (*%T _fdata *)
  1112. push ds
  1113. mov bx, _DATA
  1114. mov ds, bx
  1115. (*%E *)
  1116. mov bx, ax
  1117. mov ax, [bx]
  1118. mov bx, [@NearHeapBase]
  1119. call $ReleaseHeap
  1120. mov [@NearHeapAvail], bx
  1121. (*%T _fdata *)
  1122. pop ds
  1123. (*%E *)
  1124. pop bx
  1125. (*%T _fcall *)
  1126. ret far 0
  1127. (*%E *)
  1128. (*%F _fcall *)
  1129. ret 0
  1130. (*%E *)
  1131. (*%E _DLL *)
  1132. section
  1133. (***********************************************************************)
  1134. (* StdMem$InitFar - Standard Module Far Heap Initialization *)
  1135. (* *)
  1136. (* OS/2 and Extender Memory Models *)
  1137. (* *)
  1138. (* Automatic Initialization Procedure *)
  1139. (* Brought in by reference to @memSelTable *)
  1140. (* *)
  1141. (***********************************************************************)
  1142. segment _DATA(DATA,48H)
  1143. segment _INIT(INIT_CODE,28H)
  1144. segment _TEXT(CODE,48H)
  1145. extrn $InitHeap
  1146. extrn __FatalError
  1147. extrn __FatalErrorPos
  1148. extrn DOSALLOCSEG
  1149. extrn DOSGETHUGESHIFT
  1150. extrn StdMem@HugeShift
  1151. select _TEXT
  1152. $$DoInit:
  1153. select _INIT
  1154. Init: dw INIT_MAGIC
  1155. db initPri1
  1156. (*%T _fcall *)
  1157. dd $$DoInit
  1158. (*%E *)
  1159. (*%F _fcall *)
  1160. dw $$DoInit
  1161. (*%E *)
  1162. select _DATA
  1163. public @memSelTable : dw 0, Init
  1164. select _TEXT
  1165. public StdMem$InitFar :
  1166. push ax
  1167. push di
  1168. push ds
  1169. (*%T _fdata *)
  1170. mov ax, _DATA
  1171. mov ds, ax
  1172. (*%E *)
  1173. (* Save Huge Shift Value *)
  1174. push ds
  1175. db 068H; dw StdMem@HugeShift (*I push word immed *)
  1176. call far DOSGETHUGESHIFT
  1177. mov cx, [StdMem@HugeShift]
  1178. mov ax, 1
  1179. shl ax, cl
  1180. mov [StdMem@HugeShift], ax
  1181. (* Far Heap Initialization - Allocate Selector Table *)
  1182. db 068H; dw (memEntrySize * memInitEntries) (*I push word immed *)
  1183. push ds
  1184. db 068H; dw @memSelTable (*I push word immed *)
  1185. db 06AH, 000H (*I push 0 *)
  1186. call far DOSALLOCSEG
  1187. or ax, ax
  1188. jne $$InitAllocFailed
  1189. (* Allocate First Segment & Initialize Selector Table *)
  1190. mov ds, [@memSelTable]
  1191. push ax (* ax = 0 *)
  1192. push ds
  1193. db 06AH, memSelector (*I push 2 *)
  1194. push ax
  1195. call far DOSALLOCSEG
  1196. or ax, ax
  1197. jne $$InitAllocFailed
  1198. mov [memTerminator], ax (* Terminate Table *)
  1199. mov es, [memSelector]
  1200. xor di, di
  1201. call $InitHeap
  1202. mov [memLargest], ax
  1203. pop ds
  1204. pop di
  1205. pop ax
  1206. (*%T _fcall *)
  1207. ret far 0
  1208. (*%E *)
  1209. (*%F _fcall *)
  1210. ret 0
  1211. (*%E *)
  1212. $$InitAllocFailed:
  1213. call far __FatalErrorPos
  1214. mov cx, ERROR_MEMINIT
  1215. push cx
  1216. push dx
  1217. push ax
  1218. call far __FatalError
  1219. section
  1220. (***********************************************************************)
  1221. (* StdMem$AllocFar - Standard Far Heap Memory Allocation *)
  1222. (* *)
  1223. (* PROCEDURE AllocFar(Size:CARDINAL):FarADDRESS *)
  1224. (* *)
  1225. (* All Memory Models *)
  1226. (* *)
  1227. (* Internal Procedure *)
  1228. (* Parameters: *)
  1229. (* ax - Allocation Size Request *)
  1230. (* Trashes: *)
  1231. (* es *)
  1232. (* Return: *)
  1233. (* dx:ax - Allocation Address *)
  1234. (* Aliases: *)
  1235. (* StdMem$AllocMem in Far Pointer Models *)
  1236. (* *)
  1237. (***********************************************************************)
  1238. segment _DATA(DATA,48H)
  1239. segment _TEXT(CODE,48H)
  1240. extrn @memSelTable
  1241. extrn $InitHeap
  1242. extrn $AllocHeap
  1243. extrn DOSALLOCSEG
  1244. (*%T _fptr *)
  1245. public StdMem$AllocMem :
  1246. (*%E *)
  1247. public StdMem$AllocFar :
  1248. push ds
  1249. push bx
  1250. push di
  1251. push si
  1252. (*%T _fdata *)
  1253. mov si, _DATA
  1254. mov ds, si
  1255. (*%E *)
  1256. mov ds, [@memSelTable]
  1257. xor si, si
  1258. cmp ax, si
  1259. jz $$ScanStart
  1260. add ax, 3 (* Round Size up to nearest 4 *)
  1261. and al, 0FCH
  1262. cmp ax, memThreshold
  1263. jb $$AllocStart
  1264. jmp $$ScanStart
  1265. $$ScanTable:
  1266. add si, memEntrySize
  1267. $$ScanStart:
  1268. cmp word [si][memSelector], 0
  1269. jnz $$ScanTable
  1270. xor di, di
  1271. mov [si][memLargest], di
  1272. mov [si][memTerminator], di (* Terminate List *)
  1273. mov di, ax
  1274. push ax
  1275. push ds
  1276. lea ax, [si][memSelector]
  1277. push ax
  1278. db 06AH, 000H (*I push 0 *)
  1279. call far DOSALLOCSEG
  1280. mov dx, [si][memSelector]
  1281. or ax, ax
  1282. je $$AllocExit
  1283. mov [si][memSelector], ax (* Failed, Re-terminate List *)
  1284. mov ax, di
  1285. xor si, si
  1286. jmp $$AllocStart
  1287. $$AllocLoop:
  1288. add si, memEntrySize
  1289. cmp word [si][memSelector], 0
  1290. jz $$BlockNotFound
  1291. $$AllocStart:
  1292. cmp ax, [si][memLargest]
  1293. ja $$AllocLoop
  1294. $$BlockFound:
  1295. mov dx, [si][memSelector]
  1296. xor di, di
  1297. call $AllocHeap
  1298. or ax, ax
  1299. jz $$AllocFailed
  1300. mov [si][memLargest], bx
  1301. jmp $$AllocExit
  1302. $$BlockNotFound:
  1303. push ax
  1304. db 06AH, 000H (*I push 0 *)
  1305. push ds
  1306. lea ax, [si][memSelector]
  1307. push ax
  1308. db 06AH, 000H (*I push 0 *)
  1309. call far DOSALLOCSEG
  1310. or ax, ax
  1311. jne $$AllocFailedPop
  1312. mov [si][memTerminator], ax (* Terminate Table *)
  1313. mov es, [si][memSelector]
  1314. xor di, di
  1315. call $InitHeap
  1316. mov [si][memLargest], ax
  1317. pop ax
  1318. jmp $$BlockFound
  1319. $$AllocExit:
  1320. pop si
  1321. pop di
  1322. pop bx
  1323. pop ds
  1324. (*%T _fcall *)
  1325. ret far 0
  1326. (*%E *)
  1327. (*%F _fcall *)
  1328. ret 0
  1329. (*%E *)
  1330. $$AllocFailedPop:
  1331. pop ax
  1332. $$AllocFailed:
  1333. xor ax, ax
  1334. cwd
  1335. jmp $$AllocExit
  1336. section
  1337. (***********************************************************************)
  1338. (* StdMem$ResizeFar - Standard Far Heap Memory Reallocation *)
  1339. (* *)
  1340. (* PROCEDURE ResizeFar(Addr:FarADDRESS;OldSize,NewSize:CARDINAL):BOOLEAN*)
  1341. (* *)
  1342. (* All Memory Models *)
  1343. (* *)
  1344. (* Internal Procedure *)
  1345. (* Parameters: *)
  1346. (* bx:ax - Reallocation Address *)
  1347. (* cx - Original Block Size *)
  1348. (* dx - Requested Block Size *)
  1349. (* Trashes: *)
  1350. (* es *)
  1351. (* Return: *)
  1352. (* dx:ax - New Reallocation Address (0:0 = Failed) *)
  1353. (* Aliases: *)
  1354. (* StdMem$ResizeMem in Far Pointer Models *)
  1355. (* *)
  1356. (***********************************************************************)
  1357. segment _DATA(DATA,48H)
  1358. segment _TEXT(CODE,48H)
  1359. extrn @memSelTable
  1360. extrn $ResizeHeap
  1361. (*%T _fptr *)
  1362. public StdMem$ResizeMem :
  1363. (*%E *)
  1364. public StdMem$ResizeFar :
  1365. push ds
  1366. push si
  1367. (*%T _fdata *)
  1368. mov si, _DATA
  1369. mov ds, si
  1370. (*%E *)
  1371. mov ds, [@memSelTable]
  1372. xor si, si
  1373. add cx, 3 (* Round Sizes up to nearest 4 *)
  1374. and cl, 0FCH
  1375. add dx, 3
  1376. and dl, 0FCH
  1377. cmp cx, dx
  1378. jne $$ResizeStart
  1379. mov dx, bx
  1380. jmp $$Return
  1381. $$ResizeLoop:
  1382. add si, memEntrySize
  1383. cmp word [si][memSelector], 0
  1384. jz $$HeapCorrupt
  1385. $$ResizeStart:
  1386. cmp [si][memSelector], bx
  1387. jne $$ResizeLoop
  1388. $$SelectorFound:
  1389. push ds
  1390. push bx
  1391. mov ds, bx
  1392. mov bx, ax
  1393. mov ax, cx
  1394. xor cx, cx
  1395. call $ResizeHeap
  1396. pop dx
  1397. pop ds
  1398. or ax, ax
  1399. jz $$ResizeFailed
  1400. mov [si][memLargest], ax
  1401. mov ax, bx
  1402. $$Return:
  1403. pop si
  1404. pop ds
  1405. (*%T _fcall *)
  1406. ret far 0
  1407. (*%E *)
  1408. (*%F _fcall *)
  1409. ret 0
  1410. (*%E *)
  1411. $$HeapCorrupt:
  1412. xor ax, ax
  1413. $$ResizeFailed:
  1414. cwd
  1415. jmp $$Return
  1416. section
  1417. (***********************************************************************)
  1418. (* StdMem$ReallocFar - Standard Far Heap Memory Reallocation *)
  1419. (* *)
  1420. (* PROCEDURE ReallocFar(Addr:FarADDRESS;OldSize,NewSize:CARDINAL):BOOLEAN*)
  1421. (* *)
  1422. (* All Memory Models *)
  1423. (* *)
  1424. (* Internal Procedure *)
  1425. (* Parameters: *)
  1426. (* bx:ax - Reallocation Address *)
  1427. (* cx - Original Block Size *)
  1428. (* dx - Requested Block Size *)
  1429. (* Trashes: *)
  1430. (* none *)
  1431. (* Return: *)
  1432. (* dx:ax - New Reallocation Address (0:0 = Failed) *)
  1433. (* Aliases: *)
  1434. (* StdMem$ResizeMem in Far Pointer Models *)
  1435. (* *)
  1436. (***********************************************************************)
  1437. segment _DATA(DATA,48H)
  1438. segment _TEXT(CODE,48H)
  1439. extrn @memSelTable
  1440. extrn $ReallocHeap
  1441. (*%T _fptr *)
  1442. public StdMem$ReallocMem :
  1443. (*%E *)
  1444. public StdMem$ReallocFar :
  1445. push ds
  1446. push si
  1447. (*%T _fdata *)
  1448. mov si, _DATA
  1449. mov ds, si
  1450. (*%E *)
  1451. mov ds, [@memSelTable]
  1452. xor si, si
  1453. add cx, 3 (* Round Sizes up to nearest 4 *)
  1454. and cl, 0FCH
  1455. add dx, 3
  1456. and dl, 0FCH
  1457. cmp cx, dx
  1458. jne $$ResizeStart
  1459. mov dx, bx
  1460. jmp $$Return
  1461. $$ResizeLoop:
  1462. add si, memEntrySize
  1463. cmp word [si][memSelector], 0
  1464. jz $$HeapCorrupt
  1465. $$ResizeStart:
  1466. cmp [si][memSelector], bx
  1467. jne $$ResizeLoop
  1468. $$SelectorFound:
  1469. push ds
  1470. push bx
  1471. mov ds, bx
  1472. mov bx, ax
  1473. mov ax, cx
  1474. xor cx, cx
  1475. call $ReallocHeap
  1476. pop dx
  1477. pop ds
  1478. or ax, ax
  1479. jz $$ResizeFailed
  1480. mov [si][memLargest], ax
  1481. mov ax, bx
  1482. $$Return:
  1483. pop si
  1484. pop ds
  1485. (*%T _fcall *)
  1486. ret far 0
  1487. (*%E *)
  1488. (*%F _fcall *)
  1489. ret 0
  1490. (*%E *)
  1491. $$HeapCorrupt:
  1492. xor ax, ax
  1493. $$ResizeFailed:
  1494. cwd
  1495. jmp $$Return
  1496. section
  1497. (***********************************************************************)
  1498. (* $FreeSelector - Free an Empty Segment back to the Operating System *)
  1499. (* *)
  1500. (* All Memory Models *)
  1501. (* *)
  1502. (* Internal Procedure - Near Call *)
  1503. (* Parameters: *)
  1504. (* bx - Deallocation Segment *)
  1505. (* Trashes: *)
  1506. (* es *)
  1507. (* Return: *)
  1508. (* none *)
  1509. (* *)
  1510. (***********************************************************************)
  1511. segment _TEXT(CODE,48H)
  1512. extrn __FatalError
  1513. extrn __FatalErrorPos
  1514. extrn DOSFREESEG
  1515. public $FreeSelector:
  1516. push ax
  1517. push cx
  1518. push si
  1519. push di
  1520. push bx
  1521. call far DOSFREESEG
  1522. or ax, ax
  1523. jnz $$HeapCorrupt
  1524. mov cx, ds
  1525. mov es, cx
  1526. $$ScanTable:
  1527. cmp [si][memSelector], bx
  1528. jz $$EntryFound
  1529. add si, memEntrySize
  1530. jmp $$ScanTable
  1531. $$EntryFound:
  1532. xor cx, cx
  1533. mov di, si
  1534. $$ScanEnd:
  1535. add di, memEntrySize
  1536. add cx, memEntrySize
  1537. cmp word [di][memSelector], 0
  1538. jnz $$ScanEnd
  1539. mov di, si
  1540. add si, memEntrySize
  1541. rep; movsb
  1542. $$FreeExit:
  1543. pop di
  1544. pop si
  1545. pop cx
  1546. pop ax
  1547. ret 0
  1548. $$HeapCorrupt:
  1549. call far __FatalErrorPos
  1550. mov cx, ERROR_HEAPCORRUPT
  1551. push cx
  1552. push dx
  1553. push ax
  1554. call far __FatalError
  1555. section
  1556. (***********************************************************************)
  1557. (* StdMem$DeallocFar - Standard Far Heap Memory Deallocation *)
  1558. (* *)
  1559. (* PROCEDURE DeallocFar(Addr:FarADDRESS;Size:CARDINAL):BOOLEAN *)
  1560. (* *)
  1561. (* All Memory Models *)
  1562. (* *)
  1563. (* Internal Procedure *)
  1564. (* Parameters: *)
  1565. (* bx:ax - Deallocation Address *)
  1566. (* cx - Deallocation Size Request *)
  1567. (* Trashes: *)
  1568. (* none *)
  1569. (* Return: *)
  1570. (* ax - Success/Failure *)
  1571. (* Aliases: *)
  1572. (* StdMem$DeallocMem in Far Pointer Models *)
  1573. (* *)
  1574. (***********************************************************************)
  1575. segment _DATA(DATA,48H)
  1576. segment _TEXT(CODE,48H)
  1577. extrn @memSelTable
  1578. extrn $FreeHeap
  1579. extrn $FreeSelector
  1580. (*%T _fptr *)
  1581. public StdMem$DeallocMem :
  1582. (*%E *)
  1583. public StdMem$DeallocFar :
  1584. push ds
  1585. push si
  1586. (*%T _fdata *)
  1587. mov si, _DATA
  1588. mov ds, si
  1589. (*%E *)
  1590. mov ds, [@memSelTable]
  1591. xor si, si
  1592. add cx, 3
  1593. and cl, 0FCH (* Round Size up to nearest 4 *)
  1594. or ax, ax
  1595. jnz $$DeallocStart
  1596. call $FreeSelector
  1597. jmp $$DeallocExit
  1598. $$DeallocLoop:
  1599. add si, memEntrySize
  1600. cmp word [si][memSelector], 0
  1601. jz $$HeapCorrupt
  1602. $$DeallocStart:
  1603. cmp [si][memSelector], bx
  1604. jne $$DeallocLoop
  1605. $$SelectorFound:
  1606. push ds
  1607. push bx
  1608. mov ds, bx
  1609. mov bx, ax
  1610. mov ax, cx
  1611. xor cx, cx
  1612. call $FreeHeap
  1613. pop bx
  1614. pop ds
  1615. mov [si][memLargest], ax
  1616. or si, si
  1617. jz $$DeallocExit
  1618. cmp ax, memSegEmpty
  1619. jae $$SegEmpty
  1620. $$DeallocExit:
  1621. mov ax, TRUE
  1622. $$DeallocRet:
  1623. pop si
  1624. pop ds
  1625. (*%T _fcall *)
  1626. ret far 0
  1627. (*%E *)
  1628. (*%F _fcall *)
  1629. ret 0
  1630. (*%E *)
  1631. $$SegEmpty:
  1632. call $FreeSelector
  1633. jmp $$DeallocExit
  1634. $$HeapCorrupt:
  1635. xor ax, ax
  1636. jmp $$DeallocRet
  1637. section
  1638. (***********************************************************************)
  1639. (* StdMem$MarkFar - Pascal Far Heap Memory Marker *)
  1640. (* *)
  1641. (* PROCEDURE MarkFar(VAR p:ADDRESS) *)
  1642. (* *)
  1643. (* All Memory Models *)
  1644. (* *)
  1645. (* Internal Procedure *)
  1646. (* Parameters: *)
  1647. (* bx:ax - pointer to mark *)
  1648. (* Trashes: *)
  1649. (* es *)
  1650. (* Return: *)
  1651. (* none *)
  1652. (* Aliases: *)
  1653. (* TURBOSYS$MARK - in Far Models *)
  1654. (* *)
  1655. (***********************************************************************)
  1656. segment _DATA(DATA,48H)
  1657. segment _TEXT(CODE,48H)
  1658. extrn $MarkHeap
  1659. extrn @memSelTable
  1660. (*%T _fptr *)
  1661. public TURBOSYS$MARK :
  1662. (*%E *)
  1663. public StdMem$MarkFar :
  1664. push si
  1665. push ds
  1666. (*%T _fdata *)
  1667. mov si, _DATA
  1668. mov ds, si
  1669. (*%E *)
  1670. mov es, bx
  1671. mov bx, ax
  1672. mov ds, [@memSelTable]
  1673. xor si, si
  1674. jmp $$FindSelStart
  1675. $$FindSelector:
  1676. add si, memEntrySize
  1677. $$FindSelStart:
  1678. cmp word [si][memTerminator], 0
  1679. jnz $$FindSelector
  1680. mov si, [si][memSelector]
  1681. mov es:[bx][2], si
  1682. mov ds, si
  1683. mov si, bx
  1684. xor bx, bx
  1685. call $MarkHeap
  1686. mov es:[si], bx
  1687. pop ds
  1688. pop si
  1689. (*%T _fcall *)
  1690. ret far 0
  1691. (*%E *)
  1692. (*%F _fcall *)
  1693. ret 0
  1694. (*%E *)
  1695. section
  1696. (***********************************************************************)
  1697. (* StdMem$ReleaseFar - Pascal Far Heap Memory Mark Releaser *)
  1698. (* *)
  1699. (* PROCEDURE ReleaseFar(VAR p:ADDRESS) *)
  1700. (* *)
  1701. (* All Memory Models *)
  1702. (* *)
  1703. (* Internal Procedure *)
  1704. (* Parameters: *)
  1705. (* bx:ax - pointer to mark *)
  1706. (* Trashes: *)
  1707. (* es *)
  1708. (* Return: *)
  1709. (* none *)
  1710. (* Aliases: *)
  1711. (* TURBOSYS$RELEASE - in Far models *)
  1712. (* *)
  1713. (***********************************************************************)
  1714. segment _DATA(DATA,48H)
  1715. segment _TEXT(CODE,48H)
  1716. extrn $ReleaseHeap
  1717. extrn $FreeSelector
  1718. extrn @memSelTable
  1719. (*%T _fptr *)
  1720. public TURBOSYS$RELEASE :
  1721. (*%E *)
  1722. public StdMem$ReleaseFar :
  1723. push ds
  1724. push si
  1725. mov es, bx (* es:si = Pointer to Mark *)
  1726. mov si, ax
  1727. lds ax, es:[si] (* ds:ax = Mark *)
  1728. mov si, ds (* Save Mark Segment in si *)
  1729. xor bx, bx (* bx = 0 (heap base) *)
  1730. call $ReleaseHeap (* Release Heaplette *)
  1731. mov bx, _DATA
  1732. mov ds, bx
  1733. mov ds, [@memSelTable] (* ds:bx = Selector Table Start *)
  1734. xor bx, bx
  1735. jmp $$FindSelStart
  1736. $$FindSelector: (* Find Mark Selector *)
  1737. add bx, memEntrySize
  1738. $$FindSelStart:
  1739. cmp si, [bx][memSelector]
  1740. jnz $$FindSelector
  1741. mov [bx][memLargest], ax (* Update Largest Free Block *)
  1742. mov si, bx
  1743. $$FindEnd: (* Free any segments allocated *)
  1744. add si, memEntrySize (* Since Mark *)
  1745. mov bx, [si][memSelector]
  1746. or bx, bx
  1747. jz $$Return
  1748. call $FreeSelector
  1749. mov word [si][memSelector], 0
  1750. jmp $$FindEnd
  1751. $$Return:
  1752. pop si
  1753. pop ds
  1754. (*%T _fcall *)
  1755. ret far 0
  1756. (*%E *)
  1757. (*%F _fcall *)
  1758. ret 0
  1759. (*%E *)
  1760. section
  1761. (***********************************************************************)
  1762. (* StdMem$AllocHuge - Standard Far Heap Allocation for larger than *)
  1763. (* One segment allocations *)
  1764. (* *)
  1765. (* PROCEDURE AllocHuge(Para:CARDINAL):FarADDRESS *)
  1766. (* *)
  1767. (* OS/2 & Extender Memory Models *)
  1768. (* *)
  1769. (* Internal Procedure *)
  1770. (* Parameters: *)
  1771. (* bx:ax - Number of paragraphs to allocate *)
  1772. (* Trashes: *)
  1773. (* es *)
  1774. (* Return: *)
  1775. (* dx:ax - Far Pointer to memory *)
  1776. (* *)
  1777. (***********************************************************************)
  1778. segment _TEXT(CODE,48H)
  1779. extrn DOSALLOCHUGE
  1780. extrn __MaxReallocHugeSegments
  1781. public StdMem$AllocHuge:
  1782. mov dx,bx (* test for null request *)
  1783. or dx,ax
  1784. jz Done (* null request *)
  1785. cmp bx,1000H (* too many paragraphs ? *)
  1786. jae Fail (* yes *)
  1787. add ax,ax;adc bx,bx (* convert to bytes *)
  1788. add ax,ax;adc bx,bx
  1789. add ax,ax;adc bx,bx
  1790. add ax,ax;adc bx,bx
  1791. push ax (* space for selector return area *)
  1792. mov dx,sp (* address of selector return area *)
  1793. push bx (* args for DOSALLOCHUGE. 64k's of reqest *)
  1794. push ax (* remainder of request *)
  1795. push ss (* segment of selector return area *)
  1796. push dx (* offset of selector return area *)
  1797. (*%F SameDS *)
  1798. mov dx,seg __MaxReallocHugeSegments
  1799. mov es,dx
  1800. push es:[__MaxReallocHugeSegments]
  1801. (*%E NOT SameDS *)
  1802. (*%T SameDS *)
  1803. push [__MaxReallocHugeSegments]
  1804. (*%E SameDS *)
  1805. db 06AH, 000H (*I push 0 *) (* non share,non discardable *)
  1806. call far DOSALLOCHUGE
  1807. or ax, ax (* did allocation fail ? *)
  1808. pop dx (* returned selector *)
  1809. jz Done (* no problem *)
  1810. Fail: xor ax,ax (* allocation failed-return NULL *)
  1811. cwd
  1812. Done: db Ret0@
  1813. section
  1814. (***********************************************************************)
  1815. (* StdMem$DeallocHuge - Standard Far Heap Deallocation for blocks *)
  1816. (* Allocated with StdMem$AllocHuge *)
  1817. (* *)
  1818. (* PROCEDURE DeallocHuge(Addr:FarADDRESS):BOOLEAN *)
  1819. (* *)
  1820. (* OS/2 & Extender Memory Models *)
  1821. (* *)
  1822. (* Internal Procedure *)
  1823. (* Parameters: *)
  1824. (* bx:ax - Far Pointer to memory block *)
  1825. (* Trashes: *)
  1826. (* none *)
  1827. (* Return: *)
  1828. (* ax - success/failure *)
  1829. (* *)
  1830. (***********************************************************************)
  1831. segment _TEXT(CODE,48H)
  1832. extrn DOSFREESEG
  1833. public StdMem$DeallocHuge :
  1834. push bx (* push segment *)
  1835. call far DOSFREESEG
  1836. neg ax (* set carry if failed *)
  1837. sbb ax,ax (* ax = 0 if success,ax =-1 if failed *)
  1838. inc ax (* ax = 1 if success,ax = 0 if failed *)
  1839. db Ret0@
  1840. (*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*)
  1841. (* *)
  1842. (*@ C Run-Time Memory Management @*)
  1843. (* *)
  1844. (*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*)
  1845. section
  1846. (*%F _DLL *)
  1847. (***********************************************************************)
  1848. (* __nmalloc - C Near Heap Allocation *)
  1849. (* *)
  1850. (* void near *_nmalloc(size_t size) *)
  1851. (* *)
  1852. (* All Memory Models + C Stack Convention *)
  1853. (* *)
  1854. (* Parameters: *)
  1855. (* ax - Request Size *)
  1856. (* Trashes: *)
  1857. (* none *)
  1858. (* Return: *)
  1859. (* ax - Address of Huge Allocation *)
  1860. (* Aliases: *)
  1861. (* malloc - in Near Pointer Models *)
  1862. (* *)
  1863. (***********************************************************************)
  1864. segment _TEXT(CODE,48H)
  1865. extrn StdMem$AllocNear
  1866. Size = Frame
  1867. (*%F _fptr *)
  1868. public _malloc :
  1869. (*%E *)
  1870. public __nmalloc :
  1871. (*%F _jpicall *)
  1872. push bp
  1873. mov bp, sp
  1874. mov ax, [bp][Size]
  1875. (*%E *)
  1876. (*%T _jpicall *)
  1877. push bx
  1878. (*%E *)
  1879. or ax, ax
  1880. jz $$Return
  1881. add ax, 2
  1882. jc $$TooBig
  1883. push ax
  1884. (*%T _fcall *)
  1885. push cs
  1886. (*%E *)
  1887. call StdMem$AllocNear
  1888. or ax, ax
  1889. jz $$Failed
  1890. mov bx, ax
  1891. pop [bx]
  1892. inc ax
  1893. inc ax
  1894. $$Return:
  1895. (*%T _jpicall *)
  1896. pop bx
  1897. (*%E *)
  1898. (*%F _jpicall *)
  1899. pop bp
  1900. (*%E *)
  1901. (*%T _fcall *)
  1902. ret far 0
  1903. (*%E *)
  1904. (*%F _fcall *)
  1905. ret 0
  1906. (*%E *)
  1907. $$TooBig:
  1908. xor ax, ax
  1909. jmp $$Return
  1910. $$Failed:
  1911. pop bx
  1912. jmp $$Return
  1913. (*%E _DLL *)
  1914. section
  1915. (*%F _DLL *)
  1916. (***********************************************************************)
  1917. (* __ncalloc - C Near Heap Initialized Allocation *)
  1918. (* *)
  1919. (* void near *_ncalloc(size_t number,size_t size) *)
  1920. (* *)
  1921. (* All Memory Models + C Stack Convention *)
  1922. (* *)
  1923. (* Internal Procedure *)
  1924. (* Parameters: *)
  1925. (* ax - Number of units requested *)
  1926. (* bx - Unit Size *)
  1927. (* Trashes: *)
  1928. (* es *)
  1929. (* Return: *)
  1930. (* ax - Offset Address of Allocation *)
  1931. (* *)
  1932. (***********************************************************************)
  1933. segment _DATA(DATA,28H)
  1934. segment _TEXT(CODE,48H)
  1935. extrn __nmalloc
  1936. Size = Frame + 2
  1937. Number = Frame
  1938. (*%F _fptr *)
  1939. public _calloc :
  1940. (*%E *)
  1941. public __ncalloc :
  1942. (*%F _jpicall *)
  1943. push bp
  1944. mov bp, sp
  1945. mov ax, [bp][Size]
  1946. mov bx, [bp][Number]
  1947. (*%E *)
  1948. push di
  1949. (*%T _jpicall *)
  1950. push cx
  1951. xchg ax, bx
  1952. mov cx, dx
  1953. (*%E *)
  1954. mul bx
  1955. (*%T _jpicall *)
  1956. mov dx, cx
  1957. (*%E *)
  1958. jo $$Invalid
  1959. or ax, ax
  1960. jz $$Invalid
  1961. (*%T _jpicall *)
  1962. mov cx, ax
  1963. (*%E *)
  1964. (*%F _jpicall *)
  1965. push ax
  1966. (*%E *)
  1967. (*%T _fcall *)
  1968. push cs
  1969. (*%E *)
  1970. call __nmalloc
  1971. (*%F _jpicall *)
  1972. pop cx
  1973. (*%E *)
  1974. mov bx, ax
  1975. or ax, ax
  1976. jz $$Return
  1977. (*%T SameDS *)
  1978. mov di, ds
  1979. (*%E SameDS *)
  1980. (*%F SameDS *)
  1981. mov di, _DATA
  1982. (*%E NOT SameDS *)
  1983. mov es, di
  1984. mov di, ax
  1985. xor ax, ax
  1986. shr cx, 1
  1987. rep; stosw
  1988. jnc $$ClearMem
  1989. stosb
  1990. $$ClearMem:
  1991. mov ax, bx
  1992. $$Return:
  1993. (*%T _jpicall *)
  1994. pop cx
  1995. (*%E *)
  1996. pop di
  1997. (*%F _jpicall *)
  1998. pop bp
  1999. (*%E *)
  2000. (*%T _fcall *)
  2001. ret far 0
  2002. (*%E *)
  2003. (*%F _fcall *)
  2004. ret 0
  2005. (*%E *)
  2006. $$Invalid:
  2007. xor ax, ax
  2008. jmp $$Return
  2009. (*%E _DLL *)
  2010. section
  2011. (*%F _DLL *)
  2012. (***********************************************************************)
  2013. (* __nrealloc - C Near Heap Change Allocation *)
  2014. (* *)
  2015. (* void near *_nrealloc(void near *buffer,size_t size) *)
  2016. (* *)
  2017. (* All Memory Models + C Stack Convention *)
  2018. (* *)
  2019. (* Internal Procedure *)
  2020. (* Parameters: *)
  2021. (* ax - Buffer address *)
  2022. (* bx - New Buffer Size *)
  2023. (* Trashes: *)
  2024. (* es *)
  2025. (* Return: *)
  2026. (* ax - New Buffer address *)
  2027. (* Aliases: *)
  2028. (* _realloc - in near pointer models *)
  2029. (* *)
  2030. (***********************************************************************)
  2031. segment _DATA(DATA,48H)
  2032. segment _TEXT(CODE,48H)
  2033. extrn __nmalloc
  2034. extrn __nfree
  2035. extrn StdMem$ResizeNear
  2036. extrn _errno
  2037. Size = Frame + 2
  2038. Buffer = Frame
  2039. (*%F _fptr *)
  2040. public _realloc :
  2041. (*%E *)
  2042. public __nrealloc :
  2043. (*%F _jpicall *)
  2044. push bp
  2045. mov bp, sp
  2046. mov ax, [bp][Buffer]
  2047. mov bx, [bp][Size]
  2048. (*%E *)
  2049. (*%T _jpicall *)
  2050. push cx
  2051. (*%E *)
  2052. push di
  2053. push si
  2054. (*%F SameDS *) (* set DS to _DATA *)
  2055. push ds
  2056. mov si,_DATA
  2057. mov ds,si
  2058. (*%E NOT SameDS *)
  2059. or ax, ax (* is Buffer = NIL *)
  2060. jnz $$GoodPtr
  2061. (*%T _jpicall *)
  2062. mov ax, bx (* if so, just malloc *)
  2063. (*%E *)
  2064. (*%F _jpicall *)
  2065. push bx
  2066. (*%E *)
  2067. (*%T _fcall *)
  2068. push cs
  2069. (*%E *)
  2070. call __nmalloc
  2071. (*%F _jpicall *)
  2072. inc sp
  2073. inc sp
  2074. (*%E *)
  2075. jmp $$Return
  2076. $$GoodPtr:
  2077. or bx, bx (* is size = 0 *)
  2078. jnz $$GoodSize
  2079. (*%F _jpicall *)
  2080. push ax
  2081. (*%E *)
  2082. (*%T _fcall *)
  2083. push cs
  2084. (*%E *)
  2085. call __nfree (* if so, just free *)
  2086. (*%F _jpicall *)
  2087. inc sp
  2088. inc sp
  2089. (*%E *)
  2090. xor ax, ax
  2091. jmp $$Return
  2092. $$GoodSize:
  2093. inc bx (* make room for size word *)
  2094. inc bx
  2095. push bx
  2096. dec ax (* point si at allocated size *)
  2097. dec ax
  2098. mov si, ax
  2099. mov cx, bx
  2100. mov bx, [si]
  2101. (*%T _fcall *)
  2102. push cs
  2103. (*%E *)
  2104. call StdMem$ResizeNear
  2105. pop cx
  2106. mov di, ax
  2107. or ax, ax (* was it resizable? *)
  2108. jnz $$Exit
  2109. (*%T _jpicall *)
  2110. mov ax, cx (* if not, we have to do it *)
  2111. (*%E *)
  2112. (*%F _jpicall *)
  2113. push cx
  2114. (*%E *)
  2115. (*%T _fcall *)
  2116. push cs
  2117. (*%E *)
  2118. call __nmalloc
  2119. (*%F _jpicall *)
  2120. inc sp
  2121. inc sp
  2122. (*%E *)
  2123. or ax, ax (* Was malloc successful *)
  2124. jne $$MallocGood
  2125. mov word [_errno], ERROR_NOMEMORY (* if not report error & return *)
  2126. jmp $$Return
  2127. $$Exit:
  2128. mov [di], cx
  2129. inc di
  2130. inc di
  2131. mov ax, di
  2132. jmp $$Return
  2133. $$MallocGood:
  2134. mov di, ax
  2135. push di
  2136. mov ax, [si]
  2137. inc si
  2138. inc si
  2139. push si
  2140. cmp ax, cx
  2141. jae $$Copy
  2142. mov cx, ax
  2143. $$Copy:
  2144. push ds (* set ES to _DATA *)
  2145. pop es
  2146. shr cx, 1
  2147. rep; movsw
  2148. adc cx, cx
  2149. rep; movsb
  2150. (*%T _jpicall *)
  2151. pop ax
  2152. (*%E *)
  2153. (*%T _fcall *)
  2154. push cs
  2155. (*%E *)
  2156. call __nfree
  2157. (*%F _jpicall *)
  2158. inc sp
  2159. inc sp
  2160. (*%E *)
  2161. pop ax
  2162. $$Return:
  2163. (*%F SameDS *)
  2164. pop ds
  2165. (*%E NOT SameDS *)
  2166. pop si
  2167. pop di
  2168. (*%T _jpicall *)
  2169. pop cx
  2170. (*%E *)
  2171. (*%F _jpicall *)
  2172. pop bp
  2173. (*%E *)
  2174. (*%T _fcall *)
  2175. ret far 0
  2176. (*%E *)
  2177. (*%F _fcall *)
  2178. ret 0
  2179. (*%E *)
  2180. (*%E _DLL *)
  2181. section
  2182. (*%F _DLL *)
  2183. (***********************************************************************)
  2184. (* __nexpand - C Near Heap Change Allocation *)
  2185. (* *)
  2186. (* void near *_nexpand(void near *buffer,size_t size) *)
  2187. (* *)
  2188. (* All Memory Models *)
  2189. (* *)
  2190. (* Internal Procedure *)
  2191. (* Parameters: *)
  2192. (* ax - Buffer address *)
  2193. (* bx - New Buffer Size *)
  2194. (* Trashes: *)
  2195. (* es *)
  2196. (* Return: *)
  2197. (* ax - New Buffer address *)
  2198. (* Aliases: *)
  2199. (* __expand - in near pointer models *)
  2200. (* *)
  2201. (***********************************************************************)
  2202. segment _TEXT(CODE,48H)
  2203. extrn StdMem$ReallocNear
  2204. Size = Frame + 2
  2205. Buffer = Frame
  2206. (*%F _fptr *)
  2207. public __expand :
  2208. (*%E *)
  2209. public __nexpand :
  2210. (*%F _jpicall *)
  2211. push bp
  2212. mov bp, sp
  2213. mov ax, [bp][Size]
  2214. mov bx, [bp][Buffer]
  2215. dec bx
  2216. dec bx
  2217. (*%E *)
  2218. (*%T _jpicall *)
  2219. push cx
  2220. dec ax (* Point at size *)
  2221. dec ax
  2222. xchg ax, bx (* index address with bx *)
  2223. (*%E *)
  2224. inc ax (* make room for size *)
  2225. inc ax
  2226. push ax (* save size *)
  2227. mov cx, [bx]
  2228. xchg ax, bx
  2229. xchg bx, cx
  2230. (*%T _fcall *)
  2231. push cs
  2232. (*%E *)
  2233. call StdMem$ReallocNear
  2234. pop cx (* GetSize *)
  2235. or ax, ax (* was it resizable? *)
  2236. jz $$Return
  2237. mov bx, ax
  2238. mov [bx], cx
  2239. inc ax
  2240. inc ax
  2241. $$Return:
  2242. (*%T _jpicall *)
  2243. pop cx
  2244. (*%E *)
  2245. (*%F _jpicall *)
  2246. pop bp
  2247. (*%E *)
  2248. (*%T _fcall *)
  2249. ret far 0
  2250. (*%E *)
  2251. (*%F _fcall *)
  2252. ret 0
  2253. (*%E *)
  2254. (*%E _DLL *)
  2255. section
  2256. (*%F _DLL *)
  2257. (***********************************************************************)
  2258. (* __nfree - C Near Heap Deallocation *)
  2259. (* *)
  2260. (* void _nfree(void near *buffer) *)
  2261. (* *)
  2262. (* All Memory Models + C Stack Convention *)
  2263. (* *)
  2264. (* Internal Procedure *)
  2265. (* Parameters: *)
  2266. (* ax - Address of buffer *)
  2267. (* Trashes: *)
  2268. (* none *)
  2269. (* Return: *)
  2270. (* none *)
  2271. (* Aliases: *)
  2272. (* _free - in Near Pointer Models *)
  2273. (* *)
  2274. (***********************************************************************)
  2275. segment _TEXT(CODE,48H)
  2276. extrn StdMem$DeallocNear
  2277. Buffer = Frame
  2278. (*%F _fptr *)
  2279. public _free :
  2280. (*%E *)
  2281. public __nfree :
  2282. (*%F _jpicall *)
  2283. push bp
  2284. mov bp, sp
  2285. mov ax, [bp][Buffer]
  2286. (*%E *)
  2287. or ax, ax
  2288. jz $$Return
  2289. (*%T _jpicall *)
  2290. push bx
  2291. (*%E *)
  2292. dec ax
  2293. dec ax
  2294. mov bx, ax
  2295. mov bx, [bx]
  2296. (*%T _fcall *)
  2297. push cs
  2298. (*%E *)
  2299. call StdMem$DeallocNear
  2300. (*%T _jpicall *)
  2301. pop bx
  2302. (*%E *)
  2303. $$Return:
  2304. (*%F _jpicall *)
  2305. pop bp
  2306. (*%E *)
  2307. (*%T _fcall *)
  2308. ret far 0
  2309. (*%E *)
  2310. (*%F _fcall *)
  2311. ret 0
  2312. (*%E *)
  2313. (*%E _DLL *)
  2314. section
  2315. (*%F _DLL *)
  2316. (***********************************************************************)
  2317. (* __nmsize - C Query Size of Near Heap Allocation *)
  2318. (* *)
  2319. (* int _nmsize(void near *buffer) *)
  2320. (* *)
  2321. (* All Memory Models + C Stack Convention *)
  2322. (* *)
  2323. (* Internal Procedure *)
  2324. (* Parameters: *)
  2325. (* ax - Address of buffer *)
  2326. (* Trashes: *)
  2327. (* none *)
  2328. (* Return: *)
  2329. (* ax - Size of Original Allocation *)
  2330. (* Aliases: *)
  2331. (* _msize - in Near Pointer Models *)
  2332. (* *)
  2333. (***********************************************************************)
  2334. segment _TEXT(CODE,48H)
  2335. Buffer = Frame
  2336. (*%F _fptr *)
  2337. public __msize :
  2338. (*%E *)
  2339. public __nmsize :
  2340. (*%F _jpicall *)
  2341. push bp
  2342. mov bp, sp
  2343. mov bx, [bp][Buffer]
  2344. (*%E *)
  2345. (*%T _jpicall *)
  2346. push bx
  2347. mov bx, ax
  2348. (*%E *)
  2349. mov ax, [bx][-2]
  2350. dec ax
  2351. dec ax
  2352. (*%T _jpicall *)
  2353. pop bx
  2354. (*%E *)
  2355. (*%F _jpicall *)
  2356. pop bp
  2357. (*%E *)
  2358. (*%T _fcall *)
  2359. ret far 0
  2360. (*%E *)
  2361. (*%F _fcall *)
  2362. ret 0
  2363. (*%E *)
  2364. (*%E _DLL *)
  2365. section
  2366. (*%F _DLL *)
  2367. (***********************************************************************)
  2368. (* _nearcoreleft - C Query Near Heap Available *)
  2369. (* *)
  2370. (* unsigned _nearcoreleft(void) + C Stack Convention *)
  2371. (* *)
  2372. (* All Memory Models *)
  2373. (* *)
  2374. (* Internal Procedure *)
  2375. (* Parameters: *)
  2376. (* none *)
  2377. (* Trashes: *)
  2378. (* none *)
  2379. (* Return: *)
  2380. (* ax - Bytes available on near heap *)
  2381. (* Aliases: *)
  2382. (* __memavl *)
  2383. (* _coreleft - in Near Memory Models *)
  2384. (* *)
  2385. (***********************************************************************)
  2386. segment _TEXT(CODE,48H)
  2387. extrn StdMem$AvailNear
  2388. (*%F _fptr *)
  2389. public _coreleft :
  2390. (*%E *)
  2391. public __memavl :
  2392. public _nearcoreleft :
  2393. (*%T _fcall *)
  2394. push cs
  2395. (*%E *)
  2396. call StdMem$AvailNear
  2397. add ax, -5
  2398. (*%T _fcall *)
  2399. ret far 0
  2400. (*%E *)
  2401. (*%F _fcall *)
  2402. ret 0
  2403. (*%E *)
  2404. (*%E _DLL *)
  2405. section
  2406. (*%F _DLL *)
  2407. (***********************************************************************)
  2408. (* __memmax - C Query Near Heap Largest Block Available *)
  2409. (* *)
  2410. (* unsigned __memmax(void) *)
  2411. (* *)
  2412. (* All Memory Models + C Stack Convention *)
  2413. (* *)
  2414. (* Internal Procedure *)
  2415. (* Parameters: *)
  2416. (* none *)
  2417. (* Trashes: *)
  2418. (* none *)
  2419. (* Return: *)
  2420. (* ax - Bytes available on near heap *)
  2421. (* *)
  2422. (***********************************************************************)
  2423. segment _TEXT(CODE,48H)
  2424. extrn StdMem$LargestNear
  2425. public __memmax :
  2426. (*%T _fcall *)
  2427. push cs
  2428. (*%E *)
  2429. call StdMem$LargestNear
  2430. add ax, -5
  2431. (*%T _fcall *)
  2432. ret far 0
  2433. (*%E *)
  2434. (*%F _fcall *)
  2435. ret 0
  2436. (*%E *)
  2437. (*%E _DLL *)
  2438. section
  2439. (***********************************************************************)
  2440. (* __fmalloc - C Far Heap Allocation *)
  2441. (* *)
  2442. (* void far *_fmalloc(size_t size) *)
  2443. (* *)
  2444. (* All Memory Models + C Stack Convention *)
  2445. (* *)
  2446. (* Internal Procedure *)
  2447. (* Parameters: *)
  2448. (* ax - Request Size *)
  2449. (* Trashes: *)
  2450. (* es *)
  2451. (* Return: *)
  2452. (* dx:ax - Address of Huge Allocation *)
  2453. (* Aliases: *)
  2454. (* malloc - in Far Pointer Models *)
  2455. (* *)
  2456. (***********************************************************************)
  2457. segment _TEXT(CODE,48H)
  2458. extrn StdMem$AllocFar
  2459. Size = Frame
  2460. (*%T _fptr *)
  2461. public _malloc :
  2462. (*%E *)
  2463. public __fmalloc :
  2464. (*%F _jpicall *)
  2465. push bp
  2466. mov bp, sp
  2467. mov ax, [bp][Size]
  2468. (*%E *)
  2469. push bx
  2470. or ax, ax
  2471. jz $$BadArg2
  2472. add ax, 2
  2473. jc $$BadArg
  2474. push ax
  2475. (*%T _fcall *)
  2476. push cs
  2477. (*%E *)
  2478. call StdMem$AllocFar
  2479. mov bx, ax
  2480. or bx, dx
  2481. jz $$Failed
  2482. $$Continue:
  2483. mov es, dx
  2484. mov bx, ax
  2485. pop es:[bx]
  2486. inc ax
  2487. inc ax
  2488. $$Return:
  2489. pop bx
  2490. (*%F _jpicall *)
  2491. pop bp
  2492. (*%E *)
  2493. (*%T _fcall *)
  2494. ret far 0
  2495. (*%E *)
  2496. (*%F _fcall *)
  2497. ret 0
  2498. (*%E *)
  2499. $$BadArg:
  2500. xor ax, ax
  2501. $$BadArg2:
  2502. cwd
  2503. jmp $$Return
  2504. $$Failed:
  2505. pop bx
  2506. jmp $$Return
  2507. section
  2508. (***********************************************************************)
  2509. (* __fcalloc - C Far Heap Initialized Allocation *)
  2510. (* *)
  2511. (* void far *_fcalloc(size_t number,size_t size) *)
  2512. (* *)
  2513. (* All Memory Models + C Stack Convention *)
  2514. (* *)
  2515. (* Internal Procedure *)
  2516. (* Parameters: *)
  2517. (* ax - Number of units requested *)
  2518. (* bx - Unit Size *)
  2519. (* Trashes: *)
  2520. (* es *)
  2521. (* Return: *)
  2522. (* dx:ax - Address of Allocation *)
  2523. (* Aliases: *)
  2524. (* _calloc - in far pointer models *)
  2525. (* *)
  2526. (***********************************************************************)
  2527. segment _TEXT(CODE,48H)
  2528. extrn __fmalloc
  2529. Size = Frame + 2
  2530. Number = Frame
  2531. (*%T _fptr *)
  2532. public _calloc :
  2533. (*%E *)
  2534. public __fcalloc :
  2535. (*%F _jpicall *)
  2536. push bp
  2537. mov bp, sp
  2538. mov ax, [bp][Size]
  2539. mov bx, [bp][Number]
  2540. (*%E *)
  2541. push di
  2542. (*%T _jpicall *)
  2543. push cx
  2544. xchg ax, bx
  2545. (*%E *)
  2546. mul bx
  2547. jo $$Invalid
  2548. or ax, ax
  2549. jz $$Invalid
  2550. mov cx, ax
  2551. (*%F _jpicall *)
  2552. push ax
  2553. (*%E *)
  2554. (*%T _fcall *)
  2555. push cs
  2556. (*%E *)
  2557. call __fmalloc
  2558. (*%F _jpicall *)
  2559. inc sp
  2560. inc sp
  2561. (*%E *)
  2562. mov bx, ax
  2563. or ax, dx
  2564. jz $$Return
  2565. mov es, dx
  2566. mov di, bx
  2567. xor ax, ax
  2568. shr cx, 1
  2569. jnc $$ClearMem
  2570. stosb
  2571. $$ClearMem:
  2572. rep; stosw
  2573. mov ax, bx
  2574. $$Return:
  2575. (*%T _jpicall *)
  2576. pop cx
  2577. (*%E *)
  2578. pop di
  2579. (*%F _jpicall *)
  2580. pop bp
  2581. (*%E *)
  2582. (*%T _fcall *)
  2583. ret far 0
  2584. (*%E *)
  2585. (*%F _fcall *)
  2586. ret 0
  2587. (*%E *)
  2588. $$Invalid:
  2589. xor ax, ax
  2590. cwd
  2591. jmp $$Return
  2592. section
  2593. (***********************************************************************)
  2594. (* __frealloc - C Far Heap Change Allocation *)
  2595. (* *)
  2596. (* void far *_frealloc(void far *buffer,size_t size) *)
  2597. (* *)
  2598. (* All Memory Models + C Stack Convention *)
  2599. (* *)
  2600. (* Internal Procedure *)
  2601. (* Parameters: *)
  2602. (* bx:ax - Buffer address *)
  2603. (* cx - New Buffer Size *)
  2604. (* Trashes: *)
  2605. (* es *)
  2606. (* Return: *)
  2607. (* dx:ax - New Buffer address *)
  2608. (* Aliases: *)
  2609. (* _realloc - in far pointer models *)
  2610. (* *)
  2611. (***********************************************************************)
  2612. segment _DATA(DATA,48H)
  2613. segment _TEXT(CODE,48H)
  2614. extrn __fmalloc
  2615. extrn __ffree
  2616. extrn StdMem$ResizeFar
  2617. extrn _errno
  2618. Size = Frame + 4
  2619. Buffer = Frame
  2620. (*%T _fptr *)
  2621. public _realloc :
  2622. (*%E *)
  2623. public __frealloc :
  2624. (*%F _jpicall *)
  2625. push bp
  2626. mov bp, sp
  2627. (*%E *)
  2628. push ds
  2629. push di
  2630. push si
  2631. (*%T _jpicall *)
  2632. mov es, bx (* Seg to es *)
  2633. (*%E *)
  2634. (*%F _jpicall *)
  2635. les ax, [bp][Buffer]
  2636. mov bx, es
  2637. mov cx, [bp][Size]
  2638. (*%E *)
  2639. mov si, ax (* Ofs to si *)
  2640. or ax, bx (* is Buffer = NIL *)
  2641. jnz $$GoodPtr
  2642. (*%T _jpicall *)
  2643. mov ax, cx (* if so, just malloc *)
  2644. (*%E *)
  2645. (*%F _jpicall *)
  2646. push cx
  2647. (*%E *)
  2648. (*%T _fcall *)
  2649. push cs
  2650. (*%E *)
  2651. call __fmalloc
  2652. (*%F _jpicall *)
  2653. inc sp
  2654. inc sp
  2655. (*%E *)
  2656. jmp $$Return
  2657. $$GoodPtr:
  2658. or cx, cx (* is size = 0 *)
  2659. jnz $$GoodSize
  2660. (*%T _jpicall *)
  2661. mov ax, si (* if so, just free *)
  2662. (*%E *)
  2663. (*%F _jpicall *)
  2664. push bx
  2665. push si
  2666. (*%E *)
  2667. (*%T _fcall *)
  2668. push cs
  2669. (*%E *)
  2670. call __ffree
  2671. (*%F _jpicall *)
  2672. add sp, 4
  2673. (*%E *)
  2674. xor ax, ax
  2675. cwd
  2676. jmp $$Return
  2677. $$GoodSize:
  2678. inc cx (* make room for size word *)
  2679. inc cx
  2680. push cx
  2681. dec si (* point si at allocated size *)
  2682. dec si
  2683. mov di, es
  2684. mov dx, cx
  2685. mov cx, es:[si]
  2686. mov ax, si
  2687. (*%T _fcall *)
  2688. push cs
  2689. (*%E *)
  2690. call StdMem$ResizeFar
  2691. pop cx
  2692. mov bx, di
  2693. mov di, ax
  2694. or ax, dx (* was it resizable? *)
  2695. jnz $$Exit
  2696. (*%T _jpicall *)
  2697. mov ax, cx (* if not, we have to do it *)
  2698. (*%E *)
  2699. (*%F _jpicall *)
  2700. push cx
  2701. (*%E *)
  2702. (*%T _fcall *)
  2703. push cs
  2704. (*%E *)
  2705. call __fmalloc
  2706. (*%F _jpicall *)
  2707. inc sp
  2708. inc sp
  2709. (*%E *)
  2710. mov di, ax
  2711. or ax, dx (* Was malloc successful *)
  2712. jne $$MallocGood
  2713. (*%T _fdata *)
  2714. mov di, _DATA
  2715. mov ds, di
  2716. (*%E *)
  2717. mov word [_errno], ERROR_NOMEMORY (* if not report error & return *)
  2718. jmp $$Return
  2719. $$Exit: (* cx = new size | dx:si = pointer to memory *)
  2720. mov ds, dx
  2721. mov [di], cx
  2722. inc di
  2723. inc di
  2724. mov ax, di
  2725. jmp $$Return
  2726. $$MallocGood:
  2727. push dx
  2728. push di
  2729. inc si
  2730. inc si
  2731. push bx
  2732. push si
  2733. push ds
  2734. mov ds, bx
  2735. mov ax, [si][-2]
  2736. cmp ax, cx
  2737. jae $$Copy
  2738. mov cx, ax
  2739. $$Copy:
  2740. mov es, dx
  2741. test di, 1
  2742. je $$CopyEven
  2743. movsb
  2744. dec cx
  2745. $$CopyEven:
  2746. shr cx, 1
  2747. rep; movsw
  2748. adc cx, cx
  2749. rep; movsb
  2750. pop ds
  2751. (*%T _jpicall *)
  2752. pop ax
  2753. pop bx
  2754. (*%E *)
  2755. (*%T _fcall *)
  2756. push cs
  2757. (*%E *)
  2758. call __ffree
  2759. (*%F _jpicall *)
  2760. add sp, 4
  2761. (*%E *)
  2762. pop ax
  2763. pop dx
  2764. $$Return:
  2765. pop si
  2766. pop di
  2767. pop ds
  2768. (*%F _jpicall *)
  2769. pop bp
  2770. (*%E *)
  2771. (*%T _fcall *)
  2772. ret far 0
  2773. (*%E *)
  2774. (*%F _fcall *)
  2775. ret 0
  2776. (*%E *)
  2777. section
  2778. (***********************************************************************)
  2779. (* __fexpand - C Far Heap Change Allocation *)
  2780. (* *)
  2781. (* void far *_fexpand(void far *buffer,size_t size) *)
  2782. (* *)
  2783. (* All Memory Models + C Stack Convention *)
  2784. (* *)
  2785. (* Internal Procedure *)
  2786. (* Parameters: *)
  2787. (* bx:ax - Buffer address *)
  2788. (* cx - New Buffer Size *)
  2789. (* Trashes: *)
  2790. (* es *)
  2791. (* Return: *)
  2792. (* dx:ax - New Buffer address *)
  2793. (* Aliases: *)
  2794. (* __expand - in far pointer models *)
  2795. (* *)
  2796. (***********************************************************************)
  2797. segment _TEXT(CODE,48H)
  2798. extrn StdMem$ReallocFar
  2799. Size = Frame + 4
  2800. Buffer = Frame
  2801. (*%T _fptr *)
  2802. public __expand :
  2803. (*%E *)
  2804. public __fexpand :
  2805. (*%F _jpicall *)
  2806. push bp
  2807. mov bp, sp
  2808. les ax, [bp][Buffer]
  2809. mov bx, es
  2810. mov cx, [bp][Size]
  2811. (*%E *)
  2812. push si
  2813. inc cx (* make room for size word *)
  2814. inc cx
  2815. push cx
  2816. dec ax (* point ax at allocated size *)
  2817. dec ax
  2818. mov si, ax (* Ofs to si *)
  2819. (*%T _jpicall *)
  2820. mov es, bx (* Seg to es *)
  2821. (*%E *)
  2822. mov dx, cx
  2823. mov cx, es:[si]
  2824. (*%T _fcall *)
  2825. push cs
  2826. (*%E *)
  2827. call StdMem$ReallocFar
  2828. pop cx
  2829. mov si, ax
  2830. or ax, dx (* was it resizable? *)
  2831. jz $$Return
  2832. mov es, dx
  2833. mov es:[si], cx
  2834. mov ax, si
  2835. inc ax
  2836. inc ax
  2837. $$Return:
  2838. pop si
  2839. (*%F _jpicall *)
  2840. pop bp
  2841. (*%E *)
  2842. (*%T _fcall *)
  2843. ret far 0
  2844. (*%E *)
  2845. (*%F _fcall *)
  2846. ret 0
  2847. (*%E *)
  2848. section
  2849. (***********************************************************************)
  2850. (* __ffree - C Far Heap Deallocation *)
  2851. (* *)
  2852. (* void _ffree(void far *buffer) *)
  2853. (* *)
  2854. (* All Memory Models + C Stack Convention *)
  2855. (* *)
  2856. (* Internal Procedure *)
  2857. (* Parameters: *)
  2858. (* bx:ax - Address of buffer *)
  2859. (* Trashes: *)
  2860. (* es *)
  2861. (* Return: *)
  2862. (* none *)
  2863. (* Aliases: *)
  2864. (* _free - in Far Pointer Models *)
  2865. (* *)
  2866. (***********************************************************************)
  2867. segment _TEXT(CODE,48H)
  2868. extrn StdMem$DeallocFar
  2869. Buffer = Frame
  2870. (*%T _fptr *)
  2871. public _free :
  2872. (*%E *)
  2873. public __ffree :
  2874. (*%F _jpicall *)
  2875. push bp
  2876. mov bp, sp
  2877. les ax, [bp][Buffer]
  2878. mov bx, es
  2879. (*%E *)
  2880. (*%T _jpicall *)
  2881. push cx
  2882. mov es, bx
  2883. (*%E *)
  2884. mov cx, bx
  2885. or cx, ax
  2886. jz $$Return
  2887. dec ax
  2888. dec ax
  2889. xchg bx, ax
  2890. mov cx, es:[bx]
  2891. xchg bx, ax
  2892. (*%T _fcall *)
  2893. push cs
  2894. (*%E *)
  2895. call StdMem$DeallocFar
  2896. $$Return:
  2897. (*%T _jpicall *)
  2898. pop cx
  2899. (*%E *)
  2900. (*%F _jpicall *)
  2901. pop bp
  2902. (*%E *)
  2903. (*%T _fcall *)
  2904. ret far 0
  2905. (*%E *)
  2906. (*%F _fcall *)
  2907. ret 0
  2908. (*%E *)
  2909. section
  2910. (***********************************************************************)
  2911. (* __fmsize - C Query Size of Far Heap Allocation *)
  2912. (* *)
  2913. (* int _fmsize(void far *buffer) *)
  2914. (* *)
  2915. (* All Memory Models + C Stack Convention *)
  2916. (* *)
  2917. (* Internal Procedure *)
  2918. (* Parameters: *)
  2919. (* bx:ax - Address of buffer *)
  2920. (* Trashes: *)
  2921. (* none *)
  2922. (* Return: *)
  2923. (* ax - Size of Original Allocation *)
  2924. (* Aliases: *)
  2925. (* _msize - in Far Pointer Models *)
  2926. (* *)
  2927. (***********************************************************************)
  2928. segment _TEXT(CODE,48H)
  2929. Buffer = Frame
  2930. (*%T _fptr *)
  2931. public __msize :
  2932. (*%E *)
  2933. public __fmsize :
  2934. (*%F _jpicall *)
  2935. push bp
  2936. mov bp, sp
  2937. les bx, [bp][Buffer]
  2938. (*%E *)
  2939. (*%T _jpicall *)
  2940. mov es, bx
  2941. mov bx, ax
  2942. (*%E *)
  2943. mov ax, es:[bx][-2]
  2944. dec ax
  2945. dec ax
  2946. (*%F _jpicall *)
  2947. pop bp
  2948. (*%E *)
  2949. (*%T _fcall *)
  2950. ret far 0
  2951. (*%E *)
  2952. (*%F _fcall *)
  2953. ret 0
  2954. (*%E *)
  2955. section
  2956. (***********************************************************************)
  2957. (* _farcoreleft - C Query Far Heap Available *)
  2958. (* *)
  2959. (* unsigned long _farcoreleft(void) *)
  2960. (* *)
  2961. (* All Memory Models + C Stack Convention *)
  2962. (* *)
  2963. (* Internal Procedure *)
  2964. (* Parameters: *)
  2965. (* none *)
  2966. (* Trashes: *)
  2967. (* none *)
  2968. (* Return: *)
  2969. (* dx:ax = 0 *)
  2970. (* Aliases: *)
  2971. (* _coreleft *)
  2972. (* *)
  2973. (***********************************************************************)
  2974. segment _TEXT(CODE,48H)
  2975. (*%T _fptr *)
  2976. public _coreleft :
  2977. (*%E *)
  2978. public _farcoreleft :
  2979. xor ax, ax
  2980. cwd
  2981. (*%T _fcall *)
  2982. ret far 0
  2983. (*%E *)
  2984. (*%F _fcall *)
  2985. ret 0
  2986. (*%E *)
  2987. section
  2988. (***********************************************************************)
  2989. (* _halloc - C Huge Far Heap Allocation *)
  2990. (* *)
  2991. (* void huge *halloc(unsigned long size) *)
  2992. (* *)
  2993. (* All Memory Models + C Stack Convention *)
  2994. (* *)
  2995. (* Internal Procedure *)
  2996. (* Parameters: *)
  2997. (* bx:ax - Request Size *)
  2998. (* Trashes: *)
  2999. (* none *)
  3000. (* Return: *)
  3001. (* dx:ax - Address of Huge Allocation *)
  3002. (* *)
  3003. (***********************************************************************)
  3004. segment _TEXT(CODE,48H)
  3005. extrn __MaxReallocHugeSegments
  3006. extrn DOSALLOCHUGE
  3007. SizeLo = CodePtrSize
  3008. SizeHi = CodePtrSize + 2
  3009. public _halloc :
  3010. (*%F _jpicall *)
  3011. mov bx, sp
  3012. mov ax, ss:[bx][SizeLo] (* 64k's of request *)
  3013. mov bx, ss:[bx][SizeHi] (* remainder of request *)
  3014. (*%E *)
  3015. public $halloc :
  3016. mov dx,bx (* test for null request *)
  3017. or dx,ax
  3018. jz Done (* null request *)
  3019. push ax (* space for selector *)
  3020. mov dx,sp (* address of selector *)
  3021. push bx (* 64k's of reqest *)
  3022. push ax (* remainder of request *)
  3023. push ss (* segment of selector *)
  3024. push dx (* offset of selector *)
  3025. (*%F SameDS *)
  3026. mov dx,seg __MaxReallocHugeSegments
  3027. mov es,dx
  3028. push es:[__MaxReallocHugeSegments]
  3029. (*%E NOT SameDS *)
  3030. (*%T SameDS *)
  3031. push [__MaxReallocHugeSegments]
  3032. (*%E SameDS *)
  3033. db 06AH, 000H (*I push 0-non share,non discardable *)
  3034. call far DOSALLOCHUGE
  3035. or ax, ax (* did allocation fail ? *)
  3036. pop dx (* returned segment *)
  3037. jz Done (* no problem *)
  3038. xor ax,ax (* allocation failed-return NULL *)
  3039. cwd
  3040. Done: db Ret0@
  3041. section
  3042. (***********************************************************************)
  3043. (* _hcalloc - C Huge Far Heap Initialized Allocation *)
  3044. (* *)
  3045. (* void huge *hcalloc(unsigned long number,unsigned long size) *)
  3046. (* *)
  3047. (* All Memory Models + C Stack Convention *)
  3048. (* *)
  3049. (* Internal Procedure *)
  3050. (* Parameters: *)
  3051. (* bx:ax - Number of units requested *)
  3052. (* dx:cx - Unit Size *)
  3053. (* Trashes: *)
  3054. (* es *)
  3055. (* Return: *)
  3056. (* dx:ax - Address of Huge Allocation *)
  3057. (* *)
  3058. (***********************************************************************)
  3059. segment _DATA(DATA,48H)
  3060. segment _TEXT(CODE,48H)
  3061. extrn $halloc
  3062. extrn F$UnsMol
  3063. extrn StdMem@HugeShift
  3064. NumLo = Frame
  3065. NumHi = Frame + 2
  3066. SizeLo = Frame + 4
  3067. SizeHi = Frame + 6
  3068. public _hcalloc:
  3069. (*%F _jpicall *)
  3070. push bp
  3071. mov bp, sp
  3072. (*%E *)
  3073. push di
  3074. (*%T _fdata *)
  3075. push ds
  3076. mov di, _DATA
  3077. mov ds, di
  3078. (*%E *)
  3079. (*%F _jpicall *)
  3080. mov ax, [bp][NumLo]
  3081. mov bx, [bp][NumHi]
  3082. mov cx, [bp][SizeLo]
  3083. mov dx, [bp][SizeHi]
  3084. (*%E *)
  3085. push dx
  3086. push cx
  3087. push bx
  3088. push ax
  3089. call far F$UnsMol
  3090. push ax (* push part seg *)
  3091. mov bx, dx
  3092. (*%T _fcall *)
  3093. push cs
  3094. (*%E *)
  3095. call $halloc
  3096. or dx, dx (* did allocation fail ? *)
  3097. jz $$Return (* yes *)
  3098. mov es, dx (* es = allocation's 1'st segment *)
  3099. or bx, bx (* 64k's in allocation ? *)
  3100. jz $$Exit (* no *)
  3101. $$ClearLoop: (* clear mem 1 64k seg at a time *)
  3102. mov cx, 08000H
  3103. sub di,di
  3104. rep; stosw
  3105. mov cx, es
  3106. add cx, [StdMem@HugeShift]
  3107. mov es, cx
  3108. dec bx
  3109. jnz $$ClearLoop
  3110. $$Exit:
  3111. sub di,di
  3112. pop cx (* part segment remainder *)
  3113. shr cx, 1
  3114. rep; stosw
  3115. jnc $$NotOdd
  3116. stosb
  3117. $$NotOdd:
  3118. Done:
  3119. (*%T _fdata *)
  3120. pop ds
  3121. (*%E *)
  3122. pop di
  3123. (*%F _jpicall *)
  3124. pop bp
  3125. (*%E *)
  3126. db Ret0@
  3127. $$Return:
  3128. pop cx
  3129. jmp Done
  3130. section
  3131. (***********************************************************************)
  3132. (* _hrealloc - C Far Heap Change Huge Allocation *)
  3133. (* *)
  3134. (* void huge *_hrealloc(void huge *buffer,unsigned long size) *)
  3135. (* *)
  3136. (* All Memory Models + C Stack Convention *)
  3137. (* *)
  3138. (* Internal Procedure *)
  3139. (* Parameters: *)
  3140. (* bx:ax - Buffer address *)
  3141. (* dx:cx - New Buffer Size *)
  3142. (* Trashes: *)
  3143. (* None *)
  3144. (* Return: *)
  3145. (* dx:ax - New Buffer address *)
  3146. (* *)
  3147. (***********************************************************************)
  3148. segment _TEXT(CODE,48H)
  3149. extrn $hfree
  3150. extrn $halloc
  3151. extrn DOSREALLOCHUGE
  3152. Buffer = Frame
  3153. SizeLo = Frame + 4
  3154. SizeHi = Frame + 6
  3155. public _hrealloc :
  3156. (*%T StkParam *)
  3157. push bp
  3158. mov bp, sp
  3159. mov dx,[bp][SizeHi] (* 64'ks requested *)
  3160. mov cx,[bp][SizeLo] (* remainder requested *)
  3161. mov bx,[bp][2][Buffer] (* old segment *)
  3162. pop bp
  3163. (*%E StkParam *)
  3164. mov ax,cx
  3165. or bx,bx (* valid segment ? *)
  3166. jnz Valid (* yes *)
  3167. mov bx,dx (* allocate only *)
  3168. jmp near $halloc
  3169. Valid:or ax,dx (* non-zero request ? *)
  3170. jnz Not0 (* not 0 byte requested *)
  3171. call $hfree (* free block only *)
  3172. jmp Fail
  3173. Not0: push dx (* 64'ks requested *)
  3174. push cx (* remainder requested *)
  3175. push bx (* old segment *)
  3176. xchg bx,dx (* dx = old seg. bx:cx=request size *)
  3177. call far DOSREALLOCHUGE (* attempt to grow/shrink cur block *)
  3178. or ax, ax
  3179. jz Done (* sucess *)
  3180. Fail: xor ax,ax (* failed-return 0 *)
  3181. cwd
  3182. Done: db Ret0@
  3183. section
  3184. (***********************************************************************)
  3185. (* hfree - Standard Far Heap Deallocation for blocks *)
  3186. (* Allocated with halloc,hcalloc,or hrealloc *)
  3187. (* *)
  3188. (* PROCEDURE DeallocHuge(Addr:FarADDRESS):BOOLEAN *)
  3189. (* *)
  3190. (* OS/2 & Extender Memory Models *)
  3191. (* *)
  3192. (* Internal Procedure *)
  3193. (* Parameters: *)
  3194. (* bx:ax - Far Pointer to memory block *)
  3195. (* Trashes: *)
  3196. (* ax *)
  3197. (* Return: none *)
  3198. (* *)
  3199. (***********************************************************************)
  3200. segment _TEXT(CODE,48H)
  3201. extrn DOSFREESEG
  3202. public _hfree :
  3203. (*%T StkParam *)
  3204. mov bx,sp
  3205. mov bx,ss:[bx][2][CodePtrSize] (* segment to be freed *)
  3206. (*%E StkParam *)
  3207. public $hfree:
  3208. push bx (* push segment *)
  3209. call far DOSFREESEG
  3210. db Ret0@
  3211. end