1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1996, by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * MKS interface extension. 31 * Ensure that errno is set if malloc() fails. 32 * 33 * Copyright 1992 by Mortice Kern Systems Inc. All rights reserved. 34 * 35 */ 36 #ifdef M_RCSID 37 #ifndef lint 38 static char rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_malloc.c 1.4 1993/12/17 15:22:04 rog Exp $"; 39 #endif /*lint*/ 40 #endif /*M_RCSID*/ 41 42 #include <mks.h> 43 #include <errno.h> 44 #include <stdlib.h> 45 46 #ifdef __STDC__ 47 #define _VOID void 48 #else 49 #define _VOID char 50 #endif 51 52 #undef m_malloc /* in case <mks.h> included in <errno.h> or <stdlib.h> */ 53 54 /*f 55 * m_malloc: 56 * Portable replacement for malloc(). 57 * If malloc() fails (e.g returns NULL) 58 * then return ENOMEM unless malloc() sets errno for us on this system 59 * and ensure malloc(0) returns a non-NULL pointer. 60 * 61 */ 62 _VOID* 63 m_malloc(amount) 64 size_t amount; 65 { 66 _VOID* ptr; 67 68 /*l 69 * Prob 1: 70 * ANSI does not insist setting errno when malloc() fails. 71 * But UNIX existing practice (which MKS relies on) always returns 72 * an errno when malloc() fails. 73 * Thus, on systems that implement malloc() where an errno is not 74 * returned, we set ENOMEM. 75 * 76 * Note: we don't care about previous value of errno since 77 * POSIX.1 (Section 2.4) says you can only look at errno 78 * after a function returns a status indicating an error. 79 * (and the function explicitly states an errno value can be 80 * returned - Well, m_malloc() is so stated.) 81 * 82 * Prob 2: 83 * MKS code seems to rely on malloc(0) returning a valid pointer. 84 * This allows it to realloc() later when actual size is determined. 85 * 86 * According to ANSI (4.10.3 line 18-19) the result of malloc(0) is 87 * implementation-defined. 88 */ 89 90 errno = 0; 91 if ((ptr = malloc(amount)) == NULL) { 92 if (amount == 0) { 93 /* 94 * confirm we are really out of memory 95 */ 96 return (m_malloc(1)); 97 } 98 if (errno==0) { 99 /* 100 * ensure errno is always set 101 */ 102 errno = ENOMEM; 103 } 104 } 105 return (ptr); 106 } 107