ialloc.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ** This file is in the public domain, so clarified as of
  3. ** 2006-07-17 by Arthur David Olson.
  4. */
  5. #ifndef lint
  6. #ifndef NOID
  7. static char elsieid[] = "%W%";
  8. #endif /* !defined NOID */
  9. #endif /* !defined lint */
  10. /*LINTLIBRARY*/
  11. #include "private.h"
  12. #define nonzero(n) (((n) == 0) ? 1 : (n))
  13. char *
  14. imalloc(n)
  15. const int n;
  16. {
  17. return malloc((size_t) nonzero(n));
  18. }
  19. char *
  20. icalloc(nelem, elsize)
  21. int nelem;
  22. int elsize;
  23. {
  24. if (nelem == 0 || elsize == 0)
  25. nelem = elsize = 1;
  26. return calloc((size_t) nelem, (size_t) elsize);
  27. }
  28. void *
  29. irealloc(pointer, size)
  30. void * const pointer;
  31. const int size;
  32. {
  33. if (pointer == NULL)
  34. return imalloc(size);
  35. return realloc((void *) pointer, (size_t) nonzero(size));
  36. }
  37. char *
  38. icatalloc(old, new)
  39. char * const old;
  40. const char * const new;
  41. {
  42. register char * result;
  43. register int oldsize, newsize;
  44. newsize = (new == NULL) ? 0 : strlen(new);
  45. if (old == NULL)
  46. oldsize = 0;
  47. else if (newsize == 0)
  48. return old;
  49. else oldsize = strlen(old);
  50. if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
  51. if (new != NULL)
  52. (void) strcpy(result + oldsize, new);
  53. return result;
  54. }
  55. char *
  56. icpyalloc(string)
  57. const char * const string;
  58. {
  59. return icatalloc((char *) NULL, string);
  60. }
  61. void
  62. ifree(p)
  63. char * const p;
  64. {
  65. if (p != NULL)
  66. (void) free(p);
  67. }
  68. void
  69. icfree(p)
  70. char * const p;
  71. {
  72. if (p != NULL)
  73. (void) free(p);
  74. }