Coverage report: /development/source/library/com/dydra/gitlab/dydra-cgi/ffi/lisp/dydra-ndk/package.lisp

KindCoveredAll%
expression2051 39.2
branch02 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 (in-package :common-lisp)
2
 
3
 (defpackage :dydra-ndk
4
   (:use :cl :cffi :cffi-sys)
5
   (:shadow :defcfun)
6
   (:export ;; package.lisp:
7
            :load-library
8
            :unload-library
9
            :defcfun
10
            :defcfunwi
11
            ;; account.lisp:
12
            :account-exists-p
13
            ;; error.lisp:
14
            :error
15
            :foreign-function-error
16
            :report-query-error
17
            ;; quad.lisp:
18
            :quad
19
            :make-quad
20
            :quad-graph-id
21
            :quad-subject-id
22
            :quad-predicate-id
23
            :quad-object-id
24
            :print-quad
25
            ;; quad_cursor.lisp:
26
            :quad-cursor
27
            :make-quad-cursor
28
            :free-quad-cursor
29
            :quad-cursor-id
30
            :quad-cursor-graph-id
31
            :quad-cursor-subject-id
32
            :quad-cursor-predicate-id
33
            :quad-cursor-object-id
34
            :quad-cursor-term
35
            :quad-cursor-quad
36
            :quad-cursor-position
37
            :quad-cursor-count
38
            :quad-cursor-close
39
            :quad-cursor-next
40
            :quad-cursor-skip
41
            :print-quad-cursor
42
            :count-matched-quads
43
            ;; query.lisp:
44
            :bgp-query
45
            :sparql-query
46
            :record-query-accounting
47
            ;; repository.lisp:
48
            :repository-exists-p
49
            :repository-creation-time
50
            :repository-mutation-time
51
            :create-repository
52
            :drop-repository
53
            ;; revision.lisp:
54
            :revision-creation-time
55
            ;; statement.lisp:
56
            :statement-urn-string
57
            :statement-id
58
            :statement-subject-id
59
            :statement-predicate-id
60
            :statement-object-id
61
            ;; string.lisp:
62
            :string-collate
63
            :string-compare
64
            ;; term.lisp:
65
            :term-id
66
            :term-compare
67
            :term-datatype-id
68
            :term-value-as-string
69
            :term-value-as-boolean
70
            :term-value-as-float
71
            :term-value-as-double
72
            :term-value-as-int8
73
            :term-value-as-uint8
74
            :term-value-as-int16
75
            :term-value-as-uint16
76
            :term-value-as-int32
77
            :term-value-as-uint32
78
            :term-value-as-int64
79
            :term-value-as-uint64
80
            :term-value-as-integer
81
            :term-value-as-decimal
82
            :term-value-as-decoded-time
83
            :term-value-as-universal-time
84
            :term-value-as-unix-time
85
            ;; term-matrix.lisp:
86
            :make-term-matrix
87
            :term-matrix-construct
88
            :term-matrix-destruct
89
            :term-matrix-clone
90
            :term-matrix-dispose
91
            :term-matrix-retain
92
            :term-matrix-release
93
            :term-matrix-empty-p
94
            :term-matrix-size
95
            :term-matrix-data-pointer
96
            :term-matrix-row-count
97
            :term-matrix-row-size
98
            :term-matrix-column-count
99
            :term-matrix-element-count
100
            :term-matrix-get
101
            :term-matrix-set
102
            :term-matrix-clear
103
            :term-matrix-clear-row
104
            :term-matrix-clear-column
105
            :term-matrix-fill
106
            :term-matrix-fill-row
107
            :term-matrix-fill-column
108
            :term-matrix-drop-column
109
            :term-matrix-drop-columns
110
            :term-matrix-append
111
            :term-matrix-append-row
112
            :term-matrix-append-rows
113
            :term-matrix-append-column
114
            :term-matrix-append-columns
115
            :term-matrix-resize
116
            :term-matrix-sort
117
            :term-matrix-sort-by
118
            :print-term-matrix
119
            ;; time.lisp:
120
            :encode-time
121
            :decode-time
122
            :format-time
123
            ;; uuid.lisp:
124
            :uuid
125
            :uuid-string
126
            ;; version.lisp:
127
            :+version-string+
128
            :+version-major+
129
            :+version-minor+
130
            :+version-patch+)
131
   (:shadow :error))
