home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / programs / comms_networking / hearsay / !Hearsay / Libraries / Memory (.txt) < prev    next >
RISC OS BBC BASIC V Source  |  1996-02-19  |  8KB  |  264 lines

  1.  > Memory library
  2.  Chris Rutter, 1995
  3. compile_memory_stubs
  4.  code%,pass%
  5.  code% 256
  6.  pass%=0 
  7. P%=code%
  8. [ Opt     pass%
  9. .copy_memory_forward
  10.   Equd    0 ;from
  11.   Equd    0 ;to
  12.   Equd    0 ;length
  13. $  Ldr     r0,copy_memory_forward
  14. (  Ldr     r1,(copy_memory_forward+4)
  15. (  Ldr     r2,(copy_memory_forward+8)
  16.   Mov     r10,#0 ;inc
  17. .copy_memory_forward_loop
  18.   LdrB    r3,[r0,r10]
  19.   StrB    r3,[r1,r10]
  20.   Add     r10,r10,#1
  21.   Cmp     r10,r2
  22. &  BLe     copy_memory_forward_loop
  23.   Mov     Pc,r14
  24. .copy_memory_backward
  25.   Equd    0 ;from
  26.   Equd    0 ;to
  27.   Equd    0 ;length
  28. %  Ldr     r0,copy_memory_backward
  29. )  Ldr     r1,(copy_memory_backward+4)
  30. /  Ldr     r10,(copy_memory_backward+8) ;inc
  31. .copy_memory_backward_loop
  32.   LdrB    r3,[r0,r10]
  33.   StrB    r3,[r1,r10]
  34.   Sub     r10,r10,#1
  35.   Cmp     r10,#0
  36. %'  BGe     copy_memory_backward_loop
  37.   Mov     Pc,r14
  38.  pass%
  39. claim_heap_commented(size%,comment$)
  40.  debugmode% 
  41.  heap_comments$(active_heaps%+1)=comment$
  42. claim_heap(size%)
  43. claim_heap(size%)
  44.  anchor%
  45. ael("claim_heap")
  46.  (size% 
  47.  4)<>0 
  48.  size%=(size%-(size% 
  49.  4))+4
  50. 3-anchor%=
  51. new_heap_anchor(active_heaps%+1)
  52.  debugmode% 
  53. debug_message("Claiming heap {heap_comments$(active_heaps%+1)}, size="+
  54. hexadecimal(size%)+", anchor="+
  55. hexadecimal(anchor%)+" ("+
  56. (heapsanchor%(active_heaps%+1))+") ...")
  57.  ((slotsize%+&8000)-heaps_top%)<size% 
  58. ael("slotsize_increase")
  59.  increase%,old_slotsize%
  60. 83 increase%=size%-((slotsize%+&8000)-heaps_top%)
  61.  debugmode% 
  62. verbose 
  63. debug_message("Increasing slotsize by "+
  64. hexadecimal(increase%))
  65.  "Wimp_SlotSize",slotsize%+increase%+1024,-1 
  66.  new_slotsize%
  67.  new_slotsize%<slotsize%+increase% 
  68. msg("MemoryError.SlotSizeIncrease")
  69.  slotsize%+=increase%
  70. active_heaps%+=1
  71. @#heap_size%(active_heaps%)=size%
  72. A(heap_base%(active_heaps%)=heaps_top%
  73. heaps_top%+=size%
  74. C&!anchor%=heap_base%(active_heaps%)
  75. =anchor%
  76. enumerate_heaps
  77. ael("enumerate_heaps")
  78. debug_message("")
  79.  active_heaps%=0 
  80. debug_message("No active heaps.")
  81.  heap%
  82. debug_message("heaps base="+
  83. hexadecimal(external_heapbase%)+", heaps size="+
  84. hexadecimal(heaps_top%-external_heapbase%)+", anchor base="+
  85. hexadecimal(heap_anchors%))
  86.  heap%=1 
  87.  active_heaps%
  88. debug_message(" heap "+
  89. (heap%)+": base="+
  90. hexadecimal(heap_base%(heap%))+", size="+
  91. hexadecimal(heap_size%(heap%))+", anchor="+
  92. (heapsanchor%(heap%))+" ("+heap_comments$(heap%)+")")
  93.  heap%
  94. move_heapbase(new_location%)
  95. move_heaps(1,active_heaps%,new_location%)
  96. move_heaps(start_heap%,end_heap%,new_location%)
  97. ael("move_heaps")
  98.  active_heaps%>0 
  99.  heap_base%(start_heap%)<>new_location% 
  100.  start_memory%,end_memory%,dif%,byte%,size%
  101. ^* start_memory%=heap_base%(start_heap%)
  102. _< end_memory%=heap_base%(end_heap%)+heap_size%(end_heap%)
  103. `$ size%=end_memory%-start_memory%
  104. a% dif%=new_location%-start_memory%
  105.  debugmode% 
  106. verbose 
  107. debug_message("Heap move [{start_heap%} @ {start_memory%}:{end_heap%} @ {end_memory%}]->{new_location%}, dif={dif%}")
  108.  dif%>0 
  109. d*  copy_memory_backward!0=start_memory%
  110. e*  copy_memory_backward!4=new_location%
  111. f"  copy_memory_backward!8=size%
  112.  copy_memory_backward+12
  113. i)  copy_memory_forward!0=start_memory%
  114. j)  copy_memory_forward!4=new_location%
  115. k!  copy_memory_forward!8=size%
  116.  copy_memory_forward+12
  117.  byte%=start_heap% 
  118.  end_heap%
  119. o.  heap_base%(byte%)=heap_base%(byte%)+dif%
  120. p3  !(
  121. anchor_from_heap(byte%))=heap_base%(byte%)
  122.  byte%
  123.  start_heap%=1 
  124.  external_heapbase%=new_location%
  125.  end_heap%=active_heaps% 
  126.  heaps_top%=(end_memory%+dif%)
  127. v% external_heapbase%=new_location%
  128.  heaps_top%=new_location%
  129. =new_location%
  130. resize_heap(heap%,new_size%)
  131. ael("resize_heap")
  132.  heap_size%(heap%)=new_size% 
  133.  (new_size% 
  134.  4)<>0 
  135.  new_size%=(new_size%-(new_size% 
  136.  4))+4
  137.  debugmode% 
  138. verbose 
  139. debug_message("Resizing "+heap_comments$(heap%)+" ({heap%}) to "+
  140. hexadecimal(new_size%)+"...")
  141.  heap%>0 
  142.  heap%<=active_heaps% 
  143.  active_heaps%>0 
  144.  new_size%<heap_size%(heap%) 
  145. resize_heap_smaller(heap%,new_size%)
  146.  new_size%>heap_size%(heap%) 
  147. resize_heap_larger(heap%,new_size%)
  148.   heap_size%(heap%)=new_size%
  149. C heaps_top%=heap_size%(active_heaps%)+heap_base%(active_heaps%)
  150.  debugmode% 
  151. debug_message("resize_heap received illegal request for heap "+
  152. (heap%))
  153. resize_heap(heap%,new_size%)
  154. resize_heap(heap%,new_size%)
  155. resize_heap_smaller(heap%,new_size%)
  156. ael("resize_heap_smaller")
  157.  dif%
  158. $dif%=new_size%-heap_size%(heap%)
  159.  active_heaps%>heap% 
  160.  dummy%
  161. G dummy%=
  162. move_heaps(heap%+1,active_heaps%,heap_base%(heap%+1)+dif%)
  163. slotsize%+=dif%
  164.  "Wimp_SlotSize",slotsize%+1024,-1
  165. resize_heap_larger(heap%,new_size%)
  166. ael("resize_heap_larger")
  167.  dif%
  168. $dif%=new_size%-heap_size%(heap%)
  169.  ((slotsize%+&8000)-heaps_top%)<dif% 
  170. ael("slotsize_increase")
  171.  increase%,old_slotsize%
  172. 2 increase%=dif%-((slotsize%+&8000)-heaps_top%)
  173.  debugmode% 
  174. verbose 
  175. debug_message("Increasing slotsize by "+
  176. hexadecimal(increase%))
  177.  "Wimp_SlotSize",slotsize%+increase%+2048,-1 
  178.  new_slotsize%
  179.  new_slotsize%<slotsize%+increase% 
  180. msg("MemoryError.SlotSizeIncrease")
  181.  slotsize%+=increase%
  182.  active_heaps%>heap% 
  183. E dif%=
  184. move_heaps(heap%+1,active_heaps%,heap_base%(heap%+1)+dif%)
  185. delete_heap(heap%)
  186. ael("delete_heap")
  187.  heaps%
  188.  debugmode% 
  189. debug_message("Deleting {heap_comments$(heap%)} ({heap%})...")
  190. resize_heap(heap%,0)
  191. delete_heap_anchor(
  192. anchor_from_heap(heap%))
  193.  heap%<active_heaps% 
  194.  heaps%=heap%+1 
  195.  active_heaps%
  196. -  heap_base%(heaps%-1)=heap_base%(heaps%)
  197. -  heap_size%(heaps%-1)=heap_size%(heaps%)
  198. 5  heap_comments$(heaps%-1)=heap_comments$(heaps%)
  199. 1  heapsanchor%(heaps%-1)=heapsanchor%(heaps%)
  200.  heaps%
  201.  heaps%=1 
  202.  anchorsheap%(heaps%)>heap% 
  203.  anchorsheap%(heaps%)-=1
  204.  heaps%
  205. active_heaps%-=1
  206. heap_from_anchor(anchor%)
  207.  debugmode% 
  208.  heap%
  209. 4 heap%=anchorsheap%((anchor%-heap_anchors%) 
  210.  heap%<1 
  211. debug_message("{error$}.heap_from_anchor: resulting heap too low (anchor={anchor%}, heap={heap%})"):=0
  212.  heap%>active_heaps% 
  213. debug_message("{error$}.heap_from_anchor: resulting heap above limit (anchor={anchor%}, heap={heap%})")
  214. .=anchorsheap%((anchor%-heap_anchors%) 
  215. anchor_from_heap(heap%)
  216. *=heap_anchors%+(heapsanchor%(heap%)*4)
  217. compact_heaps
  218.  debugmode% 
  219.  heaps_top%<>heap_size%(active_heaps%)+heap_base%(active_heaps%) 
  220.  debugmode% 
  221. debug_message("heaps_top was not sum of top heap size,base! ["+error$+"]")
  222. $slotsize%=(heaps_top%+128)-&8000
  223.  "Wimp_SlotSize",slotsize%+2048,-1
  224. copy_memory(from%,to%,length%)
  225.  bytes%
  226.  bytes%=0 
  227.  length%
  228.  to%?bytes%=from%?bytes%
  229.  bytes%
  230. new_heap_anchor(heap%)
  231.  anchor%,answer%
  232. answer%=-1
  233.  anchor%=0 
  234.  heap_anchorsfree%?anchor%=0 
  235.   answer%=anchor%
  236.   anchor%=256
  237.  anchor%
  238.  answer%=-1 
  239. msg("NoAnchors")
  240. heap_anchorsfree%?answer%=1
  241. anchorsheap%(answer%)=heap%
  242. heapsanchor%(heap%)=answer%
  243.  debugmode% 
  244. verbose 
  245. debug_message("Allocating new anchor, number "+
  246. (answer%)+" is free...")
  247. =heap_anchors%+(answer%*4)
  248. delete_heap_anchor(anchor%)
  249. 'anchor%=(anchor%-heap_anchors%) 
  250.  debugmode% 
  251. verbose 
  252. debug_message("Deleting anchor "+
  253. (anchor%)+"...")
  254. heap_anchorsfree%?anchor%=0
  255. check_heap_limits(anchor%,required_size%,extra%)
  256.  debugmode% 
  257. ael("check_heap_limits")
  258.  heap%
  259. $heap%=
  260. heap_from_anchor(anchor%)
  261.  heap_size%(heap%)<required_size% 
  262. resize_heap(heap%,required_size%+extra%)
  263.  debugmode% 
  264.