X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=sidebyside;f=doc%2Fref.sgml;h=f299c197cee87f90e5045c6c1ddca6b4d89198c1;hb=340539a1be525ce05c7c06472c291fedaf09684a;hp=d8d70c04e421e0c14a3c7734bf3a30997caa00ff;hpb=267d85c58ea3dac5081b20e8ec8701c8dfb009c3;p=uffi.git
diff --git a/doc/ref.sgml b/doc/ref.sgml
index d8d70c0..f299c19 100644
--- a/doc/ref.sgml
+++ b/doc/ref.sgml
@@ -96,8 +96,12 @@ dereferenced :char pointer returns an character.
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.
@@ -884,7 +888,7 @@ can be freed.
size
- An optional size parameter. If specified, allocates and returns an
+ 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.
@@ -1046,6 +1050,61 @@ much better with static allocation.
+
+
+ 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
@@ -1436,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."))))
+
+
@@ -1543,7 +1643,7 @@ that returns a cstring.
Side Effects
- None.
+ On some implementations, this function allocates memory.
Affected by
@@ -1585,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.
@@ -1645,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.