X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=doc%2Fref.sgml;h=99234c410e200a23bb2ee9a97bd182e5436f3849;hb=db166e2970e1aaabd611e243eb899ae4d2f5f5ff;hp=607edfced873810076050e650872e0270ec179d5;hpb=d542c08a7adad816d6378fd6c80f62547033b866;p=uffi.git diff --git a/doc/ref.sgml b/doc/ref.sgml index 607edfc..99234c4 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. @@ -755,7 +759,7 @@ the array. Examples -(def-array ca :char) +(def-array-pointer ca :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)))) @@ -763,6 +767,14 @@ the array. &t; + + Notes + + The TYPE argument is ignored for CL implementations other than + AllegroCL. If you want to cast a pointer to another type use + WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY. + + Side Effects None. @@ -884,8 +896,8 @@ can be freed. size - An optional size parameter. If specified, allocates and returns an -array of type that is size members long. + 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 +1058,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 @@ -1157,6 +1224,14 @@ much better with static allocation. + + Notes + + The TYPE argument is ignored for CL implementations other than + AllegroCL. If you want to cast a pointer to another type use + WITH-CAST-POINTER together with DEREF-POINTER/DEREF-ARRAY. + + Side Effects None. @@ -1207,8 +1282,9 @@ a character. Description - Ensures that an object obtained by dereferencing a -:char pointer is a character. + Ensures that an objects obtained by dereferencing +:char and :unsigned-char +pointers are a lisp character. @@ -1429,6 +1505,92 @@ if a cstring returned by a function is &null;. + + + with-cast-pointer + Wraps a body of code with a pointer cast to a new type. + + Macro + + + Syntax + + with-cast-pointer (binding-name ptr type) & body body => value + + + + Arguments and Values + + + ptr + + A pointer to a foreign object. + + + + + type + + A foreign type of the object being pointed to. + + + + + value + + The value of the object where the pointer points. + + + + + + + Description + + Executes BODY with POINTER cast to be a pointer to type TYPE. If + BINDING-NAME is provided the cast pointer will be bound to this + name during the execution of BODY. If BINDING-NAME is not provided + POINTER must be a name bound to the pointer which should be + cast. This name will be bound to the cast pointer during the + execution of BODY. + + This is a no-op in AllegroCL but will wrap BODY in a LET form if + BINDING-NAME is provided. + + This macro is meant to be used in conjunction with DEREF-POINTER or + DEREF-ARRAY. In Allegro CL the "cast" will actually take place in + DEREF-POINTER or DEREF-ARRAY. + + + + Examples + +(with-foreign-object (size :int) + ;; FOO is a foreign function returning a :POINTER-VOID + (let ((memory (foo size))) + (when (mumble) + ;; at this point we know for some reason that MEMORY points + ;; to an array of unsigned bytes + (with-cast-pointer (memory :unsigned-byte) + (dotimes (i (deref-pointer size :int)) + (do-something-with + (deref-array memory '(:array :unsigned-byte) i))))))) + + + + Side Effects + None. + + + Affected by + None. + + + Exceptional Situations + None. + + + @@ -1436,13 +1598,73 @@ 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.")))) + + + Foreign functions that return pointers to freshly allocated +strings should in general not return cstrings, but foreign strings. +(There is no portable way to release such cstrings from Lisp.) The +following is an example of handling such a function. + + +(uffi:def-function ("readline" c-readline) + ((prompt :cstring)) + :returning (* :char)) + +(defun readline (prompt) + "Reads a string from console with line-editing." + (with-cstring (c-prompt prompt) + (let* ((c-str (c-readline c-prompt)) + (str (convert-from-foreign-string c-str))) + (uffi:free-foreign-object c-str) + str))) + + @@ -1543,7 +1765,7 @@ that returns a cstring. Side Effects - None. + On some implementations, this function allocates memory. Affected by @@ -1585,7 +1807,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 +1867,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. @@ -1955,7 +2177,7 @@ foreign function. If :void indicates module does not return Syntax - load-foreign-library filename &key module supporting-libraries => success + load-foreign-library filename &key module supporting-libraries force-load => success @@ -1986,6 +2208,13 @@ link the foreign library. (Required by CMUCL) + + force-load + + Forces the loading of the library if it has been previously loaded. + + + success @@ -2001,7 +2230,7 @@ otherwise &nil;. Description Loads a foreign library. Applies a module name to functions within the library. Ensures that a library is only loaded once during -a session. +a session. A library can be reloaded by using the :force-load key.