1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 28*0Sstevel@tonic-gate * The Regents of the University of California 29*0Sstevel@tonic-gate * All Rights Reserved 30*0Sstevel@tonic-gate * 31*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 32*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 33*0Sstevel@tonic-gate * contributors. 34*0Sstevel@tonic-gate */ 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate /* 39*0Sstevel@tonic-gate * Copyright (c) 1993-1997, by Sun Microsystems, Inc. 40*0Sstevel@tonic-gate * All rights reserved. 41*0Sstevel@tonic-gate */ 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate /* 44*0Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley 45*0Sstevel@tonic-gate * mail program 46*0Sstevel@tonic-gate * 47*0Sstevel@tonic-gate * Memory allocation routines. 48*0Sstevel@tonic-gate * Memory handed out here are reclaimed at the top of the command 49*0Sstevel@tonic-gate * loop each time, so they need not be freed. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #include "rcv.h" 53*0Sstevel@tonic-gate #include <locale.h> 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate static void *lastptr; /* addr of last buffer allocated */ 56*0Sstevel@tonic-gate static struct strings *lastsp; /* last string space allocated from */ 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate /* 59*0Sstevel@tonic-gate * Allocate size more bytes of space and return the address of the 60*0Sstevel@tonic-gate * first byte to the caller. An even number of bytes are always 61*0Sstevel@tonic-gate * allocated so that the space will always be on a word boundary. 62*0Sstevel@tonic-gate * The string spaces are of exponentially increasing size, to satisfy 63*0Sstevel@tonic-gate * the occasional user with enormous string size requests. 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate void * 67*0Sstevel@tonic-gate salloc(unsigned size) 68*0Sstevel@tonic-gate { 69*0Sstevel@tonic-gate register char *t; 70*0Sstevel@tonic-gate register unsigned s; 71*0Sstevel@tonic-gate register struct strings *sp; 72*0Sstevel@tonic-gate int index; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate s = size; 75*0Sstevel@tonic-gate #if defined(u3b) || defined(sparc) 76*0Sstevel@tonic-gate s += 3; /* needs alignment on quad boundary */ 77*0Sstevel@tonic-gate s &= ~03; 78*0Sstevel@tonic-gate #elif defined(i386) 79*0Sstevel@tonic-gate s++; 80*0Sstevel@tonic-gate s &= ~01; 81*0Sstevel@tonic-gate #else 82*0Sstevel@tonic-gate #error Unknown architecture! 83*0Sstevel@tonic-gate #endif 84*0Sstevel@tonic-gate index = 0; 85*0Sstevel@tonic-gate for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { 86*0Sstevel@tonic-gate if (sp->s_topFree == NOSTR && (STRINGSIZE << index) >= s) 87*0Sstevel@tonic-gate break; 88*0Sstevel@tonic-gate if (sp->s_nleft >= s) 89*0Sstevel@tonic-gate break; 90*0Sstevel@tonic-gate index++; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate if (sp >= &stringdope[NSPACE]) 93*0Sstevel@tonic-gate panic("String too large"); 94*0Sstevel@tonic-gate if (sp->s_topFree == NOSTR) { 95*0Sstevel@tonic-gate index = sp - &stringdope[0]; 96*0Sstevel@tonic-gate sp->s_topFree = (char *) calloc(STRINGSIZE << index, 97*0Sstevel@tonic-gate (unsigned) 1); 98*0Sstevel@tonic-gate if (sp->s_topFree == NOSTR) { 99*0Sstevel@tonic-gate fprintf(stderr, gettext("No room for space %d\n"), 100*0Sstevel@tonic-gate index); 101*0Sstevel@tonic-gate panic("Internal error"); 102*0Sstevel@tonic-gate } 103*0Sstevel@tonic-gate sp->s_nextFree = sp->s_topFree; 104*0Sstevel@tonic-gate sp->s_nleft = STRINGSIZE << index; 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate sp->s_nleft -= s; 107*0Sstevel@tonic-gate t = sp->s_nextFree; 108*0Sstevel@tonic-gate sp->s_nextFree += s; 109*0Sstevel@tonic-gate lastptr = t; 110*0Sstevel@tonic-gate lastsp = sp; 111*0Sstevel@tonic-gate return(t); 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate /* 115*0Sstevel@tonic-gate * Reallocate size bytes of space and return the address of the 116*0Sstevel@tonic-gate * first byte to the caller. The old data is copied into the new area. 117*0Sstevel@tonic-gate */ 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate void * 120*0Sstevel@tonic-gate srealloc(void *optr, unsigned size) 121*0Sstevel@tonic-gate { 122*0Sstevel@tonic-gate void *nptr; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate /* if we just want to expand the last allocation, that's easy */ 125*0Sstevel@tonic-gate if (optr == lastptr) { 126*0Sstevel@tonic-gate register unsigned s, delta; 127*0Sstevel@tonic-gate register struct strings *sp = lastsp; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate s = size; 130*0Sstevel@tonic-gate #if defined(u3b) || defined(sparc) 131*0Sstevel@tonic-gate s += 3; /* needs alignment on quad boundary */ 132*0Sstevel@tonic-gate s &= ~03; 133*0Sstevel@tonic-gate #elif defined(i386) 134*0Sstevel@tonic-gate s++; 135*0Sstevel@tonic-gate s &= ~01; 136*0Sstevel@tonic-gate #else 137*0Sstevel@tonic-gate #error Unknown architecture! 138*0Sstevel@tonic-gate #endif defined(u3b) || defined(sparc) 139*0Sstevel@tonic-gate delta = s - (sp->s_nextFree - (char *)optr); 140*0Sstevel@tonic-gate if (delta <= sp->s_nleft) { 141*0Sstevel@tonic-gate sp->s_nextFree += delta; 142*0Sstevel@tonic-gate sp->s_nleft -= delta; 143*0Sstevel@tonic-gate return (optr); 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate nptr = salloc(size); 147*0Sstevel@tonic-gate if (nptr) 148*0Sstevel@tonic-gate memcpy(nptr, optr, size); /* XXX - copying too much */ 149*0Sstevel@tonic-gate return nptr; 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* 153*0Sstevel@tonic-gate * Reset the string area to be empty. 154*0Sstevel@tonic-gate * Called to free all strings allocated 155*0Sstevel@tonic-gate * since last reset. 156*0Sstevel@tonic-gate */ 157*0Sstevel@tonic-gate void 158*0Sstevel@tonic-gate sreset(void) 159*0Sstevel@tonic-gate { 160*0Sstevel@tonic-gate register struct strings *sp; 161*0Sstevel@tonic-gate register int index; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate if (noreset) 164*0Sstevel@tonic-gate return; 165*0Sstevel@tonic-gate minit(); 166*0Sstevel@tonic-gate index = 0; 167*0Sstevel@tonic-gate for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { 168*0Sstevel@tonic-gate if (sp->s_topFree == NOSTR) 169*0Sstevel@tonic-gate continue; 170*0Sstevel@tonic-gate sp->s_nextFree = sp->s_topFree; 171*0Sstevel@tonic-gate sp->s_nleft = STRINGSIZE << index; 172*0Sstevel@tonic-gate index++; 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate lastptr = NULL; 175*0Sstevel@tonic-gate lastsp = NULL; 176*0Sstevel@tonic-gate } 177