chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Evicted objects


From: Tony Sidaway
Subject: Re: [Chicken-users] Evicted objects
Date: Tue, 6 Feb 2007 14:42:07 +0000

On 2/6/07, felix winkelmann <address@hidden> wrote:
On 2/6/07, Tony Sidaway <address@hidden> wrote:
>
> My expectations are obviously wrong.
>
> What's happening there?

Since Scheme strings do not have a zero-terminator, the call to getmem
will receive a copy of the string, with a 0-terminator appended.

Thanks, it's all blindingly obvious when you put it like that!  So
c-string arguments are never safe for use with a function that will
store the pointer value for later use.

To pass
the String object directly, use the foreign type specifier `scheme-object'
(and access the data-portion of the block via C_c_string(x), for example).


Thanks.  It's very obvious what needs to be done when I understand how
the c-string foreign argument specifier works (which is not at all how
I thought it did).

I'm happy to say that my code now works.  I do something like this:

#>
static C_char *new_c_string (C_word s) {
C_char *s1;
C_char *p;
C_word len;
   /* Return NULL if we're given something stupid */
   if ((s & C_IMMEDIATE_MARK_BITS) ||
        (C_header_bits(s) != C_STRING_TYPE)) return NULL;
   s1 = C_c_string(s);
   len = C_header_size(s);
   p = (C_char *)C_malloc(len+1);
   /* Don't try to populate if the allocation failed */
   if (NULL != p) {
       C_memcpy (p, s1, len);
        p[len] = '\0';
   }
   return p;
}

<#
(define new-c-string (foreign-lambda c-pointer "new_c_string" scheme-object))

The new procedure (new-c-string OBJ) here creates an unmanaged C
string from a Scheme string.

And for memory management purposes I can put this new string object
into the list of c-strings for the enclosing object c (which happens
to be a tinyclos class instance) so the object's finalizer can return
them all to the C heap by this:

(use lolevel)
...
(map free (slot-ref c '%c-strings))




reply via email to

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