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 __used, int flag __used)
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);
63
return xmemdupz(str, p ? (size_t)(p - str) : len);
66
void *xrealloc(void *ptr, size_t size)
68
void *ret = realloc(ptr, size);
70
ret = realloc(ptr, 1);
72
release_pack_memory(size, -1);
73
ret = realloc(ptr, size);
75
ret = realloc(ptr, 1);
77
die("Out of memory, realloc failed");
82
void *xcalloc(size_t nmemb, size_t size)
84
void *ret = calloc(nmemb, size);
85
if (!ret && (!nmemb || !size))
88
release_pack_memory(nmemb * size, -1);
89
ret = calloc(nmemb, size);
90
if (!ret && (!nmemb || !size))
93
die("Out of memory, calloc failed");
98
void *xmmap(void *start, size_t length,
99
int prot, int flags, int fd, off_t offset)
101
void *ret = mmap(start, length, prot, flags, fd, offset);
102
if (ret == MAP_FAILED) {
105
release_pack_memory(length, fd);
106
ret = mmap(start, length, prot, flags, fd, offset);
107
if (ret == MAP_FAILED)
108
die("Out of memory? mmap failed: %s", strerror(errno));
114
* xread() is the same a read(), but it automatically restarts read()
115
* operations with a recoverable error (EAGAIN and EINTR). xread()
116
* DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
118
ssize_t xread(int fd, void *buf, size_t len)
122
nr = read(fd, buf, len);
123
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
130
* xwrite() is the same a write(), but it automatically restarts write()
131
* operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
132
* GUARANTEE that "len" bytes is written even if the operation is successful.
134
ssize_t xwrite(int fd, const void *buf, size_t len)
138
nr = write(fd, buf, len);
139
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
145
ssize_t read_in_full(int fd, void *buf, size_t count)
151
ssize_t loaded = xread(fd, p, count);
153
return total ? total : loaded;
162
ssize_t write_in_full(int fd, const void *buf, size_t count)
168
ssize_t written = xwrite(fd, p, count);
187
die("dup failed: %s", strerror(errno));
191
FILE *xfdopen(int fd, const char *mode)
193
FILE *stream = fdopen(fd, mode);
195
die("Out of memory? fdopen failed: %s", strerror(errno));
199
int xmkstemp(char *template)
203
fd = mkstemp(template);
205
die("Unable to create temporary file: %s", strerror(errno));