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

Re: Src question



Quoting Robert John Andersen:
> I was poking around the vector code and noticed a reference to
> zero_length_vector which gets created in scheme_init_vector.  I was
> wondering what the following allocation line was supposed to accomplish
> since the size seems to say allocate 0 bytes:
> 
> zero_length_vector =  (Scheme_Object
> *)scheme_malloc_tagged(sizeof(Scheme_Vector) - sizeof(Scheme_Object *));

I think you're overlooking the "*" in "sizeof(Scheme_Object *)".


For those who want a longer explanation:

MzScheme "inlines" references of the elements of a vector in the base
record (which contains the type tag and vector size), instead of
allocating a separate array of references.

In particular, Scheme_Vector is defined as

 typedef struct Scheme_Vector {
   Scheme_Type type;
   int size;
   Scheme_Object *els[1];
 } Scheme_Vector;

The `els' vector isn't really of length 1. Each Scheme_Vector record is
allocated so that `els' is as long as the number of items in the
vector. But we have to put some positive number for `els' in the
definition of Scheme_Vector.

The formula for the actual size (in bytes) of the record for a vector
of N elements is

  sizeof(Scheme_Vector) + ((N - 1) * sizeof(Scheme_Object*))

In the case that N=0, it's the expression you see for
zero_length_vector.

Matthew