-// Die if we can't allocate size bytes of memory.
-void *xmalloc(size_t size)
-{
- void *ptr = malloc(size);
- if (ptr == NULL && size != 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return ptr;
-}
-
-// Die if we can't resize previously allocated memory. (This returns a pointer
-// to the new memory, which may or may not be the same as the old memory.
-// It'll copy the contents to a new chunk and free the old one if necessary.)
-void *xrealloc(void *ptr, size_t size)
-{
- ptr = realloc(ptr, size);
- if (ptr == NULL && size != 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return ptr;
-}
-#endif /* DMALLOC */
-
-// Die if we can't allocate and zero size bytes of memory.
-void *xzalloc(size_t size)
-{
- void *ptr = xmalloc(size);
- memset(ptr, 0, size);
- return ptr;
-}
-
-// Die if we can't copy a string to freshly allocated memory.
-char * xstrdup(const char *s)
-{
- char *t;
-
- if (s == NULL)
- return NULL;
-
- t = strdup (s);
-
- if (t == NULL)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
-
- return t;
-}
-
-// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
-// the (possibly truncated to length n) string into it.
-char * xstrndup(const char *s, int n)
-{
- char *t;
-
- if (ENABLE_DEBUG && s == NULL)
- bb_error_msg_and_die("xstrndup bug");
-
- t = xmalloc(++n);
-
- return safe_strncpy(t,s,n);
-}
-
-// Die if we can't open a file and return a FILE * to it.
-// Notice we haven't got xfread(), This is for use with fscanf() and friends.
-FILE *xfopen(const char *path, const char *mode)
-{
- FILE *fp;
- if ((fp = fopen(path, mode)) == NULL)
- bb_perror_msg_and_die("%s", path);
- return fp;
-}