Created
July 19, 2013 07:21
-
-
Save NalaGinrut/6037304 to your computer and use it in GitHub Desktop.
efficient bytevector-slice for GNU Guile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.