diff --git a/collects/ffi/unsafe.rkt b/collects/ffi/unsafe.rkt index 83f54aecd7..de7ebb0f77 100644 --- a/collects/ffi/unsafe.rkt +++ b/collects/ffi/unsafe.rkt @@ -973,7 +973,7 @@ ;; (_array ...+) (provide _array - array? array-ptr + array? array-length array-ptr (protect-out array-ref array-set!)) (define _array @@ -985,11 +985,11 @@ [(t n . ns) (_array (apply _array t ns) n)])) -(define-struct array (ptr type len)) +(define-struct array (ptr type length)) (define array-ref (case-lambda [(a i) - (let ([len (array-len a)]) + (let ([len (array-length a)]) (if (< -1 i len) (ptr-ref (array-ptr a) (array-type a) i) (raise-mismatch-error 'array-ref "index out of bounds: " i)))] @@ -1001,7 +1001,7 @@ (define array-set! (case-lambda [(a i v) - (let ([len (array-len a)]) + (let ([len (array-length a)]) (if (< -1 i len) (ptr-set! (array-ptr a) (array-type a) i v) (raise-mismatch-error 'array-ref "index out of bounds: " i)))] diff --git a/collects/scribblings/foreign/types.scrbl b/collects/scribblings/foreign/types.scrbl index 9ea720e7bf..28a354a262 100644 --- a/collects/scribblings/foreign/types.scrbl +++ b/collects/scribblings/foreign/types.scrbl @@ -1137,6 +1137,13 @@ sub-array).} Extracts the pointer for an array's storage.} +@defproc[(array-length [a array?]) exact-nonnegative-integer?]{ + +Extracts the length of an array. For a multidimensional array, the +result is still a single number; extract an element to get +a sub-array to get the length of the next dimension, and so on.} + + @defproc[(_array/list [type ctype?] [count exact-nonnegative-integer?] ...+) ctype?]{ diff --git a/collects/tests/racket/foreign-test.rktl b/collects/tests/racket/foreign-test.rktl index 8ba6625bcc..942709094c 100644 --- a/collects/tests/racket/foreign-test.rktl +++ b/collects/tests/racket/foreign-test.rktl @@ -223,6 +223,7 @@ (test (for/list ([i 7]) (+ i 10)) cast p _pointer (_list o _byte 7)) (t (for/list ([i 7]) (+ i 11)) 'increment_c_array (_fun _pointer -> (_list o _byte 7)) p) (let ([a (ptr-ref p (_array _byte 7))]) + (test 7 array-length a) (test 12 array-ref a 1) (ptr-set! p _byte 1 17) (test 17 array-ref a 1)))