memory.scm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. (define (address1+ x)
  4. (address+ x addressing-units-per-cell))
  5. (define (address2+ x)
  6. (address1+ (address1+ x)))
  7. ; Memory access
  8. (define *memory*)
  9. (define *memory-begin* 0)
  10. (define *memory-end* 0)
  11. (define (memory-begin)
  12. *memory-begin*)
  13. ; Size of memory in cells.
  14. (define (memory-size)
  15. (a-units->cells (address-difference *memory-end* *memory-begin*)))
  16. (define (create-memory size initial-value) ;size in cells
  17. (let ((size (cells->a-units size)))
  18. (cond ((not (= size (address-difference *memory-end* *memory-begin*)))
  19. (if (not (= *memory-end* 0))
  20. (deallocate-memory *memory*))
  21. (set! *memory* (allocate-memory size))
  22. (if (null-address? *memory*)
  23. (error "out of memory, unable to continue"))
  24. (set! *memory-begin* *memory*)
  25. (set! *memory-end* (+ *memory* size))))))
  26. (define fetch word-ref)
  27. (define fetch-byte unsigned-byte-ref)
  28. (define fetch-flonum flonum-ref)
  29. (define store! word-set!)
  30. (define store-byte! unsigned-byte-set!)
  31. (define store-flonum! flonum-set!)
  32. (define fetch-string char-pointer->string)
  33. (define fetch-nul-terminated-string char-pointer->nul-terminated-string)
  34. ;----------------------------------------------------------------
  35. (define (address->stob-descriptor addr)
  36. (add-stob-tag (address->integer addr)))
  37. (define stob-overhead 1) ; header uses up one descriptor
  38. (define (address-after-header stob)
  39. (assert (stob? stob))
  40. (integer->address (remove-stob-tag stob)))
  41. (define (address-at-header stob)
  42. (address- (address-after-header stob)
  43. (cells->a-units 1)))
  44. (define (stob-header stob)
  45. (fetch (address-at-header stob)))
  46. (define (stob-header-set! stob header)
  47. (store! (address-at-header stob) header))