ialloc.c revision 2703
1239278Sgonzo#ifndef lint 2239278Sgonzo#ifndef NOID 3239278Sgonzostatic char elsieid[] = "@(#)ialloc.c 8.21"; 4239278Sgonzo#endif /* !defined NOID */ 5239278Sgonzo#endif /* !defined lint */ 6239278Sgonzo 7239278Sgonzo/*LINTLIBRARY*/ 8239278Sgonzo 9239278Sgonzo#include "private.h" 10239278Sgonzo 11239278Sgonzo#ifdef MAL 12239278Sgonzo#define NULLMAL(x) ((x) == NULL || (x) == MAL) 13239278Sgonzo#endif /* defined MAL */ 14239278Sgonzo#ifndef MAL 15239278Sgonzo#define NULLMAL(x) ((x) == NULL) 16239278Sgonzo#endif /* !defined MAL */ 17239278Sgonzo 18239278Sgonzo#define nonzero(n) (((n) == 0) ? 1 : (n)) 19239278Sgonzo 20239278Sgonzochar * icalloc P((int nelem, int elsize)); 21239278Sgonzochar * icatalloc P((char * old, const char * new)); 22239278Sgonzochar * icpyalloc P((const char * string)); 23239278Sgonzochar * imalloc P((int n)); 24239278Sgonzochar * irealloc P((char * pointer, int size)); 25239278Sgonzovoid ifree P((char * pointer)); 26239278Sgonzo 27239278Sgonzochar * 28239278Sgonzoimalloc(n) 29239278Sgonzoconst int n; 30239278Sgonzo{ 31239278Sgonzo#ifdef MAL 32239278Sgonzo register char * result; 33239278Sgonzo 34239278Sgonzo result = malloc((alloc_size_t) nonzero(n)); 35239278Sgonzo return NULLMAL(result) ? NULL : result; 36239278Sgonzo#endif /* defined MAL */ 37239278Sgonzo#ifndef MAL 38239278Sgonzo return malloc((alloc_size_t) nonzero(n)); 39239278Sgonzo#endif /* !defined MAL */ 40239278Sgonzo} 41239278Sgonzo 42239278Sgonzochar * 43239278Sgonzoicalloc(nelem, elsize) 44239278Sgonzoint nelem; 45239278Sgonzoint elsize; 46239278Sgonzo{ 47239278Sgonzo if (nelem == 0 || elsize == 0) 48239278Sgonzo nelem = elsize = 1; 49239278Sgonzo return calloc((alloc_size_t) nelem, (alloc_size_t) elsize); 50239278Sgonzo} 51239278Sgonzo 52239278Sgonzochar * 53239278Sgonzoirealloc(pointer, size) 54239278Sgonzochar * const pointer; 55239278Sgonzoconst int size; 56239278Sgonzo{ 57239278Sgonzo if (NULLMAL(pointer)) 58239278Sgonzo return imalloc(size); 59239278Sgonzo return realloc((genericptr_t) pointer, (alloc_size_t) nonzero(size)); 60239278Sgonzo} 61239278Sgonzo 62239278Sgonzochar * 63239278Sgonzoicatalloc(old, new) 64239278Sgonzochar * const old; 65239278Sgonzoconst char * const new; 66239278Sgonzo{ 67239278Sgonzo register char * result; 68239278Sgonzo register int oldsize, newsize; 69239278Sgonzo 70239278Sgonzo newsize = NULLMAL(new) ? 0 : strlen(new); 71239278Sgonzo if (NULLMAL(old)) 72239278Sgonzo oldsize = 0; 73239278Sgonzo else if (newsize == 0) 74239278Sgonzo return old; 75239278Sgonzo else oldsize = strlen(old); 76239278Sgonzo if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) 77239278Sgonzo if (!NULLMAL(new)) 78239278Sgonzo (void) strcpy(result + oldsize, new); 79239278Sgonzo return result; 80239278Sgonzo} 81239278Sgonzo 82239278Sgonzochar * 83239278Sgonzoicpyalloc(string) 84239278Sgonzoconst char * const string; 85239278Sgonzo{ 86239278Sgonzo return icatalloc((char *) NULL, string); 87239278Sgonzo} 88239278Sgonzo 89239278Sgonzovoid 90239278Sgonzoifree(p) 91239278Sgonzochar * const p; 92239278Sgonzo{ 93239278Sgonzo if (!NULLMAL(p)) 94239278Sgonzo (void) free(p); 95239278Sgonzo} 96239278Sgonzo 97239278Sgonzovoid 98239278Sgonzoicfree(p) 99239278Sgonzochar * const p; 100239278Sgonzo{ 101239278Sgonzo if (!NULLMAL(p)) 102239278Sgonzo (void) free(p); 103239278Sgonzo} 104239278Sgonzo