difference between cffi mem-ref/mem-aref

Discussion of Common Lisp

difference between cffi mem-ref/mem-aref

Postby sinnatagg » Fri Jun 15, 2012 12:28 am

What is the difference between these two cffi functions? The fine manual is rather terse about this and I haven't managed to make them do something different yet.


-a
sinnatagg
 
Posts: 28
Joined: Tue Apr 21, 2009 3:04 am

Re: difference between cffi mem-ref/mem-aref

Postby Ramarren » Fri Jun 15, 2012 4:29 am

The manual is quite explicit on the difference:

The mem-aref function is similar to mem-ref but will automatically calculate the offset from an index.


To elaborate, the optional argument to those functions is treated differently, in mem-ref the offset is in bytes, but in mem-aref the offset is the index multiplied by the size of referenced type.

Looking at the code this is in fact the only functional difference:

Code: Select all
(defun mem-aref (ptr type &optional (index 0))
  "Like MEM-REF except for accessing 1d arrays."
  (mem-ref ptr type (* index (foreign-type-size type))))


Although there are compiler macros which implement optimizations if TYPE and/or INDEX are constant.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: difference between cffi mem-ref/mem-aref

Postby sinnatagg » Sat Jun 16, 2012 3:34 am

Thanks, didn't think of looking at the source :oops:


-a
sinnatagg
 
Posts: 28
Joined: Tue Apr 21, 2009 3:04 am


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron