guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Immediate pointers


From: Stefan Israelsson Tampe
Subject: Re: Immediate pointers
Date: Mon, 31 Jan 2011 19:39:33 +0100
User-agent: KMail/1.13.5 (Linux/2.6.34.7-0.5-desktop; KDE/4.4.4; x86_64; ; )

Hi,

I cannot say that I'm in the tag savy bin. But out of the 
box I remember that I kind of needed this in the guile-unify
project. 

The solution I'm trying there is to look at address ranges when checking 
for the object type and use that to dispatch to a unify object or to
work with a SCM pointer. This is hacky and works well enough to be able 
to debug errors and test out concepts. 

It looks like the test for adress range is not the heavy part in 
this application so it can be tolerated there.

What I really would like though is a retake on the tagging mechanism to 
allow for some innovative modifications of the scheme engine.

/Stefan

On Sunday, January 30, 2011 11:46:01 pm Ludovic Courtès wrote:
> Hello!
> 
> While using (system foreign) in a couple of projects I found myself
> doing quite a bit of pointer arithmetic in Scheme:
> 
> --8<---------------cut here---------------start------------->8---
> (define (foreign-array->list array-pointer element-count)
>   (let ((array (pointer->bytevector array-pointer
>                                     (* element-count (sizeof '*)))))
>     (unfold (cut >= <> element-count)
>             (lambda (element)
>               (let ((start (* element (sizeof '*))))
>                 (bytevector->pointer array start)))
>             1+
>             0)))
> 
> (define (pointer+ array-pointer type index)
>   (let ((offset (* index (align (sizeof type) (alignof type)))))
>     (make-pointer (+ (pointer-address array-pointer) offset))))
> 
> (define (foreign-string-array->list array len)
>   ;; Return a list of string comprising the LEN strings pointed to by the
>   ;; elements of ARRAY, a pointer to an array of pointers.
>   (unfold (cut < <> 0)
>           (lambda (index)
>             (let ((ptr (make-pointer (+ (pointer-address array)
>                                         (* index (sizeof '*))))))
>              (pointer->string (dereference-pointer ptr))))
>           1-
>           (- len 1)))
> --8<---------------cut here---------------end--------------->8---
> 
> (Examples from
> <https://gforge.inria.fr/plugins/scmgit/cgi-bin/gitweb.cgi?p=hubble/hubble.
> git;a=blob;f=modules/simgrid.scm>.)
> 
> The problem is that each ‘make-pointer’ call (and ‘dereference-pointer’,
> etc.) conses.  This can make conversion to/from C quite inefficient.
> 
> In addition, 90% of the C pointers of interest are 8-byte aligned---that’s
> on x86_64-linux-gnu, but it surely holds on most platforms, at least for
> pointers returned by ‘malloc’.
> 
> So, here comes the idea of “immediate pointers”, which would fit in a
> word.  A 3-bit tag is used, as for immediate numbers & co; pointers that
> aren’t 8-byte aligned are still stored in an scm_tc7_pointer cell.
> 
> I experimented with it using ‘scm_tc3_unused’ (== 3).  Alas, that can’t
> work, because it makes it impossible to use such an object as the ‘car’
> of a pair:
> 
>   #define SCM_I_CONSP(x)  (!SCM_IMP (x) && ((1 & SCM_CELL_TYPE (x)) == 0))
> 
> I would welcome feedback and help from tag-savvy people.
> 
> Thanks,
> Ludo’.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]