This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: I suspect a serious bug: it is in the GC



>>>>> "Greg" == Greg Harvey <Greg.Harvey@thezone.net> writes:

    [...]

    Greg> Ok, here's what's happening. First, gh_doubles2scm is
    Greg> allocating a big vector. No problem here, but this vector is
    Greg> not a scheme object, it's just a c vector as far as guile's
    Greg> concerned, so each of the newly created doubles being put
    Greg> into it are not reachable by any means until the end of the
    Greg> function, when makvect (which just constructs a real vector
    Greg> from it) is called with the contents, so if a garbage
    Greg> collection is caused by scm_makdbl all of the previously
    Greg> allocated doubles will be collected. I've attached a patch.

diff -c -c -r1.20 gh_data.c
*** gh_data.c	1999/08/30 07:02:25	1.20
--- gh_data.c	1999/10/05 21:40:51
***************
*** 141,165 ****
  SCM
  gh_ints2scm (int *d, int n)
  {
!   SCM *m;
    int i;
!   m = (SCM*) scm_must_malloc (n * sizeof (SCM), "vector");
    for (i = 0; i < n; ++i)
!     m[i] = (d[i] >= SCM_MOST_NEGATIVE_FIXNUM
! 	    && d[i] <= SCM_MOST_POSITIVE_FIXNUM
! 	    ? SCM_MAKINUM (d[i])
! 	    : scm_long2big (d[i]));
!   return makvect ((char *) m, n, scm_tc7_vector);
  }
  
  SCM
  gh_doubles2scm (double *d, int n)
  {
!   SCM *m = (SCM*) scm_must_malloc (n * sizeof (SCM), "vector");
!   int i;
!   for (i = 0; i < n; ++i)
!     m[i] = scm_makdbl (d[i], 0.0);
!   return makvect ((char *) m, n, scm_tc7_vector);
  }
  
  SCM
--- 141,163 ----
  SCM
  gh_ints2scm (int *d, int n)
  {
!   SCM v;
    int i;
!   v = scm_make_vector(SCM_MAKINUM(n), SCM_UNSPECIFIED);
    for (i = 0; i < n; ++i)
!     SCM_VELTS(v)[i] = (d[i] >= SCM_MOST_NEGATIVE_FIXNUM
!                        && d[i] <= SCM_MOST_POSITIVE_FIXNUM
!                        ? SCM_MAKINUM (d[i])
!                        : scm_long2big (d[i]));
!   return v;
  }
  
  SCM
  gh_doubles2scm (double *d, int n)
  {
!   char *m = scm_must_malloc(n * sizeof (double), "vector");
!   memcpy(m, d, n*sizeof(double));
!   return makvect(m, n, scm_tc7_dvect);
  }
  
  SCM

Ok, it seems the right way for gh_ints2scm, but your
code for gh_doubles2scm duplicates the one in gh_doubles2dvect.
And this is inappropriate as you are then forced to use
uniform-vector-ref instead of vector-ref.

So here a version of gh_doubles2scm:

SCM
gh_doubles2scm (double *d, int n)
{
  int i;
  SCM v = scm_make_vector(SCM_MAKINUM(n), SCM_UNSPECIFIED);
  for (i = 0; i < n; ++i)
    SCM_VELTS(v)[i] = scm_makdbl (d[i], 0.0);
  return v;
}

Regards.

-- 

B. Urban


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]