GC behavior and Allocators

Brian Schott via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 5 21:58:41 PDT 2014


On Friday, 4 July 2014 at 17:05:32 UTC, David Nadlinger wrote:
> On Friday, 4 July 2014 at 14:47:12 UTC, Chris Cain wrote:
>> Is there a way to lock the GC currently?
>
> There are critical regions in core.thread. While in such a 
> region, your thread will never be suspended, effectively also 
> precluding the GC from running. They are a rather dangerous 
> tool though, use them only if absolutely required and on small, 
> controlled pieces of code.
>
> David

So something like this would work?

void* GCAwareRealloc(void* ptr, size_t size)
{
   import core.thread;
   void* r;
   thread_enterCriticalRegion();
   scope (exit) thread_exitCriticalRegion();
   r = realloc(ptr, size);
   // Assuming that addRange performs an update of size
   GC.addRange(r, size);
   return r;
}


More information about the Digitalmars-d mailing list