Merge pull request #186 from gedaiu/patch-1

Add a way to enforce that the GC is not running
This commit is contained in:
Sönke Ludwig 2019-12-17 10:34:45 +01:00 committed by GitHub
commit 8792e315a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,3 +39,25 @@ void disposeGCSafe(T, Allocator)(Allocator allocator, T obj)
GC.removeRange(cast(void*)obj);
allocator.dispose(obj);
}
void ensureNotInGC(T)(string info = null) nothrow
{
import core.exception : InvalidMemoryOperationError;
try
{
import core.memory : GC;
cast(void) GC.malloc(1);
return;
}
catch(InvalidMemoryOperationError e)
{
import core.stdc.stdio : fprintf, stderr;
import core.stdc.stdlib : exit;
fprintf(stderr,
"Error: clean-up of %s incorrectly depends on destructors called by the GC.\n",
T.stringof.ptr);
if (info)
fprintf(stderr, "Info: %s\n", info.ptr);
assert(false);
}
}