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