Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Various trivial helper wrappers around standard functions |
| 3 | */ |
| 4 | #include "cache.h" |
| 5 | |
| 6 | /* |
| 7 | * There's no pack memory to release - but stay close to the Git |
| 8 | * version so wrap this away: |
| 9 | */ |
| 10 | static inline void release_pack_memory(size_t size, int flag) |
| 11 | { |
| 12 | } |
| 13 | |
| 14 | char *xstrdup(const char *str) |
| 15 | { |
| 16 | char *ret = strdup(str); |
| 17 | if (!ret) { |
| 18 | release_pack_memory(strlen(str) + 1, -1); |
| 19 | ret = strdup(str); |
| 20 | if (!ret) |
| 21 | die("Out of memory, strdup failed"); |
| 22 | } |
| 23 | return ret; |
| 24 | } |
| 25 | |
| 26 | void *xmalloc(size_t size) |
| 27 | { |
| 28 | void *ret = malloc(size); |
| 29 | if (!ret && !size) |
| 30 | ret = malloc(1); |
| 31 | if (!ret) { |
| 32 | release_pack_memory(size, -1); |
| 33 | ret = malloc(size); |
| 34 | if (!ret && !size) |
| 35 | ret = malloc(1); |
| 36 | if (!ret) |
| 37 | die("Out of memory, malloc failed"); |
| 38 | } |
| 39 | #ifdef XMALLOC_POISON |
| 40 | memset(ret, 0xA5, size); |
| 41 | #endif |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | /* |
| 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, |
| 49 | * the program dies. |
| 50 | */ |
| 51 | void *xmemdupz(const void *data, size_t len) |
| 52 | { |
| 53 | char *p = xmalloc(len + 1); |
| 54 | memcpy(p, data, len); |
| 55 | p[len] = '\0'; |
| 56 | return p; |
| 57 | } |
| 58 | |
| 59 | char *xstrndup(const char *str, size_t len) |
| 60 | { |
| 61 | char *p = memchr(str, '\0', len); |
| 62 | return xmemdupz(str, p ? p - str : len); |
| 63 | } |
| 64 | |
| 65 | void *xrealloc(void *ptr, size_t size) |
| 66 | { |
| 67 | void *ret = realloc(ptr, size); |
| 68 | if (!ret && !size) |
| 69 | ret = realloc(ptr, 1); |
| 70 | if (!ret) { |
| 71 | release_pack_memory(size, -1); |
| 72 | ret = realloc(ptr, size); |
| 73 | if (!ret && !size) |
| 74 | ret = realloc(ptr, 1); |
| 75 | if (!ret) |
| 76 | die("Out of memory, realloc failed"); |
| 77 | } |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | void *xcalloc(size_t nmemb, size_t size) |
| 82 | { |
| 83 | void *ret = calloc(nmemb, size); |
| 84 | if (!ret && (!nmemb || !size)) |
| 85 | ret = calloc(1, 1); |
| 86 | if (!ret) { |
| 87 | release_pack_memory(nmemb * size, -1); |
| 88 | ret = calloc(nmemb, size); |
| 89 | if (!ret && (!nmemb || !size)) |
| 90 | ret = calloc(1, 1); |
| 91 | if (!ret) |
| 92 | die("Out of memory, calloc failed"); |
| 93 | } |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | void *xmmap(void *start, size_t length, |
| 98 | int prot, int flags, int fd, off_t offset) |
| 99 | { |
| 100 | void *ret = mmap(start, length, prot, flags, fd, offset); |
| 101 | if (ret == MAP_FAILED) { |
| 102 | if (!length) |
| 103 | return NULL; |
| 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)); |
| 108 | } |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | /* |
| 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. |
| 116 | */ |
| 117 | ssize_t xread(int fd, void *buf, size_t len) |
| 118 | { |
| 119 | ssize_t nr; |
| 120 | while (1) { |
| 121 | nr = read(fd, buf, len); |
| 122 | if ((nr < 0) && (errno == EAGAIN || errno == EINTR)) |
| 123 | continue; |
| 124 | return nr; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 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. |
| 132 | */ |
| 133 | ssize_t xwrite(int fd, const void *buf, size_t len) |
| 134 | { |
| 135 | ssize_t nr; |
| 136 | while (1) { |
| 137 | nr = write(fd, buf, len); |
| 138 | if ((nr < 0) && (errno == EAGAIN || errno == EINTR)) |
| 139 | continue; |
| 140 | return nr; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | ssize_t read_in_full(int fd, void *buf, size_t count) |
| 145 | { |
| 146 | char *p = buf; |
| 147 | ssize_t total = 0; |
| 148 | |
| 149 | while (count > 0) { |
| 150 | ssize_t loaded = xread(fd, p, count); |
| 151 | if (loaded <= 0) |
| 152 | return total ? total : loaded; |
| 153 | count -= loaded; |
| 154 | p += loaded; |
| 155 | total += loaded; |
| 156 | } |
| 157 | |
| 158 | return total; |
| 159 | } |
| 160 | |
| 161 | ssize_t write_in_full(int fd, const void *buf, size_t count) |
| 162 | { |
| 163 | const char *p = buf; |
| 164 | ssize_t total = 0; |
| 165 | |
| 166 | while (count > 0) { |
| 167 | ssize_t written = xwrite(fd, p, count); |
| 168 | if (written < 0) |
| 169 | return -1; |
| 170 | if (!written) { |
| 171 | errno = ENOSPC; |
| 172 | return -1; |
| 173 | } |
| 174 | count -= written; |
| 175 | p += written; |
| 176 | total += written; |
| 177 | } |
| 178 | |
| 179 | return total; |
| 180 | } |
| 181 | |
| 182 | int xdup(int fd) |
| 183 | { |
| 184 | int ret = dup(fd); |
| 185 | if (ret < 0) |
| 186 | die("dup failed: %s", strerror(errno)); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | FILE *xfdopen(int fd, const char *mode) |
| 191 | { |
| 192 | FILE *stream = fdopen(fd, mode); |
| 193 | if (stream == NULL) |
| 194 | die("Out of memory? fdopen failed: %s", strerror(errno)); |
| 195 | return stream; |
| 196 | } |
| 197 | |
| 198 | int xmkstemp(char *template) |
| 199 | { |
| 200 | int fd; |
| 201 | |
| 202 | fd = mkstemp(template); |
| 203 | if (fd < 0) |
| 204 | die("Unable to create temporary file: %s", strerror(errno)); |
| 205 | return fd; |
| 206 | } |