How to force gcc to blackhole registers so that things maybe garbage collected?
Xi Ruoyao
xry111@mengyan1223.wang
Mon May 13 17:17:00 GMT 2019
On 2019-05-13 16:58 +0000, Hamad Ahmed wrote:
> I am writing a C program and using the Boehm-GC for garbage collection. The GC
> collects anything whose reference doesn't exist anymore. The problem is that
> gcc will sometimes leave pointers to memory in the registers even though those
> variables won't be used anymore in the program. This is a highly variable
> behavior depending on register pressure and program size. But this messes with
> the GC if a register holds a pointer to memory then that memory never gets
> freed. We have tried putting x=NULL to force the register to be emptied but
> gcc just ignores this because x is not used anymore in the program. Is there a
> way that gcc blackholes registers if the variable they contain won't be used
> in the program anymore? Without this functionality, using a garbage collector
> is a nightmare.
*(void* volatile*)&p = NULL;
Though this looks stupid but it works:
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <gc.h>
int main()
{
char *p = (char *)GC_malloc(256);
scanf("%255s", p);
printf("%s", p);
p = NULL;
*(void* volatile*)&p = NULL;
return 0;
}
$ gcc test.c -S -O3 -fverbose-asm -o - | grep volatile
# -fstrict-volatile-bitfields -fsync-libcalls -fthread-jumps
# test.c:11: *(void* volatile*)&p = NULL;
movq $0, 8(%rsp) #, MEM[(void * volatile *)&p]
Oh, but if we know where we can put a "p = NULL" or the stupid thing I wrote,
why not use a smart pointer based on refcount instead of GC?
--
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University
More information about the Gcc-help
mailing list