X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=doc%2Fmanual%2Fffi.texinfo;h=78267ca4a4bcbf3926a6018b79c888a2fb0f316a;hb=8a33bf220856487a5cde4b183476b6ab5103983a;hp=12aa045b9d7831d77f8dd59c580fd96be43f7d62;hpb=74cfbf6d0572b7df1b3492563408a7cb3ae103cf;p=sbcl.git diff --git a/doc/manual/ffi.texinfo b/doc/manual/ffi.texinfo index 12aa045..78267ca 100644 --- a/doc/manual/ffi.texinfo +++ b/doc/manual/ffi.texinfo @@ -284,11 +284,13 @@ return zero values. @item @cindex External formats -The foreign type specifier @code{(sb-alien:c-string &key external-format -element-type)} is similar to @code{(* char)}, but is interpreted as a -null-terminated string, and is automatically converted into a Lisp -string when accessed; or if the pointer is C @code{NULL} or @code{0}, -then accessing it gives Lisp @code{nil}. +The foreign type specifier @code{(sb-alien:c-string &key +external-format element-type not-null)} is similar to +@code{(* char)}, but is interpreted as a null-terminated string, and +is automatically converted into a Lisp string when accessed; or if the +pointer is C @code{NULL} or @code{0}, then accessing it gives Lisp +@code{nil} unless @code{not-null} is true, in which case a type-error +is signalled. External format conversion is automatically done when Lisp strings are passed to foreign code, or when foreign strings are passed to Lisp code. @@ -340,13 +342,17 @@ the variable. @item @code{sb-alien} also exports translations of these C type -specifiers as foreign type specifiers: @code{sb-alien:char}, -@code{sb-alien:short}, @code{sb-alien:int}, -@code{sb-alien:long}, @code{sb-alien:unsigned-char}, -@code{sb-alien:unsigned-short}, -@code{sb-alien:unsigned-int}, -@code{sb-alien:unsigned-long}, @code{sb-alien:float}, and -@code{sb-alien:double}. +specifiers as foreign type specifiers: +@code{char}, +@code{short}, +@code{int}, +@code{long}, +@code{unsigned-char}, +@code{unsigned-short}, +@code{unsigned-int}, +@code{unsigned-long}, +@code{float}, @code{double}, +@code{size-t}, and @code{off-t}. @end itemize @@ -475,60 +481,15 @@ record type. @subsection Foreign Dynamic Allocation Lisp code can call the C standard library functions @code{malloc} and -@code{free} to dynamically allocate and deallocate foreign -variables. The Lisp code shares the same allocator with foreign C -code, so it's OK for foreign code to call @code{free} on the result of -Lisp @code{sb-alien:make-alien}, or for Lisp code to call -@code{sb-alien:free-alien} on foreign objects allocated by C -code. - -@defmac @sbalien{make-alien} @var{type} @var{size} - -The @code{sb-alien:make-alien} macro -returns a dynamically allocated foreign value of the specified -@var{type} (which is not evaluated.) The allocated memory is not -initialized, and may contain arbitrary junk. If supplied, -@var{size} is an expression to evaluate to compute the size of the -allocated object. There are two major cases: - -@itemize -@item -When @var{type} is a foreign array type, an array of that type is -allocated and a pointer to it is returned. Note that you must use -@code{deref} to change the result to an array before you can use -@code{deref} to read or write elements: - -@lisp -(cl:in-package "CL-USER") ; which USEs package "SB-ALIEN" -(defvar *foo* (make-alien (array char 10))) -(type-of *foo*) @result{} (alien (* (array (signed 8) 10))) -(setf (deref (deref foo) 0) 10) @result{} 10 -@end lisp - -If supplied, @var{size} is used as the first dimension for the - array. - -@item -When @var{type} is any other foreign type, then an object for that -type is allocated, and a pointer to it is returned. So -@code{(make-alien int)} returns a @code{(* int)}. If @var{size} is -specified, then a block of that many objects is allocated, with the -result pointing to the first one. - -@end itemize - -@end defmac - -@defun @sbalien{free-alien} @var{foreign-value} - -The @code{sb-alien:free-alien} macro -frees the storage for @var{foreign-value}, -which must have been allocated with Lisp @code{make-alien} -or C @code{malloc}. - -See also the @code{sb-alien:with-alien} macro, which allocates foreign -values on the stack. -@end defun +@code{free} to dynamically allocate and deallocate foreign variables. +The Lisp code shares the same allocator with foreign C code, so it's +OK for foreign code to call @code{free} on the result of Lisp +@code{sb-alien:make-alien}, or for Lisp code to call +@code{sb-alien:free-alien} on foreign objects allocated by C code. + +@include macro-sb-alien-make-alien.texinfo +@include fun-sb-alien-make-alien-string.texinfo +@include fun-sb-alien-free-alien.texinfo @node Foreign Variables @comment node-name, next, previous, up