r3448: *** empty log message ***
[uffi.git] / doc / ref.sgml
index fcb08ebf8af2ca1e2e2e45629fcfe73eee1a7eef..39ae02ebaf9a920fceb1b76cef1a1671bd0f70fb 100644 (file)
@@ -96,8 +96,12 @@ dereferenced :char pointer returns an character.</para>
 pointer returns an character.</para>
          </listitem>
          <listitem>
-           <para><constant>:byte</constant> - Unsigned 8-bits. A
+           <para><constant>:byte</constant> - Signed 8-bits. A
 dereferenced :byte pointer returns an integer.</para>
+          </listitem>
+         <listitem>
+           <para><constant>:unsigned-byte</constant> - Unsigned 8-bits. A
+dereferenced :unsigned-byte pointer returns an integer.</para>
          </listitem>
          <listitem>
            <para><constant>:short</constant> - Signed 16-bits.</para>
@@ -877,15 +881,15 @@ can be freed.
          <varlistentry>
            <term><parameter>type</parameter></term>
            <listitem>
-             <para>A unevaluated type of foreign object to allocate.
+             <para>The type of foreign object to allocate. This parameter is evaluated.
              </para>
            </listitem>
          </varlistentry>
          <varlistentry>
            <term><parameter>size</parameter></term>
            <listitem>
-             <para>An optional size parameter. If specified, allocates and returns an
-array of <parameter>type</parameter> that is <parameter>size</parameter> members long.
+             <para>An optional size parameter that is evaluated. If specified, allocates and returns an
+array of <parameter>type</parameter> that is <parameter>size</parameter> members long. This parameter is evaluated.
              </para>
            </listitem>
          </varlistentry>
@@ -908,7 +912,7 @@ array of <parameter>type</parameter> that is <parameter>size</parameter> members
        <title>Examples</title>
        <programlisting>
 (def-struct ab (a :int) (b :double))
-(allocate-foreign-object ab)
+(allocate-foreign-object 'ab)
 => #&lt;ptr&gt;
        </programlisting>
       </refsect1>
@@ -973,6 +977,134 @@ array of <parameter>type</parameter> that is <parameter>size</parameter> members
     </refentry>
 
 
+    <refentry id="with-foreign-object">
+      <refnamediv>
+       <refname>with-foreign-object</refname>
+       <refpurpose>Wraps the allocation of a foreign object around a body of code.
+       </refpurpose>
+       <refclass>Macro</refclass>
+      </refnamediv>
+      <refsynopsisdiv>
+       <title>Syntax</title>
+       <synopsis>
+         <function>with-foreign-object</function> <replaceable>(var type) &amp;body body</replaceable> => <returnvalue>form-return</returnvalue>
+       </synopsis>
+      </refsynopsisdiv>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>var</parameter></term>
+           <listitem>
+             <para>The variable name to bind.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>type</parameter></term>
+           <listitem>
+             <para>The type of foreign object to allocate. This parameter is evaluated.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><returnvalue>form-return</returnvalue></term>
+           <listitem>
+             <para>The result of evaluating the <parameter>body</parameter>.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>
+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.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <programlisting>
+(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."))))
+       </programlisting>
+      </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="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>
@@ -1363,12 +1495,23 @@ 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 
+
+         &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.
+
+cstrings are used as parameters to and from functions. An
+implementation, such as CMUCL and Lispworks, a cstring may not be a
+foreign type but rather the Lisp string itself while on other
+platforms a cstring is a newly allocated foreign vector for storing
+characters. Thus, it is not possible to portably
+<emphasis>allocate</emphasis> a cstring. 
+</para>
+<para>
+In contrast, foreign strings
+are always a foreign vector of characters which have a memory
+allocated to hold them. Because of this, if you need to allocate memory to
+hold the return value of a string, use a foreign string and not a cstring.
        </para>
 </partintro>
 
@@ -1470,7 +1613,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>
@@ -1572,7 +1715,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>
@@ -1955,6 +2098,91 @@ a session.
       </refsect1>
     </refentry>
 
+      <refentry id="find-foreign-library">
+       <refnamediv>
+         <refname>find-foreign-library</refname>
+       <refpurpose>Finds a foreign library file.
+       </refpurpose>
+       <refclass>Function</refclass>
+      </refnamediv>
+      <refsect1>
+       <title>Syntax</title>
+<synopsis>
+         <function>find-foreign-library</function> <replaceable>names directories &amp; drive-letters types</replaceable> => <returnvalue>path</returnvalue>
+</synopsis>
+      </refsect1>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>names</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the base name of the library file.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>directories</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the directory the library file.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>drive-letters</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the drive letters for the library file.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>types</parameter></term>
+           <listitem>
+             <para>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.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><returnvalue>path</returnvalue></term>
+           <listitem>
+             <para>A path containing the path found, or &nil; if the library file was not found.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>Finds a foreign library by searching through a number of possible locations. Returns
+the path of the first found file.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <programlisting>
+(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"
+       </programlisting>
+      </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>
+
 </reference>