Commit | Line | Data |
---|---|---|
761330fe DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
0c5f72fb KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
acfc7e9b KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
761330fe DF |
16 | */ |
17 | ||
acfc7e9b | 18 | #ifndef lint |
f674e088 | 19 | static char sccsid[] = "@(#)strings.c 5.8 (Berkeley) %G%"; |
acfc7e9b | 20 | #endif /* not lint */ |
563238c6 KS |
21 | |
22 | /* | |
23 | * Mail -- a mail program | |
24 | * | |
25 | * String allocation routines. | |
26 | * Strings handed out here are reclaimed at the top of the command | |
27 | * loop each time, so they need not be freed. | |
28 | */ | |
29 | ||
30 | #include "rcv.h" | |
31 | ||
563238c6 KS |
32 | /* |
33 | * Allocate size more bytes of space and return the address of the | |
34 | * first byte to the caller. An even number of bytes are always | |
35 | * allocated so that the space will always be on a word boundary. | |
36 | * The string spaces are of exponentially increasing size, to satisfy | |
37 | * the occasional user with enormous string size requests. | |
38 | */ | |
39 | ||
40 | char * | |
41 | salloc(size) | |
42 | { | |
43 | register char *t; | |
44 | register int s; | |
45 | register struct strings *sp; | |
46 | int index; | |
47 | ||
48 | s = size; | |
021f9b0f KB |
49 | s += 3; |
50 | s &= ~03; | |
563238c6 KS |
51 | index = 0; |
52 | for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { | |
53 | if (sp->s_topFree == NOSTR && (STRINGSIZE << index) >= s) | |
54 | break; | |
55 | if (sp->s_nleft >= s) | |
56 | break; | |
57 | index++; | |
58 | } | |
59 | if (sp >= &stringdope[NSPACE]) | |
60 | panic("String too large"); | |
61 | if (sp->s_topFree == NOSTR) { | |
62 | index = sp - &stringdope[0]; | |
f674e088 | 63 | sp->s_topFree = malloc(STRINGSIZE << index); |
563238c6 KS |
64 | if (sp->s_topFree == NOSTR) { |
65 | fprintf(stderr, "No room for space %d\n", index); | |
66 | panic("Internal error"); | |
67 | } | |
68 | sp->s_nextFree = sp->s_topFree; | |
69 | sp->s_nleft = STRINGSIZE << index; | |
70 | } | |
71 | sp->s_nleft -= s; | |
72 | t = sp->s_nextFree; | |
73 | sp->s_nextFree += s; | |
74 | return(t); | |
75 | } | |
76 | ||
77 | /* | |
78 | * Reset the string area to be empty. | |
79 | * Called to free all strings allocated | |
80 | * since last reset. | |
81 | */ | |
563238c6 KS |
82 | sreset() |
83 | { | |
84 | register struct strings *sp; | |
85 | register int index; | |
86 | ||
87 | if (noreset) | |
88 | return; | |
563238c6 KS |
89 | index = 0; |
90 | for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) { | |
91 | if (sp->s_topFree == NOSTR) | |
92 | continue; | |
93 | sp->s_nextFree = sp->s_topFree; | |
94 | sp->s_nleft = STRINGSIZE << index; | |
95 | index++; | |
96 | } | |
97 | } | |
f674e088 EW |
98 | |
99 | /* | |
100 | * Make the string area permanent. | |
101 | * Meant to be called in main, after initialization. | |
102 | */ | |
103 | spreserve() | |
104 | { | |
105 | register struct strings *sp; | |
106 | ||
107 | for (sp = &stringdope[0]; sp < &stringdope[NSPACE]; sp++) | |
108 | sp->s_topFree = NOSTR; | |
109 | } |