PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fth/t_alloc.fth

https://github.com/cataska/pforth
Forth | 116 lines | 102 code | 14 blank | 0 comment | 1 complexity | b976cc3d5514556dce7a57ba41b8cc47 MD5 | raw file
  1. \ @(#) t_alloc.fth 97/01/28 1.4
  2. \ Test PForth ALLOCATE
  3. \
  4. \ Copyright 1994 3DO, Phil Burk
  5. anew task-t_alloc.fth
  6. decimal
  7. 64 constant NUM_TAF_SLOTS
  8. variable TAF-MAX-ALLOC
  9. variable TAF-MAX-SLOT
  10. \ hold addresses and sizes
  11. NUM_TAF_SLOTS array TAF-ADDRESSES
  12. NUM_TAF_SLOTS array TAF-SIZES
  13. : TAF.MAX.ALLOC? { | numb addr ior maxb -- max }
  14. 0 -> maxb
  15. \ determine maximum amount we can allocate
  16. 1024 40 * -> numb
  17. BEGIN
  18. numb 0>
  19. WHILE
  20. numb allocate -> ior -> addr
  21. ior 0=
  22. IF \ success
  23. addr free abort" Free failed!"
  24. numb -> maxb
  25. 0 -> numb
  26. ELSE
  27. numb 1024 - -> numb
  28. THEN
  29. REPEAT
  30. maxb
  31. ;
  32. : TAF.INIT ( -- )
  33. NUM_TAF_SLOTS 0
  34. DO
  35. 0 i taf-addresses !
  36. LOOP
  37. \
  38. taf.max.alloc? ." Total Avail = " dup . cr
  39. dup taf-max-alloc !
  40. NUM_TAF_SLOTS / taf-max-slot !
  41. ;
  42. : TAF.ALLOC.SLOT { slotnum | addr size -- }
  43. \ allocate some RAM
  44. taf-max-slot @ 8 -
  45. choose 8 +
  46. dup allocate abort" Allocation failed!"
  47. -> addr
  48. -> size
  49. addr slotnum taf-addresses !
  50. size slotnum taf-sizes !
  51. \
  52. \ paint RAM with slot number
  53. addr size slotnum fill
  54. ;
  55. : TAF.FREE.SLOT { slotnum | addr size -- }
  56. slotnum taf-addresses @ -> addr
  57. \ something allocated so check it and free it.
  58. slotnum taf-sizes @ 0
  59. DO
  60. addr i + c@ slotnum -
  61. IF
  62. ." Error at " addr i + .
  63. ." , slot# " slotnum . cr
  64. abort
  65. THEN
  66. LOOP
  67. addr free abort" Free failed!"
  68. 0 slotnum taf-addresses !
  69. ;
  70. : TAF.DO.SLOT { slotnum -- }
  71. slotnum taf-addresses @ 0=
  72. IF
  73. slotnum taf.alloc.slot
  74. ELSE
  75. slotnum taf.free.slot
  76. THEN
  77. ;
  78. : TAF.TERM
  79. NUM_TAF_SLOTS 0
  80. DO
  81. i taf-addresses @
  82. IF
  83. i taf.free.slot
  84. THEN
  85. LOOP
  86. \
  87. taf.max.alloc? dup ." Final MAX = " . cr
  88. ." Original MAX = " taf-max-alloc @ dup . cr
  89. = IF ." Test PASSED." ELSE ." Test FAILED!" THEN cr
  90. ;
  91. : TAF.TEST ( NumTests -- )
  92. 1 max
  93. dup . ." tests" cr \ flushemit
  94. taf.init
  95. ." Please wait for test to complete..." cr
  96. 0
  97. DO NUM_TAF_SLOTS choose taf.do.slot
  98. LOOP
  99. taf.term
  100. ;
  101. .( Testing ALLOCATE and FREE) cr
  102. 10000 taf.test