Make xmalloc and xrealloc thread-safe