r3593: *** empty log message ***
[uffi.git] / doc / ref.sgml
index 1058e484bdd2b7016bd12d9bc8c4e01d22313120..f299c197cee87f90e5045c6c1ddca6b4d89198c1 100644 (file)
@@ -1050,6 +1050,61 @@ much better with static allocation.
       </refsect1>
     </refentry>
 
+    <refentry id="size-of-foreign-type">
+      <refnamediv>
+       <refname>size-of-foreign-type</refname>
+       <refpurpose>Returns the number of data bytes used by a foreign object type.
+       </refpurpose>
+       <refclass>Macro</refclass>
+      </refnamediv>
+      <refsynopsisdiv>
+       <title>Syntax</title>
+       <synopsis>
+         <function>size-of-foreign-type</function> <replaceable>ftype</replaceable>
+       </synopsis>
+      </refsynopsisdiv>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>ftype</parameter></term>
+           <listitem>
+             <para>A foreign type specifier. This parameter is evaluated.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>
+         Returns the number of data bytes used by a foreign object type. This does not include any Lisp storage overhead.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <para>
+<programlisting>
+(size-of-foreign-object :unsigned-byte)
+=> 1
+(size-of-foreign-object 'my-100-byte-vector-type)
+=> 100
+</programlisting>
+        </para>
+      </refsect1>
+      <refsect1>
+       <title>Side Effects</title>
+       <para>None.</para>
+      </refsect1>      <refsect1>
+       <title>Affected by</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Exceptional Situations</title>
+       <para>None.</para>
+      </refsect1>
+    </refentry>
+
     <refentry id="pointer-address">
       <refnamediv>
        <refname>pointer-address</refname>
@@ -1440,13 +1495,54 @@ if a cstring returned by a function is &null;.
 <partintro>
 <title>Overview</title>
 <para>
-         &uffi; has functions to two types of <varname>C</varname>-compatible 
-         strings, <emphasis>cstring</emphasis> and <emphasis>foreign</emphasis> 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.
-       </para>
+
+         &uffi; has functions to two types of
+<varname>C</varname>-compatible
+         strings: <emphasis>cstring</emphasis> and
+<emphasis>foreign</emphasis> strings.
+
+cstrings are used <emphasis>only</emphasis> 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.  
+</para>
+
+<programlisting>
+(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))))
+</programlisting>
+
+<para> 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.  </para>
+
+<programlisting>
+(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."))))
+</programlisting>
+
 </partintro>
 
     <refentry id="convert-from-cstring">
@@ -1547,7 +1643,7 @@ that returns a cstring.
       </refsect1>
       <refsect1>
        <title>Side Effects</title>
-       <para>None.</para>
+       <para>On some implementations, this function allocates memory.</para>
       </refsect1>
       <refsect1>
        <title>Affected by</title>
@@ -1589,7 +1685,7 @@ that returns a cstring.
        <title>Description</title>
        <para>
          Frees any memory possibly allocated by
-         <function>convert-to-cstring</function>.
+         <function>convert-to-cstring</function>. On some implementions, a cstring is just the Lisp string itself.
        </para>
       </refsect1>
       <refsect1>
@@ -1649,7 +1745,7 @@ that returns a cstring.
       <refsect1>
        <title>Description</title>
        <para>
-         Binds a lexical variable to a newly allocated <varname>cstring</varname>. Automatically frees <varname>cstring</varname>.
+         Binds a symbol to a cstring created from conversion of a string. Automatically frees the <varname>cstring</varname>.
        </para>
       </refsect1>
       <refsect1>