cs: add malloc support for 'interior mode

Since immobile vectors now exist at the Chez Scheme level, it's easy
to support 'interior mode (similar to 'interior-atomic).
This commit is contained in:
Matthew Flatt 2021-04-04 07:51:34 -06:00
parent 725710e445
commit dcaf358c64
3 changed files with 14 additions and 10 deletions

View File

@ -289,14 +289,15 @@ specification is required at minimum:
This allocation mode corresponds to
@cpp{scheme_malloc_atomic_allow_interior} in the C API.}
@item{@indexed-racket['nonatomic-interior] --- Like
@item{@indexed-racket['interior] --- Like
@racket['nonatomic], but the allocated object will not be moved
by the garbage collector as long as the allocated object is
retained.
This mode is supported only for the @BC[] Racket implementation, and
it corresponds to @cpp{scheme_malloc_allow_interior} in the C
API.}
For the @BC[] Racket implementation, a reference can point
to the interior of the object, instead of its starting address.
This allocation mode corresponds to
@cpp{scheme_malloc_allow_interior} in the C API.}
@item{@indexed-racket['tagged] --- Allocates memory that must
start with a @tt{short} value that is registered as a tag with
@ -337,7 +338,8 @@ If no mode is specified, then @racket['nonatomic] allocation is used
when the type is a @racket[_gcpointer]- or @racket[_scheme]-based
type, and @racket['atomic] allocation is used otherwise.
@history[#:changed "6.4.0.10" @elem{Added the @racket['tagged] allocation mode.}]}
@history[#:changed "6.4.0.10" @elem{Added the @racket['tagged] allocation mode.}
#:changed "8.0.0.13" @elem{Changed CS to support the @racket['interior] allocation mode.}]}
@defproc[(free [cptr cpointer?]) void]{

View File

@ -747,7 +747,7 @@
_pointer))))
;; test 'interior allocation mode
(when (eq? 'racket (system-type 'vm))
(let ()
;; Example by Ron Garcia
(define-struct data (a b))
(define (cbox s)

View File

@ -1437,12 +1437,14 @@
[(eq? mode 'nonatomic)
(make-cpointer (#%make-vector (quotient size ptr-size-in-bytes) 0) #f)]
[(eq? mode 'atomic-interior)
;; This is not quite the same as traditional Racket, because
;; a finalizer is associated with the cpointer (as opposed to
;; the address that is wrapped by the cpointer). Also, interior
;; pointers are not allowed as GCable pointers.
;; This is not quite the same as Racket BC, because interior
;; pointers are not allowed as GCable pointers. So, "interior"
;; just means "doesn't move".
(let* ([bstr (make-immobile-bytevector size)])
(make-cpointer bstr #f))]
[(eq? mode 'interior)
;; Ditto
(make-cpointer (#%make-immobile-vector (quotient size ptr-size-in-bytes) 0) #f)]
[else
(raise-unsupported-error 'malloc
(format "'~a mode is not supported" mode))]))