Re[2]: [Gc] Determine the requested size of a memory block

Ivan Maidanski ivmai at mail.ru
Thu Apr 22 02:47:29 PDT 2010


Thu, 22 Apr 2010 11:05:38 +0200 Christian Gudrian <christian at gudrian.org>:

> Am 22.04.2010 09:43, schrieb Ivan Maidanski:
> 
> > In theory, it's possible to adjust hb_sz (returned by GC_size) to
> > have a byte granularity (with some negligible performance loss).
> 
> I certainly do not want to fumble around in the innards of the collector.
> Instead I now provide a custom implementation of the array new operator:
> 
> // -----------------------------------------------------------------
> inline void * operator new[] (size_t size,
>                               GCPlacement gcp,
>                               GCCleanUpFunc cleanup = 0,
>                               void * clientData = 0)
> {
>    size_t * ptr = (size_t *) ::operator new (size + sizeof (size_t), 
>                                              gcp, cleanup, clientData);
>    *ptr++ = size;

So, all array pointers are displaced (and all_interior_pointer should be on, and to call some GC functions (like GC_register_finalizer) you have to call GC_base) - not a good idea, IMHO.

>    return ptr;
> }
> // -----------------------------------------------------------------
> 
> The length() function looks like this:
> 
> // -----------------------------------------------------------------
> template<class T>
> size_t length (T * array)
> {
>    size_t * ptr = (size_t *) GC_base ( (void *) array);

This won't work correctly in case of GC_DEBUG.

>    assert(ptr);
>    return *ptr / sizeof (T);
> }
> // -----------------------------------------------------------------
> 
> Now it's possible to use arrays this way:
> 
> char * c = new (PointerFreeGC) char[123];
> for (int i = 0; i < length(c); i++)

I think, length() is quite convenient.

Hmm... I'd prefer GC_size() to have byte granularity (and exclude GC debug header) to be usable in this case.

Hans -
what do you think of this topic?

> {
>    char value = c[i];
>    // ...
> }
> 
> Christian



More information about the Gc mailing list