memory.scm 9.3 KB

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