Memory.asm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. include_macros equ 1
  2. include_deb_mac equ 1
  3. include_error_codes equ 1
  4. include include.asm
  5. ifdef mem_check ;check memory allocated and dealocated
  6. ;WARNING! THIS DISABLES FREE
  7. ;watch_address equ 0147f8ch
  8. abs_max_memory equ (2000*1000) ;up in 100000's
  9. memory_item struc
  10. address dd ?
  11. mem_size dd ?
  12. call_add dd ?
  13. memory_item ends
  14. memory_block_size equ 40
  15. start32data
  16. align 4
  17. memory_list dd ? ;pointer to allocation list
  18. memory_entries dd ? ;no of entries
  19. memory_block dd ? ;space allocated for entries
  20. total_memory dd ? ;32 bit memory currently allocated
  21. dos_mem_allocated dd 0 ;16 bit memory allocated
  22. max_memory dd 0 ;largest amount of memory allocated
  23. end32data
  24. endif
  25. start32code
  26. extrn _malloc:near
  27. extrn _realloc:near
  28. extrn _free:near
  29. ifdef mem_check
  30. extrn free_fixed_items:near
  31. extrn free_text_items:near
  32. endif
  33. initialise_memory proc
  34. ifdef mem_check
  35. push memory_block_size*SIZE memory_item ;set up the memory list
  36. call _malloc
  37. lea esp,4[esp]
  38. mov [memory_list],eax
  39. mov [memory_entries],0
  40. mov [memory_block],memory_block_size
  41. mov [total_memory],0
  42. endif
  43. ret
  44. initialise_memory endp
  45. my_malloc proc
  46. ; Allocate eax bytes
  47. ;printf "malloc %d bytes",eax
  48. ifdef debug_42
  49. jifne eax,not_0
  50. call_address
  51. program_error em_internal_error
  52. not_0:
  53. endif
  54. ifdef mem_check
  55. mov ecx,[memory_entries] ;check for room
  56. cmp ecx,[memory_block]
  57. jc room
  58. push eax
  59. mov eax,[memory_block] ;allocate more memory
  60. add eax,memory_block_size
  61. mov [memory_block],eax
  62. mov ebx,SIZE memory_item
  63. mul ebx
  64. push eax
  65. push [memory_list]
  66. call _realloc
  67. lea esp,8[esp]
  68. mov [memory_list],eax
  69. pop eax
  70. mov ecx,[memory_entries]
  71. room: ;Find a free place (ecx=memory_entries)
  72. mov ebx,[memory_list]
  73. jcxz first_one
  74. find_place: test (memory_item ptr[ebx]).address,-1
  75. je found_place
  76. add ebx,SIZE memory_item
  77. floop find_place
  78. first_one: inc [memory_entries]
  79. found_place: mov (memory_item ptr[ebx]).mem_size,eax
  80. add [total_memory],eax
  81. ifdef mem_check ;register max memory we use
  82. mov edx,[total_memory]
  83. cmp [max_memory],edx
  84. jnc max_ok
  85. mov [max_memory],edx
  86. max_ok:
  87. endif
  88. ; Check if we are allocating too much memory
  89. ifdef debug_42
  90. cmp [total_memory],abs_max_memory
  91. jc no_test
  92. printf "abs_max_memory was %d",abs_max_memory
  93. printf "We want %d",[total_memory]
  94. no_test:
  95. endif
  96. push ebx
  97. push eax
  98. call _malloc
  99. jife eax,no_memory
  100. lea esp,4[esp]
  101. pop ebx
  102. mov (memory_item ptr[ebx]).address,eax
  103. pop ecx
  104. push ecx
  105. mov (memory_item ptr[ebx]).call_add,ecx
  106. ifdef watch_address
  107. cmp eax,watch_address
  108. jne not_watch
  109. call Aaaaaaaa
  110. not_watch:
  111. endif
  112. else ;ifdef mem_check
  113. push eax
  114. call _malloc
  115. jife eax,no_memory
  116. lea esp,4[esp]
  117. endif
  118. ret
  119. no_memory:
  120. ifdef mem_check
  121. printf "total memory %d",[total_memory]
  122. endif
  123. program_error em_memory_error
  124. my_malloc endp
  125. my_free proc
  126. ; Free memory location eax
  127. cherror eax,e,0,em_internal_error
  128. ifdef mem_check
  129. mov esi,[memory_list]
  130. mov ecx,[memory_entries]
  131. look_loop: cmp (memory_item ptr[esi]).address,eax
  132. je found_mem
  133. add esi,SIZE memory_item
  134. floop look_loop
  135. pop ebx
  136. printf "address %x not allocated (call %x)",eax,ebx
  137. program_error em_internal_error
  138. found_mem: push eax ;save for free
  139. mov eax,(memory_item ptr[esi]).mem_size
  140. sub [total_memory],eax
  141. mov (memory_item ptr[esi]).address,0
  142. ifndef watch_address
  143. call _free
  144. endif
  145. else ;ifdef mem_check
  146. push eax
  147. call _free
  148. endif
  149. lea esp,4[esp]
  150. ret
  151. my_free endp
  152. ifdef mem_check
  153. end32code
  154. start32data
  155. free_list dd backscreen
  156. dd game_grid
  157. dd mouse_text_data
  158. dd saved_data
  159. dd game_grids
  160. dd mice_data
  161. dd route_grid
  162. dd main_character_set
  163. dd link_character_set
  164. dd control_char_set
  165. dd object_mouse_data
  166. dd status_ch_set
  167. dd work_palette
  168. dd c2_save_game_texts
  169. dd spp_control_panel
  170. dd spp_button
  171. dd spp_dn_btn
  172. dd spp_save_panel
  173. dd spp_yes_no
  174. dd spp_slide
  175. dd spp_slode
  176. dd c2_palette_data
  177. dd dinner_table_area
  178. dd pre_after_table_area
  179. ifdef with_replay
  180. dd replay_data
  181. endif
  182. dd -1
  183. end32data
  184. start32code
  185. check_mem proc
  186. ; First deallocate what we can
  187. printf "checking memory free"
  188. mov esi,offset free_list
  189. free_loop: lodsd
  190. cmp eax,-1
  191. je free_done
  192. push esi
  193. mov eax,[eax]
  194. jife eax,noppy
  195. call my_free
  196. noppy: pop esi
  197. jmp free_loop
  198. free_done: mov esi,offset loaded_file_list
  199. free_lflop: lodsd
  200. jife eax,flop_done
  201. and eax,2047
  202. push esi
  203. mov eax,[offset item_list+eax*4] ;get address and clear entry
  204. call my_free
  205. pop esi
  206. jmp free_lflop
  207. flop_done: mov esi,offset module_list
  208. mov ecx,16
  209. mod_check: lodsd
  210. jife eax,no_mod
  211. push esi
  212. push ecx
  213. call my_free
  214. pop ecx
  215. pop esi
  216. no_mod: loop mod_check
  217. call free_fixed_items
  218. call free_text_items
  219. ; Now check for what we couldn't
  220. mov esi,[memory_list]
  221. mov ecx,[memory_entries]
  222. jife ecx,no_mentries
  223. check_loop: mov eax,(memory_item ptr[esi]).address
  224. jife eax,no_mem
  225. mov ebx,(memory_item ptr[esi]).call_add
  226. printf "%xh not deallocated (call %x)",eax,ebx
  227. no_mem: add esi,SIZE memory_item
  228. loop check_loop
  229. no_mentries: printf "Total_mem (left) %d",[total_memory]
  230. mov eax,[max_memory]
  231. mov ebx,[dos_mem_allocated]
  232. add ebx,eax
  233. printf "Max memory %d (%d with dos mem)",eax,ebx
  234. ret
  235. check_mem endp
  236. ifdef watch_address
  237. Aaaaaaaa proc
  238. ret
  239. Aaaaaaaa endp
  240. endif
  241. endif
  242. end32code
  243. end
  244.