malloc.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. clsync - file tree sync utility based on fanotify and inotify
  3. Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "common.h"
  16. #include "malloc.h"
  17. #include "output.h"
  18. char *xmalloc(size_t size) {
  19. size++; // Just in case
  20. char *ret = (char *)malloc(size);
  21. if(ret == NULL) {
  22. printf_e("xmalloc(%i): Cannot allocate memory (#%i: %s).\n", size, errno, strerror(errno));
  23. exit(errno);
  24. }
  25. #ifdef PARANOID
  26. memset(ret, 0, size);
  27. #endif
  28. return ret;
  29. }
  30. char *xcalloc(size_t nmemb, size_t size) {
  31. nmemb++; // Just in case
  32. size++; // Just in case
  33. char *ret = (char *)calloc(nmemb, size);
  34. if(ret == NULL) {
  35. printf_e("xcalloc(%i): Cannot allocate memory (#%i: %s).\n", size, errno, strerror(errno));
  36. exit(errno);
  37. }
  38. // memset(ret, 0, nmemb*size); // Just in case
  39. return ret;
  40. }
  41. char *xrealloc(char *oldptr, size_t size) {
  42. size++; // Just in case
  43. char *ret = (char *)realloc(oldptr, size);
  44. if(ret == NULL) {
  45. printf_e("xrealloc(%p, %i): Cannot reallocate memory (#%i: %s).\n", oldptr, size, errno, strerror(errno));
  46. exit(errno);
  47. }
  48. return ret;
  49. }