memory2.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; An implementation of Pre-Scheme's memory interface that can detect some
  3. ; stray reads and writes. It has numerous limitiations:
  4. ; Allocations are always on page boundaries.
  5. ; No more than 16 megabytes can be allocated at once.
  6. ; More than 32 or 64 or so allocations result in addresses being
  7. ; bignums (dealloctions have no effect on this).
  8. ;
  9. ; Memory is represented as a vector of byte-vectors, with each byte-vector
  10. ; representing a 16-megabyte page. Allocations are always made on page
  11. ; boundaries, so the byte-vectors only need be as large as the allocated
  12. ; areas. Pages are never re-used.
  13. ; Memory is one big vector, with markers at beginning and end of free blocks
  14. ; and allocationg by by address ordered first fit.
  15. ; Strings and things end up as bignums...
  16. (define *memory* (make-vector 0 0))
  17. (define-record-type address :address
  18. (make-address index)
  19. address?
  20. (index address-index))
  21. (define-record-discloser :address
  22. (lambda (addr) (list 'address (address-index addr))))
  23. ; We add 100000000 to addresses to make them
  24. (define address-offset 100000000)
  25. (define (address->integer addr)
  26. (+ (address-index addr) address-offset))
  27. (define (integer->address int)
  28. (make-address (- int address-offset)))
  29. (define (word-ref addr)
  30. (if (address? addr)
  31. (let ((index (address-index addr)))
  32. (if (and (= 0
  33. (vector-ref *memory* (address-index addr)))
  34. (define (word-ref addr)
  35. (vector-ref *memory* (address-index addr)))
  36. (define *memory* (make-vector 16 #f)) ; vector of pages
  37. (define log-max-size 24) ; log of page size
  38. (define address-shift (- log-max-size)) ; turns addresses into page indices
  39. (define max-size (arithmetic-shift 1 log-max-size)) ; page size
  40. (define address-mask ; mask to get address within page
  41. (- (arithmetic-shift 1 log-max-size) 1))
  42. (define *next-index* 0) ; next available page
  43. (define (reinitialize-memory)
  44. (set! *memory* (make-vector 16 #f))
  45. (set! *next-index* 0))
  46. ; Extend the page vector if necessary, and then make a page of the
  47. ; appropriate size.
  48. (define (allocate-memory size)
  49. (cond ((> size max-size)
  50. #f) ; the null pointer
  51. (else
  52. (if (>= *next-index* (vector-length *memory*))
  53. (let ((new (make-vector (* 2 (vector-length *memory*)))))
  54. (do ((i 0 (+ i 1)))
  55. ((>= i (vector-length *memory*)))
  56. (vector-set! new i (vector-ref *memory* i)))
  57. (set! *memory* new)))
  58. (let ((index *next-index*))
  59. (set! *next-index* (+ *next-index* 1))
  60. (vector-set! *memory* index (make-code-vector size 0))
  61. (arithmetic-shift index log-max-size)))))
  62. ; Turning an address into a page or page index
  63. (define (address->vector address)
  64. (vector-ref *memory* (arithmetic-shift address address-shift)))
  65. (define (address->vector-index address)
  66. (bitwise-and address address-mask))
  67. ; Throw away the page containing ADDRESS, which must be the first address in
  68. ; that page,
  69. (define (deallocate-memory address)
  70. (let ((vector (address->vector address))
  71. (byte-address (address->vector-index address)))
  72. (if (and vector (= byte-address 0))
  73. (vector-set! *memory* (arithmetic-shift address address-shift) #f)
  74. (error "bad deallocation address" address))))
  75. ; Various ways of accessing memory
  76. (define (unsigned-byte-ref address)
  77. (code-vector-ref (address->vector address)
  78. (address->vector-index address)))
  79. (define (signed-code-vector-ref bvec i)
  80. (let ((x (code-vector-ref bvec i)))
  81. (if (< x 128)
  82. x
  83. (bitwise-ior x -128))))
  84. (define (word-ref address)
  85. (let ((vector (address->vector address))
  86. (byte-address (address->vector-index address)))
  87. (if (not (= 0 (bitwise-and byte-address 3)))
  88. (error "unaligned address error" address)
  89. (+ (+ (arithmetic-shift (signed-code-vector-ref vector byte-address) 24)
  90. (arithmetic-shift (code-vector-ref vector (+ byte-address 1)) 16))
  91. (+ (arithmetic-shift (code-vector-ref vector (+ byte-address 2)) 8)
  92. (code-vector-ref vector (+ byte-address 3)))))))
  93. (define (unsigned-byte-set! address value)
  94. (code-vector-set! (address->vector address)
  95. (address->vector-index address)
  96. (bitwise-and 255 value)))
  97. (define (word-set! address value)
  98. (let ((vector (address->vector address))
  99. (byte-address (address->vector-index address)))
  100. (if (not (= 0 (bitwise-and byte-address 3)))
  101. (error "unaligned address error" address))
  102. (code-vector-set! vector byte-address
  103. (bitwise-and 255 (arithmetic-shift value -24)))
  104. (code-vector-set! vector (+ byte-address 1)
  105. (bitwise-and 255 (arithmetic-shift value -16)))
  106. (code-vector-set! vector (+ byte-address 2)
  107. (bitwise-and 255 (arithmetic-shift value -8)))
  108. (code-vector-set! vector (+ byte-address 3)
  109. (bitwise-and 255 value))))
  110. ; Block I/O procedures.
  111. (define (write-block port address count)
  112. (let ((vector (address->vector address))
  113. (byte-address (address->vector-index address)))
  114. (do ((i 0 (+ i 1)))
  115. ((>= i count))
  116. (write-char (ascii->char (code-vector-ref vector (+ i byte-address)))
  117. port))
  118. (values count (enum errors no-errors))))
  119. (define (read-block port address count)
  120. (cond ((not (byte-ready? port))
  121. (values 0 #f (enum errors no-errors)))
  122. ((eof-object? (peek-byte port))
  123. (values 0 #t (enum errors no-errors)))
  124. (else
  125. (let ((vector (address->vector address))
  126. (byte-address (address->vector-index address)))
  127. (let loop ((i 0))
  128. (if (or (= i count)
  129. (not (byte-ready? port)))
  130. (values i #f (enum errors no-errors))
  131. (let ((b (read-byte port)))
  132. (cond ((eof-object? b)
  133. (values i #f (enum errors no-errors)))
  134. (else
  135. (code-vector-set! vector
  136. (+ i byte-address)
  137. b)
  138. (loop (+ i 1)))))))))))
  139. (define (copy-memory! from to count)
  140. (let ((from (address-index from))
  141. (to (address-index to)))
  142. (let ((from-vector (address->vector from))
  143. (from-address (address->vector-index from))
  144. (to-vector (address->vector to))
  145. (to-address (address->vector-index to)))
  146. (if (>= from-address to-address)
  147. (do ((i 0 (+ i 1)))
  148. ((>= i count))
  149. (code-vector-set! to-vector
  150. (+ i to-address)
  151. (code-vector-ref from-vector
  152. (+ i from-address))))
  153. (do ((i (- count 1) (- i 1)))
  154. ((negative? i))
  155. (code-vector-set! to-vector
  156. (+ i to-address)
  157. (code-vector-ref from-vector
  158. (+ i from-address))))))))
  159. (define (memory-equal? from to count)
  160. (let ((from-vector (address->vector from))
  161. (from-address (address->vector-index from))
  162. (to-vector (address->vector to))
  163. (to-address (address->vector-index to)))
  164. (let loop ((i 0))
  165. (cond ((>= i count)
  166. #t)
  167. ((= (code-vector-ref to-vector (+ i to-address))
  168. (code-vector-ref from-vector (+ i from-address)))
  169. (loop (+ i 1)))
  170. (else
  171. #f)))))
  172. ; Turn the LENGTH bytes starting from ADDRESS into a string.
  173. (define (char-pointer->string address length)
  174. (let ((vector (address->vector address))
  175. (byte-address (address->vector-index address))
  176. (string (make-string length)))
  177. (do ((i 0 (+ i 1)))
  178. ((= i length))
  179. (string-set! string
  180. i
  181. (ascii->char (code-vector-ref vector (+ byte-address i)))))
  182. string))
  183. ; Turn the bytes from ADDRESS to the next nul (byte equal to 0) into a
  184. ; string. This is a trivial operation in C.
  185. (define (char-pointer->nul-terminated-string address)
  186. (let ((vector (address->vector address))
  187. (byte-address (address->vector-index address)))
  188. (char-pointer->string address (index-of-first-nul vector byte-address))))
  189. (define (index-of-first-nul vector address)
  190. (let loop ((i address))
  191. (cond ((= i (code-vector-length vector))
  192. (error "CHAR-POINTER->STRING called on pointer with no nul termination"))
  193. ((= 0 (code-vector-ref vector i))
  194. (- i address))
  195. (else
  196. (loop (+ i 1))))))
  197. ; We really need these to work.
  198. (define address+ +)
  199. (define address- -)
  200. (define address-difference -)
  201. (define address->integer identity)
  202. (define integer->address identity)
  203. (define ...)