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 This allocation mode corresponds to
@cpp{scheme_malloc_atomic_allow_interior} in the C API.} @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 @racket['nonatomic], but the allocated object will not be moved
by the garbage collector as long as the allocated object is by the garbage collector as long as the allocated object is
retained. retained.
This mode is supported only for the @BC[] Racket implementation, and For the @BC[] Racket implementation, a reference can point
it corresponds to @cpp{scheme_malloc_allow_interior} in the C to the interior of the object, instead of its starting address.
API.} This allocation mode corresponds to
@cpp{scheme_malloc_allow_interior} in the C API.}
@item{@indexed-racket['tagged] --- Allocates memory that must @item{@indexed-racket['tagged] --- Allocates memory that must
start with a @tt{short} value that is registered as a tag with 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 when the type is a @racket[_gcpointer]- or @racket[_scheme]-based
type, and @racket['atomic] allocation is used otherwise. 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]{ @defproc[(free [cptr cpointer?]) void]{

View File

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

View File

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