Skip to content
Browse files

生のバイト列操作用のパッケージを作成

  • Loading branch information...
1 parent cf6fa60 commit 23a493f847b4e08ea36371d0e6004ece04fd8ff8 @sile committed Sep 3, 2011
Showing with 112 additions and 31 deletions.
  1. +8 −0 aio.asd
  2. +31 −30 aio.lisp
  3. +48 −0 alien/bytes/bytes.lisp
  4. +9 −0 alien/bytes/package.lisp
  5. +16 −1 package.lisp
View
8 aio.asd
@@ -17,6 +17,14 @@
(:file "alien/epoll/type")
(:file "alien/epoll/function")
(:file "alien/epoll/epoll")
+
+ (:file "alien/io/package")
+ (:file "alien/io/function")
+ (:file "alien/io/io")
+
+ (:file "alien/bytes/package")
+ (:file "alien/bytes/bytes")
+
(:file "package")
(:file "aio")
))
View
61 aio.lisp
@@ -1,4 +1,4 @@
-(in-package :aio)
+(in-package :aio) ;; filename: aio_event.lisp
#|
(defparameter *fd->handler* (make-hash-table))
@@ -44,34 +44,6 @@
(defun free-bytes (bytes)
(aio.alien:free-bytes bytes))
-;; XXX:
-(defun nonblock-read (fd bytes size &aux (ptr (sb-alien:alien-sap bytes)))
- (multiple-value-bind (ret ok)
- (aio.alien:o-nonblock fd)
- (if (not ok)
- (values nil ret)
- (if ret
- (values (sb-unix:unix-read fd ptr size)
- (aio.alien::errsym))
- (progn ; XXX: check
- (setf (aio.alien:o-nonblock fd) t)
- (multiple-value-prog1 (values (sb-unix:unix-read fd ptr size) (aio.alien::errsym))
- (setf (aio.alien:o-nonblock fd) nil)))))))
-
-;; TODO: native-ioパッケージを用意? (fcntl, read, write)
-(defun nonblock-write (fd bytes size &aux (ptr (sb-alien:alien-sap bytes)))
- (multiple-value-bind (ret ok)
- (aio.alien:o-nonblock fd)
- (if (not ok)
- (values nil ret)
- (if ret
- (values (sb-unix:unix-write fd ptr 0 size)
- (aio.alien::errsym))
- (progn ; XXX: check
- (setf (aio.alien:o-nonblock fd) t)
- (multiple-value-prog1 (values (sb-unix:unix-write fd ptr 0 size) (aio.alien::errsym))
- (setf (aio.alien:o-nonblock fd) nil)))))))
-
(defun to-lisp-string (bytes size)
(sb-ext:octets-to-string
(coerce (loop FOR i FROM 0 BELOW size
@@ -184,4 +156,33 @@
`(unless ,matched
,@default))
t)))
-
+
+;;;;;;;;;;;;;;;
+(defun read (fd bytes size &key force-nonblock)
+ (aio.alien.io:read fd bytes size :force-nonblock force-nonblock))
+
+(defun write (fd bytes size &key force-nonblock)
+ (aio.alien.io:write fd bytes size :force-nonblock force-nonblock))
+
+(defmacro ensure-nonblock ((fd) &body body)
+ `(aio.alien.io:ensure-nonblock (,fd)
+ ,@body))
+
+;;;;;;;;;;;;;;;
+(defun make-bytes (size)
+ (aio.alien.bytes:alloc-bytes size))
+
+(defun to-bytes (lisp-bytes)
+ (aio.alien.bytes:to-bytes lisp-bytes))
+
+(defun from-bytes (bytes)
+ (aio.alien.bytes:from-bytes bytes))
+
+(defun subbytes (bytes start &optional end)
+ (aio.alien.bytes:subbytes bytes start end))
+
+(defun byte-ref (bytes index)
+ (aio.alien.bytes:ref bytes index))
+
+(defun bytes-size (bytes)
+ (aio.alien.bytes:size bytes))
View
48 alien/bytes/bytes.lisp
@@ -0,0 +1,48 @@
+(in-package :aio.alien.bytes)
+
+(defstruct bytes
+ (ptr 0 :type (alien (* (unsigned 8))))
+ (start 0 :type fixnum)
+ (end 0 :type fixnum))
+
+(defun bytes-size (bytes)
+ (with-slots (start end) bytes
+ (- end start)))
+
+(defmethod print-object ((o bytes) stream)
+ (print-unreadable-object (o stream :identity t)
+ (format stream "~a ~s ~a" :bytes :size (bytes-size o))))
+
+(defun size (bytes)
+ (bytes-size bytes))
+
+(defun alloc-bytes (size)
+ (let ((o (make-alien (unsigned 8) size)))
+ (sb-ext:finalize (make-bytes :ptr o :start 0 :end size)
+ (lambda () (free-alien o)))))
+
+(defun ref (bytes index)
+ (with-slots (ptr start) (the bytes bytes)
+ (deref ptr (+ start index))))
+
+(defun (setf ref) (new-value bytes index)
+ (with-slots (ptr start) (the bytes bytes)
+ (setf (deref ptr (+ start index)) new-value)))
+
+(defun to-bytes (lisp-octets &key (start 0) (end (length lisp-octets)))
+ (let* ((len (- end start))
+ (bytes (alloc-bytes len)))
+ (dotimes (i len bytes)
+ (setf (ref bytes i) (aref lisp-octets (+ i start))))))
+
+(defun from-bytes (bytes &key (start 0) (end (bytes-size bytes)))
+ (let* ((len (- end start))
+ (octets (make-array len :element-type '(unsigned-byte 8))))
+ (dotimes (i len octets)
+ (setf (aref octets i) (ref bytes (+ i start))))))
+
+(defun subbytes (bytes start &optional end)
+ (with-slots (ptr (offset start)) bytes
+ (make-bytes :ptr ptr
+ :start (+ offset start)
+ :end (+ offset end))))
View
9 alien/bytes/package.lisp
@@ -0,0 +1,9 @@
+(defpackage aio.alien.bytes
+ (:use :common-lisp :sb-alien)
+ (:export alloc-bytes
+ to-bytes
+ from-bytes
+ size
+ ref
+ subbytes))
+(in-package :aio.alien.bytes)
View
17 package.lisp
@@ -1,6 +1,8 @@
(defpackage aio
(:use :common-lisp)
- (:export *default-context*
+ (:shadow :common-lisp read write)
+ (:export ;; TODO: aio.event
+ *default-context*
context
create-context
@@ -19,6 +21,19 @@
event-rdhup
event-et
event-oneshot
+
+ ;; TODO: aio.io
+ ensure-nonblock
+ read
+ write
+
+ ;; TODO: aio.bytes
+ make-bytes
+ to-bytes
+ from-bytes
+ subbytes
+ byte-ref
+ bytes-size
))
(in-package :aio)

0 comments on commit 23a493f

Please sign in to comment.
Something went wrong with that request. Please try again.