132
 
133
 (in-package :dydra-ndk)
134
 
135
 (eval-when (:compile-toplevel :load-toplevel :execute)
136
   (pushnew #P"/opt/dydra/lib/" *foreign-library-directories* :test #'equal)
137
   (define-foreign-library libdydra
138
     (:unix (:or "libdydra.so.0" "libdydra.so"))
139
     (:darwin (:or "libdydra.0.dylib" "libdydra.dylib"))
140
     (t (:default "libdydra"))))
141
 
142
 (defcvar ("dydra_error_callback" *error-callback*) :pointer)
143
 
144
 (defcallback error-callback (:boolean :int8) ((errno :int) (message :string) (function :string))
145
   (restart-case (foreign-function-error errno function message)
146
     (retry () :report "Retry calling the foreign function." t)
147
     (continue () :report "Continue regardless, returning from the foreign function." nil)))
148
 
149
 (defun load-library (&key path version debug features)
150
   "Loads the libdydra native library.
151
    Must be called before invoking any foreign functions in the library."
152
   (declare (type boolean debug)
153
            (type list features)
154
            (ignorable path version debug features))
155
   (load-foreign-library 'libdydra)
156
   (setf *error-callback* (callback error-callback))
157
   (values)) ;;; no meaningful return value
158
 
159
 (defun unload-library ()
160
   "Unloads the libdydra native library."
161
   (close-foreign-library 'libdydra)
162
   (values)) ;;; no meaningful return value
163
 
164
 (defmacro with-checked-pointer ((ptr) &rest body)
165
   (let* ((ptr-var (gensym))
166
          (body (or body (list ptr-var))))
167
     `(let ((,ptr-var ,ptr))
168
        (declare (type foreign-pointer ,ptr-var))
169
        (if (null-pointer-p ,ptr-var)
170
          (cl:error "~A is a null pointer." ,ptr-var)
171
          (progn ,@body)))))
172
 
173
 (defmacro with-checked-pointers ((&rest ptrs) &rest body)
174
   (let ((result `(progn ,@body)))
175
     (dolist (ptr (reverse ptrs))
176
       (setf result `(with-checked-pointer (,ptr) ,result)))
177
     result))
178
 
179
 ;;; cffi extension as c++ unwind workaround
180
 (defparameter *called-ffi-operators* ())
181
 (defparameter *last-ffi-operator* ())
182
 
183
 (defmacro dydra-ndk:defcfun ((foreign-name lisp-name &rest name-options) ffi-return &rest ffi-args)
184
   (let ((wrapped-name (intern (concatenate 'string "%." (symbol-name lisp-name)) (symbol-package lisp-name)))
185
         (args (mapcar #'first ffi-args)))
186
     `(progn (cffi:defcfun (,foreign-name ,wrapped-name ,@name-options) ,ffi-return ,@ffi-args)
187
             (declaim (inline ,lisp-name))
188
             (defun ,lisp-name ,args
189
               (sb-sys:without-interrupts
190
                ;; track the last called - not thread-safe, but close enough
191
                ;; 2020-12-22: removed tracking
192
                ;; (setf (getf *called-ffi-operators* ',lisp-name) (incf *count-ffi-operations*))
193
                ;; an unblocked thread was still free to modify this
194
                ;;(setq *last-ffi-operator* (cons ',lisp-name (bt:thread-name (bt:current-thread))))
195
                (,wrapped-name ,@args)
196
                )))))
197
 
198
 (defmacro dydra-ndk:defcfunwi (&rest args)
199
   `(dydra-ndk:defcfun ,@args))
200