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 | */ |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 10 | static inline void release_pack_memory(size_t size __used, int flag __used) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 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 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 26 | void *xrealloc(void *ptr, size_t size) |
| 27 | { |
| 28 | void *ret = realloc(ptr, size); |
| 29 | if (!ret && !size) |
| 30 | ret = realloc(ptr, 1); |
| 31 | if (!ret) { |
| 32 | release_pack_memory(size, -1); |
| 33 | ret = realloc(ptr, size); |
| 34 | if (!ret && !size) |
| 35 | ret = realloc(ptr, 1); |
| 36 | if (!ret) |
| 37 | die("Out of memory, realloc failed"); |
| 38 | } |
| 39 | return ret; |
| 40 | } |