4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / strings.c
CommitLineData
761330fe 1/*
a12ff486
KB
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
0c5f72fb 4 *
f15db449 5 * %sccs.include.redist.c%
761330fe
DF
6 */
7
acfc7e9b 8#ifndef lint
a12ff486 9static char sccsid[] = "@(#)strings.c 8.1 (Berkeley) %G%";
acfc7e9b 10#endif /* not lint */
563238c6
KS
11
12/*
13 * Mail -- a mail program
14 *
15 * String allocation routines.
16 * Strings handed out here are reclaimed at the top of the command
17 * loop each time, so they need not be freed.
18 */
19
20#include "rcv.h"
a0d23834 21#include "extern.h"
563238c6 22
563238c6
KS
23/*
24 * Allocate size more bytes of space and return the address of the
25 * first byte to the caller. An even number of bytes are always
26 * allocated so that the space will always be on a word boundary.
27 * The string spaces are of exponentially increasing size, to satisfy
28 * the occasional user with enormous string size requests.
29 */
30
31char *
32salloc(size)
a0d23834 33 int size;
563238c6
KS
34{
35 register char *t;
36 register int s;
37 register struct strings *sp;
38 int index;
39
40 s = size;
021f9b0f
KB
41 s += 3;
42 s &= ~03;
563238c6
KS
43 index = 0;
44 for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
45 if (sp->s_topFree == NOSTR && (STRINGSIZE << index) >= s)
46 break;
47 if (sp->s_nleft >= s)
48 break;
49 index++;
50 }
51 if (sp >= &stringdope[NSPACE])
52 panic("String too large");
53 if (sp->s_topFree == NOSTR) {
54 index = sp - &stringdope[0];
f674e088 55 sp->s_topFree = malloc(STRINGSIZE << index);
563238c6
KS
56 if (sp->s_topFree == NOSTR) {
57 fprintf(stderr, "No room for space %d\n", index);
58 panic("Internal error");
59 }
60 sp->s_nextFree = sp->s_topFree;
61 sp->s_nleft = STRINGSIZE << index;
62 }
63 sp->s_nleft -= s;
64 t = sp->s_nextFree;
65 sp->s_nextFree += s;
66 return(t);
67}
68
69/*
70 * Reset the string area to be empty.
71 * Called to free all strings allocated
72 * since last reset.
73 */
a0d23834 74void
563238c6
KS
75sreset()
76{
77 register struct strings *sp;
78 register int index;
79
80 if (noreset)
81 return;
563238c6
KS
82 index = 0;
83 for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) {
84 if (sp->s_topFree == NOSTR)
85 continue;
86 sp->s_nextFree = sp->s_topFree;
87 sp->s_nleft = STRINGSIZE << index;
88 index++;
89 }
90}
f674e088
EW
91
92/*
93 * Make the string area permanent.
94 * Meant to be called in main, after initialization.
95 */
a0d23834 96void
f674e088
EW
97spreserve()
98{
99 register struct strings *sp;
100
101 for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++)
102 sp->s_topFree = NOSTR;
103}