move malloc_or_dir back to header
fix issues reported by tsan (some still open)