wrapper.c 817 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 __maybe_unused,
  10. int flag __maybe_unused)
  11. {
  12. }
  13. char *xstrdup(const char *str)
  14. {
  15. char *ret = strdup(str);
  16. if (!ret) {
  17. release_pack_memory(strlen(str) + 1, -1);
  18. ret = strdup(str);
  19. if (!ret)
  20. die("Out of memory, strdup failed");
  21. }
  22. return ret;
  23. }
  24. void *xrealloc(void *ptr, size_t size)
  25. {
  26. void *ret = realloc(ptr, size);
  27. if (!ret && !size)
  28. ret = realloc(ptr, 1);
  29. if (!ret) {
  30. release_pack_memory(size, -1);
  31. ret = realloc(ptr, size);
  32. if (!ret && !size)
  33. ret = realloc(ptr, 1);
  34. if (!ret)
  35. die("Out of memory, realloc failed");
  36. }
  37. return ret;
  38. }