home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / utils / ialloc.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  2KB  |  89 lines

  1. #ifndef lint
  2. #ifndef NOID
  3. static char    elsieid[] = "@(#)ialloc.c    8.28";
  4. #endif /* !defined NOID */
  5. #endif /* !defined lint */
  6.  
  7. /*LINTLIBRARY*/
  8.  
  9. #include "private.h"
  10.  
  11. #define nonzero(n)    (((n) == 0) ? 1 : (n))
  12.  
  13. char *    icalloc P((int nelem, int elsize));
  14. char *    icatalloc P((char * old, const char * new));
  15. char *    icpyalloc P((const char * string));
  16. char *    imalloc P((int n));
  17. void *    irealloc P((void * pointer, int size));
  18. void    ifree P((char * pointer));
  19.  
  20. char *
  21. imalloc(n)
  22. const int    n;
  23. {
  24.     return malloc((size_t) nonzero(n));
  25. }
  26.  
  27. char *
  28. icalloc(nelem, elsize)
  29. int    nelem;
  30. int    elsize;
  31. {
  32.     if (nelem == 0 || elsize == 0)
  33.         nelem = elsize = 1;
  34.     return calloc((size_t) nelem, (size_t) elsize);
  35. }
  36.  
  37. void *
  38. irealloc(pointer, size)
  39. void * const    pointer;
  40. const int    size;
  41. {
  42.     if (pointer == NULL)
  43.         return imalloc(size);
  44.     return realloc((void *) pointer, (size_t) nonzero(size));
  45. }
  46.  
  47. char *
  48. icatalloc(old, new)
  49. char * const        old;
  50. const char * const    new;
  51. {
  52.     register char *    result;
  53.     register int    oldsize, newsize;
  54.  
  55.     newsize = (new == NULL) ? 0 : strlen(new);
  56.     if (old == NULL)
  57.         oldsize = 0;
  58.     else if (newsize == 0)
  59.         return old;
  60.     else    oldsize = strlen(old);
  61.     if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
  62.         if (new != NULL)
  63.             (void) strcpy(result + oldsize, new);
  64.     return result;
  65. }
  66.  
  67. char *
  68. icpyalloc(string)
  69. const char * const    string;
  70. {
  71.     return icatalloc((char *) NULL, string);
  72. }
  73.  
  74. void
  75. ifree(p)
  76. char * const    p;
  77. {
  78.     if (p != NULL)
  79.         (void) free(p);
  80. }
  81.  
  82. void
  83. icfree(p)
  84. char * const    p;
  85. {
  86.     if (p != NULL)
  87.         (void) free(p);
  88. }
  89.