2 * Various trivial helper wrappers around standard functions
6 static void try_to_free_builtin(size_t size)
8 release_pack_memory(size, -1);
11 static void (*try_to_free_routine)(size_t size) = try_to_free_builtin;
13 void set_try_to_free_routine(void (*routine)(size_t))
15 try_to_free_routine = (routine) ? routine : try_to_free_builtin;
18 char *xstrdup(const char *str)
20 char *ret = strdup(str);
22 try_to_free_routine(strlen(str) + 1);
25 die("Out of memory, strdup failed");
30 void *xmalloc(size_t size)
32 void *ret = malloc(size);
36 try_to_free_routine(size);
41 die("Out of memory, malloc failed");
44 memset(ret, 0xA5, size);
49 void *xmallocz(size_t size)
53 die("Data too large to fit into virtual memory space.");
54 ret = xmalloc(size + 1);
55 ((char*)ret)[size] = 0;
60 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
61 * "data" to the allocated memory, zero terminates the allocated memory,
62 * and returns a pointer to the allocated memory. If the allocation fails,
65 void *xmemdupz(const void *data, size_t len)
67 return memcpy(xmallocz(len), data, len);
70 char *xstrndup(const char *str, size_t len)
72 char *p = memchr(str, '\0', len);
73 return xmemdupz(str, p ? p - str : len);
76 void *xrealloc(void *ptr, size_t size)
78 void *ret = realloc(ptr, size);
80 ret = realloc(ptr, 1);
82 try_to_free_routine(size);
83 ret = realloc(ptr, size);
85 ret = realloc(ptr, 1);
87 die("Out of memory, realloc failed");
92 void *xcalloc(size_t nmemb, size_t size)
94 void *ret = calloc(nmemb, size);
95 if (!ret && (!nmemb || !size))
98 try_to_free_routine(nmemb * size);
99 ret = calloc(nmemb, size);
100 if (!ret && (!nmemb || !size))
103 die("Out of memory, calloc failed");
108 void *xmmap(void *start, size_t length,
109 int prot, int flags, int fd, off_t offset)
111 void *ret = mmap(start, length, prot, flags, fd, offset);
112 if (ret == MAP_FAILED) {
115 release_pack_memory(length, fd);
116 ret = mmap(start, length, prot, flags, fd, offset);
117 if (ret == MAP_FAILED)
118 die_errno("Out of memory? mmap failed");
124 * xread() is the same a read(), but it automatically restarts read()
125 * operations with a recoverable error (EAGAIN and EINTR). xread()
126 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
128 ssize_t xread(int fd, void *buf, size_t len)
132 nr = read(fd, buf, len);
133 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
140 * xwrite() is the same a write(), but it automatically restarts write()
141 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
142 * GUARANTEE that "len" bytes is written even if the operation is successful.
144 ssize_t xwrite(int fd, const void *buf, size_t len)
148 nr = write(fd, buf, len);
149 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
155 ssize_t read_in_full(int fd, void *buf, size_t count)
161 ssize_t loaded = xread(fd, p, count);
163 return total ? total : loaded;
172 ssize_t write_in_full(int fd, const void *buf, size_t count)
178 ssize_t written = xwrite(fd, p, count);
197 die_errno("dup failed");
201 FILE *xfdopen(int fd, const char *mode)
203 FILE *stream = fdopen(fd, mode);
205 die_errno("Out of memory? fdopen failed");
209 int xmkstemp(char *template)
213 fd = mkstemp(template);
215 die_errno("Unable to create temporary file");
219 int xmkstemp_mode(char *template, int mode)
223 fd = git_mkstemp_mode(template, mode);
225 die_errno("Unable to create temporary file");
230 * zlib wrappers to make sure we don't silently miss errors
233 void git_inflate_init(z_streamp strm)
237 switch (inflateInit(strm)) {
242 err = "out of memory";
244 case Z_VERSION_ERROR:
245 err = "wrong version";
250 die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
253 void git_inflate_end(z_streamp strm)
255 if (inflateEnd(strm) != Z_OK)
256 error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
259 int git_inflate(z_streamp strm, int flush)
261 int ret = inflate(strm, flush);
265 /* Out of memory is fatal. */
267 die("inflate: out of memory");
269 /* Data corruption errors: we may want to recover from them (fsck) */
271 err = "needs dictionary"; break;
273 err = "data stream error"; break;
275 err = "stream consistency error"; break;
277 err = "unknown error"; break;
279 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
285 error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
289 int odb_mkstemp(char *template, size_t limit, const char *pattern)
293 * we let the umask do its job, don't try to be more
294 * restrictive except to remove write permission.
297 snprintf(template, limit, "%s/%s",
298 get_object_directory(), pattern);
299 fd = git_mkstemp_mode(template, mode);
304 /* some mkstemp implementations erase template on failure */
305 snprintf(template, limit, "%s/%s",
306 get_object_directory(), pattern);
307 safe_create_leading_directories(template);
308 return xmkstemp_mode(template, mode);
311 int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
315 snprintf(name, namesz, "%s/pack/pack-%s.keep",
316 get_object_directory(), sha1_to_hex(sha1));
317 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
322 safe_create_leading_directories(name);
323 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
326 int unlink_or_warn(const char *file)
328 int rc = unlink(file);
333 warning("unable to unlink %s: %s",
334 file, strerror(errno));