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 | */ |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 10 | static inline void release_pack_memory(size_t size __maybe_unused, |
| 11 | int flag __maybe_unused) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 12 | { |
| 13 | } |
| 14 | |
| 15 | char *xstrdup(const char *str) |
| 16 | { |
| 17 | char *ret = strdup(str); |
| 18 | if (!ret) { |
| 19 | release_pack_memory(strlen(str) + 1, -1); |
| 20 | ret = strdup(str); |
| 21 | if (!ret) |
| 22 | die("Out of memory, strdup failed"); |
| 23 | } |
| 24 | return ret; |
| 25 | } |
| 26 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 27 | void *xrealloc(void *ptr, size_t size) |
| 28 | { |
| 29 | void *ret = realloc(ptr, size); |
| 30 | if (!ret && !size) |
| 31 | ret = realloc(ptr, 1); |
| 32 | if (!ret) { |
| 33 | release_pack_memory(size, -1); |
| 34 | ret = realloc(ptr, size); |
| 35 | if (!ret && !size) |
| 36 | ret = realloc(ptr, 1); |
| 37 | if (!ret) |
| 38 | die("Out of memory, realloc failed"); |
| 39 | } |
| 40 | return ret; |
| 41 | } |