changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/std/alien.lisp

changeset 7: 05527b920c97
parent: 32ed719c5189
child: 61482ce290f9
author: ellis <ellis@rwest.io>
date: Mon, 16 Oct 2023 23:11:19 -0400
permissions: -rw-r--r--
description: asd cleanups
1 ;;; std/alien.lisp --- foreign alien friends
2 
3 ;;; Commentary:
4 
5 ;; FFI in Lisp is somewhat different than FFI in other host langs. As
6 ;; such, we usually refer to our Lispy FFI interfaces inline with the
7 ;; CMUCL terminology: alien interfaces.
8 
9 ;; ref: https://www.sbcl.org/manual/#Foreign-Function-Interface for details
10 
11 ;; sb-alien is a high-level interface which automatically converts C
12 ;; memory pointers to lisp objects and back, but this can be slow for
13 ;; large or complex objects.
14 
15 ;; The lower-level interface is based on System Area Pointers (or
16 ;; SAPs), which provide untyped access to foreign memory.
17 
18 ;; Objects which can't be automatically converted into Lisp values are
19 ;; represented by objects of type ALIEN-VALUE.
20 
21 ;;; Code:
22 (uiop:define-package :std/alien
23  (:nicknames :alien)
24  (:use :sb-vm :sb-ext :sb-c :std/base)
25  (:use-reexport :sb-alien)
26  (:export
27  :copy-c-string
28  :clone
29  :foreign-int-to-integer :foreign-int-to-bool :bool-to-foreign-int
30  :defbytes
31  :u1 :u2 :u3 :u4 :u8 :u16 :u24 :u32 :u64 :u128
32  :i2 :i3 :i4 :i8 :i16 :i24 :i32 :i64 :i128
33  :f16 :f24 :f32 :f64 :f128))
34 
35 (in-package :alien)
36 
37 ;; (reexport-from :sb-vm
38 ;; :include
39 ;; '(:with-pinned-objects :with-pinned-object-iterator :with-code-pages-pinned
40 ;; :sanctify-for-execution))
41 
42 (defun copy-c-string (src dest &aux (index 0))
43  (loop (let ((b (sb-sys:sap-ref-8 src index)))
44  (when (= b 0)
45  (setf (fill-pointer dest) index)
46  (return))
47  (setf (char dest index) (code-char b))
48  (incf index))))
49 
50 (defun clone-strings (list)
51  (with-alien ((x (* (* char))
52  (make-alien (* char) (length list))))
53  (unwind-protect
54  (labels ((populate (list index function)
55  (if list
56  (let ((array (sb-ext:string-to-octets (car list) :null-terminate t)))
57  (sb-sys:with-pinned-objects (array)
58  (setf (deref x index) (sap-alien (sb-sys:vector-sap array) (* char)))
59  (populate (cdr list) (1+ index) function)))
60  (funcall function))))
61  (populate list 0
62  (lambda ()
63  (loop for i below (length list)
64  do (print (cast (deref x i) c-string))))))
65  (free-alien x))))
66 
67 (defun foreign-int-to-integer (buffer size)
68  "Check SIZE of int BUFFER. return BUFFER."
69  (assert (= size (sb-alien:alien-size sb-alien:int :bytes)))
70  buffer)
71 
72 (defun foreign-int-to-bool (x size)
73  (if (zerop (foreign-int-to-integer x size))
74  nil
75  t))
76 
77 (defun bool-to-foreign-int (val)
78  (if val 1 0))
79 
80 ;;; Bytes
81 (defmacro defbytes (&body bitsets)
82  "For each cons-cell in BITSETS, define a new CAR-byte type for each
83 member of CDR."
84  `(loop for set in ',bitsets
85  collect
86  (let* ((ty (car set))
87  (pfx
88  (cond
89  ((eq 'signed-byte ty) "I")
90  ((eq 'unsigned-byte ty) "U")
91  ((eq 'float ty) "F")
92  (t (subseq (symbol-name ty) 0 1))))
93  (nums (cdr set))
94  r) ;result
95  (setf r
96  (mapc
97  (lambda (x)
98  `(deftype ,(symbolicate pfx (format 'nil "~a" x)) ()
99  (cons ,ty ,x)))
100  nums))
101  (cons ty r))))
102 
103 (defbytes
104  (unsigned-byte 1 2 3 4 8 16 24 32 64 128)
105  (signed-byte 2 3 4 8 16 24 32 64 128)
106  (float 16 24 32 64 128))