Fix portability bugs in valgrind support.