safeMalloc implementation