X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=sidebyside;f=doc%2Fref.sgml;h=f299c197cee87f90e5045c6c1ddca6b4d89198c1;hb=eb03e5da9d2a2918aff4004597205b36a32f7f6e;hp=2eb2e1c438ca9bae4c460e9b8b29afba26bcf5e0;hpb=fb93b1923db347f01bdebc7226e5e1eaacacc9f9;p=uffi.git
diff --git a/doc/ref.sgml b/doc/ref.sgml
index 2eb2e1c..f299c19 100644
--- a/doc/ref.sgml
+++ b/doc/ref.sgml
@@ -90,16 +90,18 @@
:char - Signed 8-bits. A
dereferenced :char pointer returns an character.
-
:unsigned-char - Unsigned 8-bits. A dereferenced :unsigned-char
pointer returns an character.
-
- :byte - Unsigned 8-bits. A
+ :byte - Signed 8-bits. A
dereferenced :byte pointer returns an integer.
+
+
+ :unsigned-byte - Unsigned 8-bits. A
+dereferenced :unsigned-byte pointer returns an integer.
:short - Signed 16-bits.
@@ -317,7 +319,7 @@ abstracts the difference in implementations where some return a
Examples
-(def-array ca :char)
+(def-array-pointer ca :unsigned-char)
(let ((fs (convert-to-foreign-string "ab")))
(values (null-char-p (deref-array fs 'ca 0))
(null-char-p (deref-array fs 'ca 2))))
@@ -498,7 +500,7 @@ structure. It's type is :pointer-self.
- def-slot-value
+ get-slot-value
Retrieves a value from a slot of a structure.
Macro
@@ -642,17 +644,17 @@ structure. It's type is :pointer-self.
-
+
- def-array
- Defines a foreign array type.
+ def-array-pointer
+ Defines a pointer to a array of type.
Macro
Syntax
- def-array name type
+ def-array-pointer name type
@@ -677,13 +679,13 @@ structure. It's type is :pointer-self.
Description
- Defines a foreign array type.
+ Defines a type tat is a pointer to an array of type.
Examples
-(def-array byte-array :unsigned-char)
+(def-array-pointer byte-array-pointer :unsigned-char)
@@ -778,6 +780,75 @@ the array.
None.
+
+
+
+ def-union
+ Defines a foreign union type.
+
+ Macro
+
+
+ Syntax
+
+ def-union name &rest fields
+
+
+
+ Arguments and Values
+
+
+ name
+
+ A name of the new union type.
+
+
+
+
+ fields
+
+ A list of fields of the union.
+
+
+
+
+
+
+ Description
+
+ Defines a foreign union type.
+
+
+
+ Examples
+
+(def-union test-union
+ (a-char :char)
+ (an-int :int))
+
+(let ((u (allocate-foreign-object 'test-union))
+ (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
+ (prog1
+ (ensure-char-character (get-slot-value u 'test-union 'a-char))
+ (free-foreign-object u)))
+=> #\A
+
+
+
+ Side Effects
+ Defines a new foreign type.
+
+
+ Affected by
+ None.
+
+
+ Exceptional Situations
+ None.
+
+
+
+
@@ -785,7 +856,8 @@ the array.
Overview
- Objects are entities that can allocated and freed.
+ Objects are entities that can allocated, referred to by pointers, and
+can be freed.
@@ -800,7 +872,7 @@ the array.
Syntax
- allocate-foreign-object type => ptr
+ allocate-foreign-object type &optional size => ptr
@@ -809,7 +881,15 @@ the array.
type
- A type of foreign object to allocate.
+ The type of foreign object to allocate. This parameter is evaluated.
+
+
+
+
+ size
+
+ An optional size parameter that is evaluated. If specified, allocates and returns an
+array of type that is size members long. This parameter is evaluated.
@@ -897,6 +977,134 @@ the array.
+
+
+ with-foreign-object
+ Wraps the allocation of a foreign object around a body of code.
+
+ Macro
+
+
+ Syntax
+
+ with-foreign-object (var type) &body body => form-return
+
+
+
+ Arguments and Values
+
+
+ var
+
+ The variable name to bind.
+
+
+
+
+ type
+
+ The type of foreign object to allocate. This parameter is evaluated.
+
+
+
+
+ form-return
+
+ The result of evaluating the body.
+
+
+
+
+
+
+ Description
+
+This function wraps the allocation, binding, and destruction of a foreign object.
+On &cmucl; and
+&lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
+much better with static allocation.
+
+
+
+ Examples
+
+(defun gethostname2 ()
+ "Returns the hostname"
+ (uffi:with-foreign-object (name '(:array :unsigned-char 256))
+ (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
+ (uffi:convert-from-foreign-string name)
+ (error "gethostname() failed."))))
+
+
+
+ Side Effects
+ None.
+
+
+ Affected by
+ None.
+
+
+ Exceptional Situations
+ None.
+
+
+
+
+
+ size-of-foreign-type
+ Returns the number of data bytes used by a foreign object type.
+
+ Macro
+
+
+ Syntax
+
+ size-of-foreign-type ftype
+
+
+
+ Arguments and Values
+
+
+ ftype
+
+ A foreign type specifier. This parameter is evaluated.
+
+
+
+
+
+
+ Description
+
+ Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
+
+
+
+ Examples
+
+
+(size-of-foreign-object :unsigned-byte)
+=> 1
+(size-of-foreign-object 'my-100-byte-vector-type)
+=> 100
+
+
+
+
+ Side Effects
+ None.
+
+ Affected by
+ None.
+
+
+ Exceptional Situations
+ None.
+
+
+
pointer-address
@@ -960,7 +1168,7 @@ the array.
Syntax
- def-pointer ptr type => value
+ deref-pointer ptr type => value
@@ -981,7 +1189,7 @@ the array.
- value
+ value
The value of the object where the pointer points.
@@ -999,9 +1207,76 @@ the array.
Examples
+(let ((intp (allocate-foreign-object :int)))
+ (setf (deref-pointer intp :int) 10)
+ (prog1
+ (deref-pointer intp :int)
+ (free-foreign-object intp)))
+=> 10
+
+
+
+
+ Side Effects
+ None.
+
+
+ Affected by
+ None.
+
+
+ Exceptional Situations
+ None.
+
+
+
+
+
+ ensure-char-character
+ Ensures that a dereferenced :char pointer is
+a character.
+
+ Macro
+
+
+ Syntax
+
+ ensure-char-character object => char
+
+
+
+ Arguments and Values
+
+
+ object
+
+ Either a character or a integer specifying a character code.
+
+
+
+
+ char
+
+ A character.
+
+
+
+
+
+
+ Description
+
+ Ensures that an object obtained by dereferencing a
+:char pointer is a character.
+
+
+
+ Examples
+
+
(let ((fs (convert-to-foreign-string "a")))
(prog1
- (ensure-char (deref-pointer fs :char))
+ (ensure-char-character (deref-pointer fs :char))
(free-foreign-object fs)))
=> #\a
@@ -1017,8 +1292,78 @@ the array.
Exceptional Situations
+ Depending upon the implementation and what &uffi; expects, this
+macro may signal an error if the object is not a character or
+integer.
+
+
+
+
+
+ ensure-char-integer
+ Ensures that a dereferenced :char pointer is
+an integer.
+
+ Macro
+
+
+ Syntax
+
+ ensure-char-integer object => int
+
+
+
+ Arguments and Values
+
+
+ object
+
+ Either a character or a integer specifying a character code.
+
+
+
+
+ int
+
+ An integer.
+
+
+
+
+
+
+ Description
+
+ Ensures that an object obtained by dereferencing a
+:char pointer is an integer.
+
+
+
+ Examples
+
+
+(let ((fs (convert-to-foreign-string "a")))
+ (prog1
+ (ensure-char-integer (deref-pointer fs :char))
+ (free-foreign-object fs)))
+=> 96
+
+
+
+
+ Side Effects
+ None.
+
+
+ Affected by
None.
+
+ Exceptional Situations
+ Depending upon the implementation and what &uffi; expects, this
+macro may signal an error if the object is not a character or
+integer.
+
@@ -1150,13 +1495,54 @@ if a cstring returned by a function is &null;.
Overview
- &uffi; has functions to two types of C-compatible
- strings, cstring and foreign strings.
-cstrings are used as parameters to and from functions. An implementation, such as CMUCL,
-may not convert these to a foreign type for efficiency sake. Thus, it is not
-possible to "allocate" a cstring. In contrast, foreign strings
-always need to have memory for them.
-
+
+ &uffi; has functions to two types of
+C-compatible
+ strings: cstring and
+foreign strings.
+
+cstrings are used only as parameters to and from
+functions. In some implementations a cstring is not a foreign type but
+rather the Lisp string itself. On other platforms a cstring is a newly
+allocated foreign vector for storing characters. The following is an
+example of using cstrings to both send and return a value.
+
+
+
+(uffi:def-function ("getenv" c-getenv)
+ ((name :cstring))
+ :returning :cstring)
+
+(defun my-getenv (key)
+ "Returns an environment variable, or NIL if it does not exist"
+ (check-type key string)
+ (uffi:with-cstring (key-native key)
+ (uffi:convert-from-cstring (c-getenv key-native))))
+
+
+ In contrast, foreign strings are always a foreign vector of
+characters which have memory allocated. Thus, if you need to allocate
+memory to hold the return value of a string, you must use a foreign
+string and not a cstring. The following is an example of using a foreign
+string for a return value.
+
+
+(uffi:def-function ("gethostname" c-gethostname)
+ ((name (* :unsigned-char))
+ (len :int))
+ :returning :int)
+
+(defun gethostname ()
+ "Returns the hostname"
+ (let* ((name (uffi:allocate-foreign-string 256))
+ (result-code (c-gethostname name 256))
+ (hostname (when (zerop result-code)
+ (uffi:convert-from-foreign-string name))))
+ (uffi:free-foreign-object name)
+ (unless (zerop result-code)
+ (error "gethostname() failed."))))
+
+
@@ -1257,7 +1643,7 @@ that returns a cstring.
Side Effects
- None.
+ On some implementations, this function allocates memory.
Affected by
@@ -1299,7 +1685,7 @@ that returns a cstring.
Description
Frees any memory possibly allocated by
- convert-to-cstring.
+ convert-to-cstring. On some implementions, a cstring is just the Lisp string itself.
@@ -1359,7 +1745,7 @@ that returns a cstring.
Description
- Binds a lexical variable to a newly allocated cstring. Automatically frees cstring.
+ Binds a symbol to a cstring created from conversion of a string. Automatically frees the cstring.
@@ -1640,7 +2026,7 @@ foreign function. If :void indicates module does not return
Examples
(def-function "gethostname"
- ((name :cstring)
+ ((name (* :unsigned-char))
(len :int))
:returning :int)
@@ -1742,6 +2128,91 @@ a session.
+
+
+ find-foreign-library
+ Finds a foreign library file.
+
+ Function
+
+
+ Syntax
+
+ find-foreign-library names directories & drive-letters types => path
+
+
+
+ Arguments and Values
+
+
+ names
+
+ A string or list of strings containing the base name of the library file.
+
+
+
+
+ directories
+
+ A string or list of strings containing the directory the library file.
+
+
+
+
+ drive-letters
+
+ A string or list of strings containing the drive letters for the library file.
+
+
+
+
+ types
+
+ A string or list of strings containing the file type of the library file. Default
+is &nil;. If &nil;, will use a default type based on the currently running implementation.
+
+
+
+
+ path
+
+ A path containing the path found, or &nil; if the library file was not found.
+
+
+
+
+
+
+ Description
+ Finds a foreign library by searching through a number of possible locations. Returns
+the path of the first found file.
+
+
+
+ Examples
+
+(find-foreign-library '("libmysqlclient" "libmysql")
+ '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
+ :types '("so" "dll")
+ :drive-letters '("C" "D" "E"))
+=> #P"D:\\mysql\\lib\\opt\\libmysql.dll"
+
+
+
+ Side Effects
+ None.
+
+
+
+ Affected by
+ None.
+
+
+ Exceptional Situations
+ None.
+
+
+