Skip to content

Instantly share code, notes, and snippets.

@NalaGinrut
Created July 19, 2013 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NalaGinrut/6037304 to your computer and use it in GitHub Desktop.
Save NalaGinrut/6037304 to your computer and use it in GitHub Desktop.
efficient bytevector-slice for GNU Guile
(use-modules (system foreign) (rnrs bytevector))
;; TODO:
;; 1. (> hi (bytevector-length bv))
;; 2. (< lo 0) wrap reference
(define (%bytevector-slice bv lo hi)
(and (< hi lo) (error %bytevector-slice "wrong range" lo hi))
(let* ((ptr (bytevector->pointer bv))
(addr (pointer-address ptr))
(la (+ addr lo))
(len (- hi lo)))
(pointer->bytevector (make-pointer la) len)))
(define-syntax bytevector-slice
(syntax-rules (:)
((_ bv lo : hi)
(%bytevector-slice bv lo hi))
((_ bv lo :)
(%bytevector-slice bv lo (bytevector-length bv)))
((_ bv : hi)
(%bytevector-slice bv 0 hi))))
@NalaGinrut
Copy link
Author

OK, this implementation is unsafe for GC, we have to define an obj with (make-object-property) in top-level to hold the original bytevector in case it was collected by GC.
But this means I have to lock the obj each time, no, no more lock as possible. I prefer copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment