You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.1 KiB

19 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. void
  10. error(char *errstr, ...) {
  11. va_list ap;
  12. va_start(ap, errstr);
  13. vfprintf(stderr, errstr, ap);
  14. va_end(ap);
  15. exit(1);
  16. }
  17. static void
  18. bad_malloc(unsigned int size)
  19. {
  20. fprintf(stderr, "fatal: could not malloc() %d bytes\n",
  21. (int) size);
  22. exit(1);
  23. }
  24. void *
  25. emallocz(unsigned int size)
  26. {
  27. void *res = calloc(1, size);
  28. if(!res)
  29. bad_malloc(size);
  30. return res;
  31. }
  32. void *
  33. emalloc(unsigned int size)
  34. {
  35. void *res = malloc(size);
  36. if(!res)
  37. bad_malloc(size);
  38. return res;
  39. }
  40. void *
  41. erealloc(void *ptr, unsigned int size)
  42. {
  43. void *res = realloc(ptr, size);
  44. if(!res)
  45. bad_malloc(size);
  46. return res;
  47. }
  48. char *
  49. estrdup(const char *str)
  50. {
  51. void *res = strdup(str);
  52. if(!res)
  53. bad_malloc(strlen(str));
  54. return res;
  55. }
  56. void
  57. failed_assert(char *a, char *file, int line)
  58. {
  59. fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
  60. abort();
  61. }
  62. void
  63. swap(void **p1, void **p2)
  64. {
  65. void *tmp = *p1;
  66. *p1 = *p2;
  67. *p2 = tmp;
  68. }