2 * Various trivial helper wrappers around standard functions
7 * There's no pack memory to release - but stay close to the Git
8 * version so wrap this away:
10 static inline void release_pack_memory(size_t size, int flag)
14 char *xstrdup(const char *str)
16 char *ret = strdup(str);
18 release_pack_memory(strlen(str) + 1, -1);
21 die("Out of memory, strdup failed");
26 void *xmalloc(size_t size)
28 void *ret = malloc(size);
32 release_pack_memory(size, -1);
37 die("Out of memory, malloc failed");
40 memset(ret, 0xA5, size);
46 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
47 * "data" to the allocated memory, zero terminates the allocated memory,
48 * and returns a pointer to the allocated memory. If the allocation fails,
51 void *xmemdupz(const void *data, size_t len)
53 char *p = xmalloc(len + 1);
59 char *xstrndup(const char *str, size_t len)
61 char *p = memchr(str, '\0', len);
62 return xmemdupz(str, p ? p - str : len);
65 void *xrealloc(void *ptr, size_t size)
67 void *ret = realloc(ptr, size);
69 ret = realloc(ptr, 1);
71 release_pack_memory(size, -1);
72 ret = realloc(ptr, size);
74 ret = realloc(ptr, 1);
76 die("Out of memory, realloc failed");
81 void *xcalloc(size_t nmemb, size_t size)
83 void *ret = calloc(nmemb, size);
84 if (!ret && (!nmemb || !size))
87 release_pack_memory(nmemb * size, -1);
88 ret = calloc(nmemb, size);
89 if (!ret && (!nmemb || !size))
92 die("Out of memory, calloc failed");
97 void *xmmap(void *start, size_t length,
98 int prot, int flags, int fd, off_t offset)
100 void *ret = mmap(start, length, prot, flags, fd, offset);
101 if (ret == MAP_FAILED) {
104 release_pack_memory(length, fd);
105 ret = mmap(start, length, prot, flags, fd, offset);
106 if (ret == MAP_FAILED)
107 die("Out of memory? mmap failed: %s", strerror(errno));
113 * xread() is the same a read(), but it automatically restarts read()
114 * operations with a recoverable error (EAGAIN and EINTR). xread()
115 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
117 ssize_t xread(int fd, void *buf, size_t len)
121 nr = read(fd, buf, len);
122 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
129 * xwrite() is the same a write(), but it automatically restarts write()
130 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
131 * GUARANTEE that "len" bytes is written even if the operation is successful.
133 ssize_t xwrite(int fd, const void *buf, size_t len)
137 nr = write(fd, buf, len);
138 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
144 ssize_t read_in_full(int fd, void *buf, size_t count)
150 ssize_t loaded = xread(fd, p, count);
152 return total ? total : loaded;
161 ssize_t write_in_full(int fd, const void *buf, size_t count)
167 ssize_t written = xwrite(fd, p, count);
186 die("dup failed: %s", strerror(errno));
190 FILE *xfdopen(int fd, const char *mode)
192 FILE *stream = fdopen(fd, mode);
194 die("Out of memory? fdopen failed: %s", strerror(errno));
198 int xmkstemp(char *template)
202 fd = mkstemp(template);
204 die("Unable to create temporary file: %s", strerror(errno));