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
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.
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]
24 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
33 * University Copyright- Copyright (c) 1982, 1986, 1988
34 * The Regents of the University of California
37 * University Acknowledgment- Portions of this document are derived from
38 * software developed by the University of California, Berkeley, and its
42 #pragma ident "%Z%%M% %I% %E% SMI"
45 * mailx -- a modified version of a University of California at Berkeley
48 * Memory allocation routines.
49 * Memory handed out here are reclaimed at the top of the command
50 * loop each time, so they need not be freed.
56 static void *lastptr
; /* addr of last buffer allocated */
57 static struct strings
*lastsp
; /* last string space allocated from */
60 * Allocate size more bytes of space and return the address of the
61 * first byte to the caller. An even number of bytes are always
62 * allocated so that the space will always be on a word boundary.
63 * The string spaces are of exponentially increasing size, to satisfy
64 * the occasional user with enormous string size requests.
72 register struct strings
*sp
;
76 #if defined(u3b) || defined(sparc)
77 s
+= 3; /* needs alignment on quad boundary */
83 #error Unknown architecture!
86 for (sp
= &stringdope
[0]; sp
< &stringdope
[NSPACE
]; sp
++) {
87 if (sp
->s_topFree
== NOSTR
&& (STRINGSIZE
<< index
) >= s
)
93 if (sp
>= &stringdope
[NSPACE
])
94 panic("String too large");
95 if (sp
->s_topFree
== NOSTR
) {
96 index
= sp
- &stringdope
[0];
97 sp
->s_topFree
= (char *) calloc(STRINGSIZE
<< index
,
99 if (sp
->s_topFree
== NOSTR
) {
100 fprintf(stderr
, gettext("No room for space %d\n"),
102 panic("Internal error");
104 sp
->s_nextFree
= sp
->s_topFree
;
105 sp
->s_nleft
= STRINGSIZE
<< index
;
116 * Reallocate size bytes of space and return the address of the
117 * first byte to the caller. The old data is copied into the new area.
121 srealloc(void *optr
, unsigned size
)
125 /* if we just want to expand the last allocation, that's easy */
126 if (optr
== lastptr
) {
127 register unsigned s
, delta
;
128 register struct strings
*sp
= lastsp
;
131 #if defined(u3b) || defined(sparc)
132 s
+= 3; /* needs alignment on quad boundary */
138 #error Unknown architecture!
139 #endif /* defined(u3b) || defined(sparc) */
140 delta
= s
- (sp
->s_nextFree
- (char *)optr
);
141 if (delta
<= sp
->s_nleft
) {
142 sp
->s_nextFree
+= delta
;
143 sp
->s_nleft
-= delta
;
149 memcpy(nptr
, optr
, size
); /* XXX - copying too much */
154 * Reset the string area to be empty.
155 * Called to free all strings allocated
161 register struct strings
*sp
;
168 for (sp
= &stringdope
[0]; sp
< &stringdope
[NSPACE
]; sp
++) {
169 if (sp
->s_topFree
== NOSTR
)
171 sp
->s_nextFree
= sp
->s_topFree
;
172 sp
->s_nleft
= STRINGSIZE
<< index
;