Fix if/else scope in yow.c from Rudi
[sxemacs] / src / sheap.c
1 /* Static Heap management routines for SXEmacs.
2    Copyright (C) 1994, 1998 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs
5
6 SXEmacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19
20 #include <config.h>
21 #include "lisp.h"
22
23 #include <unistd.h>
24 #include <sheap-adjust.h>
25
26 #define STATIC_HEAP_BASE        0x800000
27 #define STATIC_HEAP_SLOP        0x40000
28 #define STATIC_HEAP_SIZE \
29 (STATIC_HEAP_BASE + SHEAP_ADJUSTMENT + STATIC_HEAP_SLOP)
30 #define BLOCKSIZE       (1<<12)
31 #define ALLOC_UNIT (BLOCKSIZE-1)
32 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
33 #define ALIGN_ALLOC(addr) ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
34
35 char static_heap_buffer[STATIC_HEAP_SIZE] = { 0 };
36 char *static_heap_base = static_heap_buffer;
37 char *static_heap_ptr = static_heap_buffer;
38 unsigned long static_heap_size = STATIC_HEAP_SIZE;
39 int static_heap_initialized = 0;
40 int static_heap_dumped = 0;
41
42 void *more_static_core(ptrdiff_t increment);
43 void *more_static_core(ptrdiff_t increment)
44 {
45         int size = (int)increment;
46         void *result;
47
48         if (!static_heap_initialized) {
49 #ifdef VALMASK
50                 if (((unsigned long)static_heap_base & ~VALMASK) != 0) {
51                         printf
52                             ("error: The heap was allocated in upper memory.\n");
53                         exit(-1);
54                 }
55 #endif
56                 static_heap_base = (char *)ALIGN_ALLOC(static_heap_buffer);
57                 static_heap_ptr = static_heap_base;
58                 static_heap_size = STATIC_HEAP_SIZE -
59                     (static_heap_base - static_heap_buffer);
60                 static_heap_initialized = 1;
61         }
62
63         result = static_heap_ptr;
64
65         /* we don't need to align - handled by gmalloc.  */
66
67         if (size < 0) {
68                 if (static_heap_ptr + size < static_heap_base) {
69                         return 0;
70                 }
71         } else {
72                 if (static_heap_ptr + size >=
73                     static_heap_base + static_heap_size) {
74                         printf("\nRequested %d bytes, static heap exhausted! "
75                                "base is %p, current ptr is %p. "
76                                "You have exhausted the static heap.\n\n"
77                                "If you are simply trying to compile, remove "
78                                "sheap-adjust.h and recompile from the top "
79                                "level.\nIf this doesn't work then "
80                                "STATIC_HEAP_SLOP (defined in this file) is "
81                                "too small.\n\n"
82                                "If you want to run temacs, change "
83                                "SHEAP_ADJUSTMENT in sheap-adjust.h\nto 0 or "
84                                "a +ve number. Generally you should *not* try "
85                                "to run temacs\nwith a static heap, you should "
86                                "dump first.\n", size, static_heap_base,
87                                static_heap_ptr);
88
89                         exit(-1);
90                         return 0;
91                 }
92         }
93         static_heap_ptr += size;
94
95         return result;
96 }
97
98 static void sheap_adjust_h()
99 {
100         FILE *stream = fopen("sheap-adjust.h", "w");
101
102         if (stream == NULL)
103                 report_file_error("Opening sheap adjustment file",
104                                   Fcons(build_string("sheap-adjust.h"), Qnil));
105
106         fprintf(stream,
107                 "/*\tDo not edit this file!\n"
108                 "\tAutomatically generated by SXEmacs */\n"
109                 "# define SHEAP_ADJUSTMENT (%d)\n",
110                 ((static_heap_ptr - static_heap_buffer) - STATIC_HEAP_BASE));
111         fclose(stream);
112 }
113
114 void report_sheap_usage(int die_if_pure_storage_exceeded);
115 void report_sheap_usage(int die_if_pure_storage_exceeded)
116 {
117         int rc = 0;
118
119         size_t lost = (STATIC_HEAP_BASE + STATIC_HEAP_SLOP + SHEAP_ADJUSTMENT)
120             - (static_heap_ptr - static_heap_buffer);
121         char buf[200];
122         int n = snprintf(buf, sizeof(buf), "Static heap usage: %ld of %ld",
123                          (long)(static_heap_ptr - static_heap_buffer),
124                          (long)(STATIC_HEAP_BASE + STATIC_HEAP_SLOP + SHEAP_ADJUSTMENT));
125         assert(n>=0 && n < sizeof(buf));
126
127         if (lost > STATIC_HEAP_SLOP) {
128                 int len = strlen(buf);
129                 n = snprintf(buf + len, sizeof(buf)-len, " -- %ldk wasted",
130                              (long)(lost / 1024));
131                 assert(n>=0 && n < (sizeof(buf)-len));
132
133                 if (die_if_pure_storage_exceeded) {
134                         sheap_adjust_h();
135                         rc = -1;
136                 }
137                 message("%s", buf);
138         }
139
140         if (rc < 0) {
141                 unlink("SATISFIED");
142                 fatal
143                     ("Static heap size adjusted, Don't Panic!  I will restart the `make'");
144         }
145 }