alloc.c (1788B)
1 /* 2 * grep - search a file for a pattern 3 * 4 * Gunnar Ritter, Freiburg i. Br., Germany, April 2001. 5 */ 6 /* 7 * Copyright (c) 2003 Gunnar Ritter 8 * 9 * This software is provided 'as-is', without any express or implied 10 * warranty. In no event will the authors be held liable for any damages 11 * arising from the use of this software. 12 * 13 * Permission is granted to anyone to use this software for any purpose, 14 * including commercial applications, and to alter it and redistribute 15 * it freely, subject to the following restrictions: 16 * 17 * 1. The origin of this software must not be misrepresented; you must not 18 * claim that you wrote the original software. If you use this software 19 * in a product, an acknowledgment in the product documentation would be 20 * appreciated but is not required. 21 * 22 * 2. Altered source versions must be plainly marked as such, and must not be 23 * misrepresented as being the original software. 24 * 25 * 3. This notice may not be removed or altered from any source distribution. 26 */ 27 28 /* Sccsid @(#)alloc.c 1.3 (gritter) 4/17/03> */ 29 30 /* 31 * Memory allocation routines. 32 */ 33 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include "alloc.h" 37 38 /* 39 * Memory allocation with check. 40 */ 41 void * 42 smalloc(size_t nbytes) 43 { 44 void *p; 45 46 if ((p = (void *)malloc(nbytes)) == NULL) { 47 write(2, "Out of memory\n", 14); 48 exit(077); 49 } 50 return p; 51 } 52 53 /* 54 * Memory reallocation with check. 55 */ 56 void * 57 srealloc(void *ptr, size_t nbytes) 58 { 59 void *cp; 60 61 if ((cp = (void *)realloc(ptr, nbytes)) == NULL) { 62 write(2, "Out of memory\n", 14); 63 exit(077); 64 } 65 return cp; 66 } 67 68 /* 69 * Zero-filled allocation with check. 70 */ 71 void * 72 scalloc(size_t nelem, size_t elsize) 73 { 74 void *cp; 75 76 if ((cp = calloc(nelem, elsize)) == NULL) { 77 write(2, "Out of memory\n", 14); 78 exit(077); 79 } 80 return cp; 81 }