wrapper.c 790 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Various trivial helper wrappers around standard functions
  3. */
  4. #include "cache.h"
  5. /*
  6. * There's no pack memory to release - but stay close to the Git
  7. * version so wrap this away:
  8. */
  9. static inline void release_pack_memory(size_t size __used, int flag __used)
  10. {
  11. }
  12. char *xstrdup(const char *str)
  13. {
  14. char *ret = strdup(str);
  15. if (!ret) {
  16. release_pack_memory(strlen(str) + 1, -1);
  17. ret = strdup(str);
  18. if (!ret)
  19. die("Out of memory, strdup failed");
  20. }
  21. return ret;
  22. }
  23. void *xrealloc(void *ptr, size_t size)
  24. {
  25. void *ret = realloc(ptr, size);
  26. if (!ret && !size)
  27. ret = realloc(ptr, 1);
  28. if (!ret) {
  29. release_pack_memory(size, -1);
  30. ret = realloc(ptr, size);
  31. if (!ret && !size)
  32. ret = realloc(ptr, 1);
  33. if (!ret)
  34. die("Out of memory, realloc failed");
  35. }
  36. return ret;
  37. }