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