view src/sheap.c @ 801:2b676dc88c66

[xemacs-hg @ 2002-04-01 03:58:02 by ben] bug fixes (e.g. ballooning on X windows) Makefile.in.in: Try to make the Makefile notice if its source Makefile.in.in is changed, and regenerate and run itself. Use a bigger default SHEAP_ADJUSTMENT on Cygwin; otherwise you can't compile under Mule if a Lisp file has changed. (can't run temacs) TODO.ben-mule-21-5: update. mule/mule-cmds.el: Hash the result of mswindows-get-language-environment-from-locale, since it's very expensive (and causes huge ballooning of memory under X Windows, since it's called from x-get-resource). cl-extra.el, code-files.el, files.el, simple.el, subr.el, x-faces.el: Create new string-equal-ignore-case, based on built-in compare-strings -- compare strings ignoring case without the need to generate garbage by calling downcase. Use it in equalp and elsewhere. alloc.c, bytecode.c, chartab.c, data.c, elhash.c, emacs.c, eval.c, event-Xt.c, event-unixoid.c, extents.c, file-coding.c, fileio.c, fns.c, glyphs.c, gutter.c, lisp-union.h, lisp.h, mule-charset.c, nt.c, process-unix.c, process.c, specifier.c, symbols.c, sysdep.c, sysdep.h, text.c, toolbar.c: Try to implement GC triggering based on percentage of total memory usage. Not currently activated (percentage set to 0) because not quite working. Add `memory-usage' primitive to return XEmacs' idea of its memory usage. Add primitive compare-strings, compatible with FSF 21.1 -- can compare any part of two strings, optionally ignoring case. Improve qxe() functions in text.c for text comparison. Use RETURN_NOT_REACHED to try to avoid warnings about unreachable code. Add volatile_make_int() to fix warning in unix_send_process().
author ben
date Mon, 01 Apr 2002 03:59:04 +0000
parents 943eaba38521
children 2b6fa2618f76
line wrap: on
line source

/* Static Heap management routines for XEmacs.
   Copyright (C) 1994, 1998 Free Software Foundation, Inc.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.*/

#include <config.h>
#include "lisp.h"

#include "sysfile.h"

#include <unistd.h>
#include <sheap-adjust.h>

#define STATIC_HEAP_BASE	0x800000
#define STATIC_HEAP_SLOP	0x40000
#define STATIC_HEAP_SIZE \
(STATIC_HEAP_BASE + SHEAP_ADJUSTMENT + STATIC_HEAP_SLOP)
#define BLOCKSIZE	(1<<12)
#define ALLOC_UNIT (BLOCKSIZE-1)
#define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
#define ALIGN_ALLOC(addr) ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)

char	static_heap_buffer[STATIC_HEAP_SIZE]={0};
char*	static_heap_base=static_heap_buffer;
char*	static_heap_ptr=static_heap_buffer;
unsigned long	static_heap_size=STATIC_HEAP_SIZE;
int 	static_heap_initialized=0;
int 	static_heap_dumped=0;

void* more_static_core ( ptrdiff_t increment );
void* more_static_core ( ptrdiff_t increment )
{
  int size = (int) increment;
  void *result;

  if (!static_heap_initialized)
    {
#ifdef VALMASK
      if (((unsigned long) static_heap_base & ~VALMASK) != 0)
	{
	  printf ("error: The heap was allocated in upper memory.\n");
	  exit (-1);
	}
#endif
      static_heap_base=(char*)ALIGN_ALLOC(static_heap_buffer);
      static_heap_ptr=static_heap_base;
      static_heap_size=STATIC_HEAP_SIZE -
	(static_heap_base-static_heap_buffer);
#ifdef CYGWIN
      sbrk(BLOCKSIZE);		/* force space for fork to work */
#endif
      static_heap_initialized=1;
    }

  result = static_heap_ptr;

  /* we don't need to align - handled by gmalloc.  */

  if (size < 0)
    {
      if (static_heap_ptr + size < static_heap_base)
	{
	  return 0;
	}
    }
  else
    {
      if (static_heap_ptr + size >= static_heap_base + static_heap_size)
	{
	  printf(

"\nRequested %d bytes, static heap exhausted!  base is %p, current ptr
is %p. You have exhausted the static heap. 

If you are simply trying to compile, remove sheap-adjust.h
and recompile from the top level. If this doesn't
work then STATIC_HEAP_SLOP (defined in this file) is too small.

If you want to run temacs, change SHEAP_ADJUSTMENT in sheap-adjust.h
to 0 or a +ve number. Generally you should *not* try to run temacs
with a static heap, you should dump first.\n", size,
static_heap_base, static_heap_ptr);

	  exit(-1);
	  return 0;
	}
    }
  static_heap_ptr += size;

  return result;
}

static void
sheap_adjust_h ()
{
  FILE *stream = retry_fopen ("sheap-adjust.h", "w");

  if (stream == NULL)
    report_file_error ("Opening sheap adjustment file",
		       build_string ("sheap-adjust.h"));

  fprintf (stream,
	   "/*\tDo not edit this file!\n"
	   "\tAutomatically generated by XEmacs */\n"
	   "# define SHEAP_ADJUSTMENT (%d)\n",
	   ((static_heap_ptr - static_heap_buffer) - STATIC_HEAP_BASE));
  retry_fclose (stream);
}

void report_sheap_usage (int die_if_pure_storage_exceeded);
void
report_sheap_usage (int die_if_pure_storage_exceeded)
{
  int rc = 0;

  Bytecount lost = (STATIC_HEAP_BASE + STATIC_HEAP_SLOP + SHEAP_ADJUSTMENT)
    - (static_heap_ptr - static_heap_buffer);
  char buf[200];
  sprintf (buf, "Static heap usage: %ld of %ld",
               (long) (static_heap_ptr - static_heap_buffer),
	   (long) (STATIC_HEAP_BASE + STATIC_HEAP_SLOP + SHEAP_ADJUSTMENT));

  if (lost > STATIC_HEAP_SLOP) {
    sprintf (buf + strlen (buf), " -- %ldk wasted", (long)(lost/1024));
    if (die_if_pure_storage_exceeded) {
      sheap_adjust_h();
      rc = -1;
    }
    message ("%s", buf);
  }

  if (rc < 0) {
    unlink("SATISFIED");
    fatal ("Static heap size adjusted, Don't Panic!  I will restart the `make'");
  }
}