16 Unsafe Operations
(require racket/unsafe/ops) |
All fuctions and forms provided by racket/base and racket check their arguments to ensure that the arguments conform to contracts and other constraints. For example, vector-ref checks its arguments to ensure that the first argument is a vector, that the second argument is an exact integer, and that the second argument is between 0 and one less than the vector’s length, inclusive.
Functions provided by racket/unsafe/ops are unsafe. They have certain constraints, but the constraints are not checked, which allows the system to generate and execute faster code. If arguments violate an unsafe function’s constraints, the function’s behavior and result is unpredictable, and the entire system can crash or become corrupted.
All of the exported bindings of racket/unsafe/ops are protected in the sense of protect-out, so access to unsafe operations can be prevented by adjusting the code inspector (see Code Inspectors).
16.1 Unsafe Numeric Operations
(unsafe-fx+ a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fx- a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fx* a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxquotient a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxremainder a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxmodulo a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxabs a) → fixnum? a : fixnum?
(unsafe-fxand a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxior a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxxor a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxnot a) → fixnum? a : fixnum?
(unsafe-fxlshift a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxrshift a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fx= a b) → boolean? a : fixnum? b : fixnum?
(unsafe-fx< a b) → boolean? a : fixnum? b : fixnum?
(unsafe-fx> a b) → boolean? a : fixnum? b : fixnum?
(unsafe-fx<= a b) → boolean? a : fixnum? b : fixnum?
(unsafe-fx>= a b) → boolean? a : fixnum? b : fixnum?
(unsafe-fxmin a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fxmax a b) → fixnum? a : fixnum? b : fixnum?
(unsafe-fl+ a b) → inexact-real? a : inexact-real? b : inexact-real?
(unsafe-fl- a b) → inexact-real? a : inexact-real? b : inexact-real?
(unsafe-fl* a b) → inexact-real? a : inexact-real? b : inexact-real?
(unsafe-fl/ a b) → inexact-real? a : inexact-real? b : inexact-real?
(unsafe-flabs a) → inexact-real? a : inexact-real?
(unsafe-fl= a b) → boolean? a : inexact-real? b : inexact-real?
(unsafe-fl< a b) → boolean? a : inexact-real? b : inexact-real?
(unsafe-fl> a b) → boolean? a : inexact-real? b : inexact-real?
(unsafe-fl<= a b) → boolean? a : inexact-real? b : inexact-real?
(unsafe-fl>= a b) → boolean? a : inexact-real? b : inexact-real?
(unsafe-flmin a) → inexact-real? a : inexact-real?
(unsafe-flmax a) → inexact-real? a : inexact-real?
(unsafe-flround a) → inexact-real? a : inexact-real?
(unsafe-flfloor a) → inexact-real? a : inexact-real?
(unsafe-flceiling a) → inexact-real? a : inexact-real?
(unsafe-fltruncate a) → inexact-real? a : inexact-real?
(unsafe-flsin a) → inexact-real? a : inexact-real?
(unsafe-flcos a) → inexact-real? a : inexact-real?
(unsafe-fltan a) → inexact-real? a : inexact-real?
(unsafe-flasin a) → inexact-real? a : inexact-real?
(unsafe-flacos a) → inexact-real? a : inexact-real?
(unsafe-flatan a) → inexact-real? a : inexact-real?
(unsafe-fllog a) → inexact-real? a : inexact-real?
(unsafe-flexp a) → inexact-real? a : inexact-real?
(unsafe-flsqrt a) → inexact-real? a : inexact-real?
(unsafe-make-flrectangular a b) → (and/c complex? inexact? (not/c real?)) a : inexact-real? b : inexact-real?
(unsafe-flreal-part a) → inexact-real? a : (and/c complex? inexact? (not/c real?))
(unsafe-flimag-part a) → inexact-real? a : (and/c complex? inexact? (not/c real?))
(unsafe-fx->fl a) → inexact-real? a : fixnum?
(unsafe-fl->fx a) → fixnum? a : inexact-real?
16.2 Unsafe Data Extraction
(unsafe-car p) → any/c p : pair?
(unsafe-cdr p) → any/c p : pair?
(unsafe-mcar p) → any/c p : mpair?
(unsafe-mcdr p) → any/c p : mpair?
(unsafe-set-mcar! p v) → void? p : mpair? v : any/c
(unsafe-set-mcdr! p v) → void? p : mpair? v : any/c
(unsafe-list-ref lst pos) → any/c lst : any/c pos : (and/c exact-nonnegative-integer? fixnum?)
(unsafe-list-tail lst pos) → any/c lst : any/c pos : (and/c exact-nonnegative-integer? fixnum?)
(unsafe-unbox b) → fixnum? b : box?
(unsafe-set-box! b k) → void? b : box? k : fixnum?
(unsafe-unbox* v) → any/c v : (and/c box? (not/c impersonator?))
(unsafe-set-box*! v val) → void? v : (and/c box? (not/c impersonator?)) val : any/c
(unsafe-vector-length v) → fixnum? v : vector?
(unsafe-vector-ref v k) → any/c v : vector? k : fixnum?
(unsafe-vector-set! v k val) → void? v : vector? k : fixnum? val : any/c
(unsafe-vector*-length v) → fixnum? v : (and/c vector? (not/c impersonator?))
(unsafe-vector*-ref v k) → any/c v : (and/c vector? (not/c impersonator?)) k : fixnum?
(unsafe-vector*-set! v k val) → void? v : (and/c vector? (not/c impersonator?)) k : fixnum? val : any/c
A vector’s size can never be larger than a fixnum, so even vector-length always returns a fixnum.
(unsafe-string-length str) → fixnum? str : string?
(unsafe-string-ref str k) → (and/c char? (lambda (ch) (<= 0 (char->integer ch) 255))) str : string? k : fixnum?
(unsafe-string-set! str k ch) → void? str : (and/c string? (not/c immutable?)) k : fixnum? ch : char?
(unsafe-bytes-length bstr) → fixnum? bstr : bytes?
(unsafe-bytes-ref bstr k) → byte? bstr : bytes? k : fixnum?
(unsafe-bytes-set! bstr k b) → void? bstr : (and/c bytes? (not/c immutable?)) k : fixnum? b : byte?
(unsafe-flvector-length v) → fixnum? v : flvector?
(unsafe-flvector-ref v k) → any/c v : flvector? k : fixnum?
(unsafe-flvector-set! v k x) → void? v : flvector? k : fixnum? x : inexact-real?
(unsafe-f64vector-ref vec k) → inexact-real? vec : f64vector? k : fixnum?
(unsafe-f64vector-set! vec k n) → void? vec : f64vector? k : fixnum? n : inexact-real?
(unsafe-s16vector-ref vec k) → (integer-in -32768 32767) vec : s16vector? k : fixnum?
(unsafe-s16vector-set! vec k n) → void? vec : s16vector? k : fixnum? n : (integer-in -32768 32767)
(unsafe-u16vector-ref vec k) → (integer-in 0 65535) vec : u16vector? k : fixnum?
(unsafe-u16vector-set! vec k n) → void? vec : u16vector? k : fixnum? n : (integer-in 0 65535)
(unsafe-struct-ref v k) → any/c v : any/c k : fixnum?
(unsafe-struct-set! v k val) → void? v : any/c k : fixnum? val : any/c
(unsafe-struct*-ref v k) → any/c v : (not/c impersonator?) k : fixnum?
(unsafe-struct*-set! v k val) → void? v : (not/c impersonator?) k : fixnum? val : any/c