Declaimation help

From: Dave Roberts (ldave-re-move_at_re-move.droberts.com)
Date: 03/31/04


Date: Wed, 31 Mar 2004 08:00:39 GMT

I'm trying to understand declarations and declaimations and I'm not really
sure I get it. Specifically, I understand how to declare variable types
with DECLARE within a function. No biggie there. My confusion is with
global type declarations, specifically for functions.

For instance, is there any value in a (DECLAIM (FTYPE (FUNCTION (<type>...)
<ret-type>) <function-name>)) for particular often-used functions? Would I
have to put this declaration in every file which makes a call to
<function-name>, or would just doing it once in a common file and the using
a defsystem like ASDF handle that correctly? My confusion is sort of around
how similar a (DECLAIM (FTYPE...)) is to a C .h file declaration.

For instance, can SBCL uses this to more effectively deal with calls to the
that function from other functions? If not, is there anything that will
help, or do you just rely on the compiler type inferencing being so
fantastic that it's able to deduce all the type info, including return type
info?

For instance, this is some code from the DNS resolver code I'm working on.
It uses UFFI to interface to Linux's resolver.so library. The GET-BYTE
function is called tremendously often to read bytes from the buffer. I have
declared types for the GET-BYTE function parameters and I have actually
declared the function inline in the DECLAIM. I also added the DECLAIM
FTYPE, but I'm not sure what the correct usage should be.

(uffi:def-type byte-ptr (* :unsigned-byte))
(deftype buffer-index () `(integer 0 ,(1- (expt 2 14))))
(deftype uint8 () '(unsigned-byte 8))
(deftype uint16 () '(unsigned-byte 16))
(deftype uint32 () '(unsigned-byte 32))
(deftype dns-pointer () `(integer 0 ,(1- (expt 2 14))))

(declaim (inline get-byte)
         (ftype (function (byte-ptr buffer-index buffer-index) uint8)
                get-byte))

(defun get-byte (buffer size index)
  "Get a byte from the foreign object array at the specified index."
  (declare (optimize (speed 3) (safety 0) (space 0))
           (byte-ptr buffer)
           (buffer-index size)
           (buffer-index index))
  (assert (and (<= 0 index) (< index size)))
  (the uint8 (uffi:deref-array buffer :unsigned-byte index)))

-- 
Dave Roberts
ldave-re-move@re-move.droberts.com