428
+ − 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 "lisp.h"
442
+ − 23
771
+ − 24 #include "sysfile.h"
+ − 25
428
+ − 26 #include <unistd.h>
+ − 27 #include <sheap-adjust.h>
+ − 28
+ − 29 #define STATIC_HEAP_BASE 0x800000
+ − 30 #define STATIC_HEAP_SLOP 0x40000
+ − 31 #define STATIC_HEAP_SIZE \
+ − 32 (STATIC_HEAP_BASE + SHEAP_ADJUSTMENT + STATIC_HEAP_SLOP)
+ − 33 #define BLOCKSIZE (1<<12)
+ − 34 #define ALLOC_UNIT (BLOCKSIZE-1)
+ − 35 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
+ − 36 #define ALIGN_ALLOC(addr) ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
+ − 37
+ − 38 char static_heap_buffer[STATIC_HEAP_SIZE]={0};
+ − 39 char* static_heap_base=static_heap_buffer;
+ − 40 char* static_heap_ptr=static_heap_buffer;
+ − 41 unsigned long static_heap_size=STATIC_HEAP_SIZE;
+ − 42 int static_heap_initialized=0;
+ − 43 int static_heap_dumped=0;
+ − 44
442
+ − 45 void* more_static_core ( ptrdiff_t increment );
428
+ − 46 void* more_static_core ( ptrdiff_t increment )
+ − 47 {
+ − 48 int size = (int) increment;
+ − 49 void *result;
+ − 50
+ − 51 if (!static_heap_initialized)
+ − 52 {
+ − 53 #ifdef VALMASK
+ − 54 if (((unsigned long) static_heap_base & ~VALMASK) != 0)
+ − 55 {
+ − 56 printf ("error: The heap was allocated in upper memory.\n");
+ − 57 exit (-1);
+ − 58 }
+ − 59 #endif
+ − 60 static_heap_base=(char*)ALIGN_ALLOC(static_heap_buffer);
+ − 61 static_heap_ptr=static_heap_base;
+ − 62 static_heap_size=STATIC_HEAP_SIZE -
+ − 63 (static_heap_base-static_heap_buffer);
442
+ − 64 #ifdef CYGWIN
428
+ − 65 sbrk(BLOCKSIZE); /* force space for fork to work */
+ − 66 #endif
+ − 67 static_heap_initialized=1;
+ − 68 }
+ − 69
+ − 70 result = static_heap_ptr;
+ − 71
+ − 72 /* we don't need to align - handled by gmalloc. */
+ − 73
+ − 74 if (size < 0)
+ − 75 {
+ − 76 if (static_heap_ptr + size < static_heap_base)
+ − 77 {
+ − 78 return 0;
+ − 79 }
+ − 80 }
+ − 81 else
+ − 82 {
+ − 83 if (static_heap_ptr + size >= static_heap_base + static_heap_size)
+ − 84 {
+ − 85 printf(
+ − 86
+ − 87 "\nRequested %d bytes, static heap exhausted! base is %p, current ptr
+ − 88 is %p. You have exhausted the static heap.
+ − 89
+ − 90 If you are simply trying to compile, remove sheap-adjust.h
+ − 91 and recompile from the top level. If this doesn't
+ − 92 work then STATIC_HEAP_SLOP (defined in this file) is too small.
+ − 93
+ − 94 If you want to run temacs, change SHEAP_ADJUSTMENT in sheap-adjust.h
+ − 95 to 0 or a +ve number. Generally you should *not* try to run temacs
+ − 96 with a static heap, you should dump first.\n", size,
+ − 97 static_heap_base, static_heap_ptr);
+ − 98
+ − 99 exit(-1);
+ − 100 return 0;
+ − 101 }
+ − 102 }
+ − 103 static_heap_ptr += size;
+ − 104
+ − 105 return result;
+ − 106 }
+ − 107
+ − 108 static void
+ − 109 sheap_adjust_h ()
+ − 110 {
771
+ − 111 FILE *stream = retry_fopen ("sheap-adjust.h", "w");
428
+ − 112
+ − 113 if (stream == NULL)
+ − 114 report_file_error ("Opening sheap adjustment file",
563
+ − 115 build_string ("sheap-adjust.h"));
428
+ − 116
+ − 117 fprintf (stream,
+ − 118 "/*\tDo not edit this file!\n"
+ − 119 "\tAutomatically generated by XEmacs */\n"
+ − 120 "# define SHEAP_ADJUSTMENT (%d)\n",
+ − 121 ((static_heap_ptr - static_heap_buffer) - STATIC_HEAP_BASE));
771
+ − 122 retry_fclose (stream);
428
+ − 123 }
+ − 124
442
+ − 125 void report_sheap_usage (int die_if_pure_storage_exceeded);
428
+ − 126 void
+ − 127 report_sheap_usage (int die_if_pure_storage_exceeded)
+ − 128 {
+ − 129 int rc = 0;
+ − 130
665
+ − 131 Bytecount lost = (STATIC_HEAP_BASE + STATIC_HEAP_SLOP + SHEAP_ADJUSTMENT)
428
+ − 132 - (static_heap_ptr - static_heap_buffer);
+ − 133 char buf[200];
+ − 134 sprintf (buf, "Static heap usage: %ld of %ld",
+ − 135 (long) (static_heap_ptr - static_heap_buffer),
+ − 136 (long) (STATIC_HEAP_BASE + STATIC_HEAP_SLOP + SHEAP_ADJUSTMENT));
+ − 137
+ − 138 if (lost > STATIC_HEAP_SLOP) {
+ − 139 sprintf (buf + strlen (buf), " -- %ldk wasted", (long)(lost/1024));
+ − 140 if (die_if_pure_storage_exceeded) {
+ − 141 sheap_adjust_h();
+ − 142 rc = -1;
+ − 143 }
+ − 144 message ("%s", buf);
+ − 145 }
+ − 146
+ − 147 if (rc < 0) {
+ − 148 unlink("SATISFIED");
+ − 149 fatal ("Static heap size adjusted, Don't Panic! I will restart the `make'");
+ − 150 }
+ − 151 }
+ − 152
+ − 153