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