view src/w32heap.c @ 61126:c0aa521e0ca7

Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-220 Merge from gnus--rel--5.10 Patches applied: * gnus--rel--5.10 (patch 45-52) - Update from CVS - Update from CVS: texi Makefile.in CVS keyw cruft - Update from CVS: ChangeLog tweaks 2005-03-29 Reiner Steib <Reiner.Steib@gmx.de> * etc/gnus-refcard.tex, etc/gnus-logo.eps: New files. 2005-03-25 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/message.el (message-resend): Bind rfc2047-encode-encoded-words. * lisp/gnus/mm-util.el (mm-replace-in-string): New function. (mm-xemacs-find-mime-charset-1): Ignore errors while loading latin-unity, which cannot be used with XEmacs 21.1. * lisp/gnus/rfc2047.el (rfc2047-encode-function-alist): Rename from rfc2047-encoding-function-alist in order to avoid conflicting with the old version. (rfc2047-encode-message-header): Remove useless goto-char. (rfc2047-encodable-p): Don't move point. (rfc2047-syntax-table): Treat `(' and `)' as is. (rfc2047-encode-region): Concatenate words containing non-ASCII characters in structured fields; don't encode space-delimited ASCII words even in unstructured fields; don't break words at char-category boundaries; encode encoded words in structured fields; treat text within parentheses as special; show the original text when error has occurred; move point to the end of the region after encoding, suggested by IRIE Tetsuya <irie@t.email.ne.jp>; treat backslash-quoted characters as non-special; check carefully whether to encode special characters; fix some kind of misconfigured headers; signal a real error if debug-on-quit or debug-on-error is non-nil; don't infloop, suggested by Hiroshi Fujishima <pooh@nature.tsukuba.ac.jp>; assume the close parenthesis may be included in the encoded word; encode bogus delimiters. (rfc2047-encode-string): Use mm-with-multibyte-buffer. (rfc2047-encode-max-chars): New variable. (rfc2047-encode-1): New function. (rfc2047-encode): Use it; encode text so that it occupies the maximum width within 76-column; work correctly on Q encoding for iso-2022-* charsets; fold the line before encoding; don't append a space if the encoded word includes close parenthesis. (rfc2047-fold-region): Use existing whitespace for LWSP; make it sure not to break a line just after the header name. (rfc2047-b-encode-region): Remove. (rfc2047-b-encode-string): New function. (rfc2047-q-encode-region): Remove. (rfc2047-q-encode-string): New function. (rfc2047-encode-parameter): New function. (rfc2047-encoded-word-regexp): Don't use shy group. (rfc2047-decode-region): Follow rfc2047-encoded-word-regexp change. (rfc2047-parse-and-decode): Ditto. (rfc2047-decode): Treat the ascii coding-system as raw-text by default. 2005-03-25 Lars Magne Ingebrigtsen <larsi@gnus.org> * lisp/gnus/rfc2047.el (rfc2047-encode-encoded-words): New variable. (rfc2047-field-value): Strip props. (rfc2047-encode-message-header): Disabled header folding -- not all headers can be folded, and this should be done by the message composition mode. Probably. I think. (rfc2047-encodable-p): Say that =? needs encoding. (rfc2047-encode-region): Encode =? strings. 2005-03-25 Jesper Harder <harder@ifa.au.dk> * lisp/gnus/rfc2047.el (rfc2047-encoded-word-regexp): Support RFC 2231 language tags; remove unnecessary '+'. Reported by Stefan Wiens <s.wi@gmx.net>. (rfc2047-decode-string): Don't cons a string unnecessarily. (rfc2047-parse-and-decode, rfc2047-decode): Use a character for the encoding to avoid consing a string. (rfc2047-decode): Use mm-subst-char-in-string instead of mm-replace-chars-in-string. 2005-03-25 TSUCHIYA Masatoshi <tsuchiya@namazu.org> * lisp/gnus/rfc2047.el (rfc2047-encode): Use uppercase letters to specify encodings of MIME-encoded words, in order to improve interoperability with several broken MUAs. 2005-03-21 Reiner Steib <Reiner.Steib@gmx.de> * lisp/gnus/gnus-srvr.el (gnus-browse-select-group): Add NUMBER argument and pass it to `gnus-browse-read-group'. (gnus-browse-read-group): Add NUMBER argument and pass it to `gnus-group-read-ephemeral-group'. * lisp/gnus/gnus-group.el (gnus-group-read-ephemeral-group): Add NUMBER argument and pass it to `gnus-group-read-group'. 2005-03-19 Aidan Kehoe <kehoea@parhasard.net> * lisp/gnus/mm-util.el (mm-xemacs-find-mime-charset): Only call mm-xemacs-find-mime-charset-1 if we have the mule feature available at runtime. 2005-03-25 Katsumi Yamaoka <yamaoka@jpl.org> * man/emacs-mime.texi (Display Customization): Markup fixes. (rfc2047): Update. 2005-03-23 Reiner Steib <Reiner.Steib@gmx.de> * man/gnus-faq.texi: Replaced with auto-generated version.
author Miles Bader <miles@gnu.org>
date Wed, 30 Mar 2005 08:14:32 +0000
parents 272f0851d05e
children 292c20627319 4c90ffeb71c5
line wrap: on
line source

/* Heap management routines for GNU Emacs on the Microsoft W32 API.
   Copyright (C) 1994 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs 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.

GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

   Geoff Voelker (voelker@cs.washington.edu)			     7-29-94
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include "w32heap.h"
#include "lisp.h"  /* for VALMASK */

#undef RVA_TO_PTR
#define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))

/* This gives us the page size and the size of the allocation unit on NT.  */
SYSTEM_INFO sysinfo_cache;

/* This gives us version, build, and platform identification.  */
OSVERSIONINFO osinfo_cache;

unsigned long syspage_mask = 0;

/* These are defined to get Emacs to compile, but are not used.  */
int edata;
int etext;

/* The major and minor versions of NT.  */
int w32_major_version;
int w32_minor_version;
int w32_build_number;

/* Distinguish between Windows NT and Windows 95.  */
int os_subtype;

/* Cache information describing the NT system for later use.  */
void
cache_system_info (void)
{
  union
    {
      struct info
	{
	  char  major;
	  char  minor;
	  short platform;
	} info;
      DWORD data;
    } version;

  /* Cache the version of the operating system.  */
  version.data = GetVersion ();
  w32_major_version = version.info.major;
  w32_minor_version = version.info.minor;

  if (version.info.platform & 0x8000)
    os_subtype = OS_WIN95;
  else
    os_subtype = OS_NT;

  /* Cache page size, allocation unit, processor type, etc.  */
  GetSystemInfo (&sysinfo_cache);
  syspage_mask = sysinfo_cache.dwPageSize - 1;

  /* Cache os info.  */
  osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  GetVersionEx (&osinfo_cache);

  w32_build_number = osinfo_cache.dwBuildNumber;
  if (os_subtype == OS_WIN95)
    w32_build_number &= 0xffff;
}

/* Emulate getpagesize.  */
int
getpagesize (void)
{
  return sysinfo_cache.dwPageSize;
}

/* Info for managing our preload heap, which is essentially a fixed size
   data area in the executable.  */
PIMAGE_SECTION_HEADER preload_heap_section;

/* Info for keeping track of our heap.  */
unsigned char *data_region_base = NULL;
unsigned char *data_region_end = NULL;
unsigned char *real_data_region_end = NULL;
unsigned long  reserved_heap_size = 0;

/* The start of the data segment.  */
unsigned char *
get_data_start (void)
{
  return data_region_base;
}

/* The end of the data segment.  */
unsigned char *
get_data_end (void)
{
  return data_region_end;
}

static char *
allocate_heap (void)
{
  /* Try to get as much as possible of the address range from the end of
     the preload heap section up to the usable address limit.  Since GNU
     malloc can handle gaps in the memory it gets from sbrk, we can
     simply set the sbrk pointer to the base of the new heap region.  */
  unsigned long base =
    ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
	       + preload_heap_section->Misc.VirtualSize),
	      get_allocation_unit ());
  unsigned long end  = 1 << VALBITS; /* 256MB */
  void *ptr = NULL;

  while (!ptr && (base < end))
    {
      reserved_heap_size = end - base;
      ptr = VirtualAlloc ((void *) base,
			  get_reserved_heap_size (),
			  MEM_RESERVE,
			  PAGE_NOACCESS);
      base += 0x00100000;  /* 1MB increment */
    }

  return ptr;
}


/* Emulate Unix sbrk.  */
void *
sbrk (unsigned long increment)
{
  void *result;
  long size = (long) increment;

  result = data_region_end;

  /* If size is negative, shrink the heap by decommitting pages.  */
  if (size < 0)
    {
      int new_size;
      unsigned char *new_data_region_end;

      size = -size;

      /* Sanity checks.  */
      if ((data_region_end - size) < data_region_base)
	return NULL;

      /* We can only decommit full pages, so allow for
	 partial deallocation [cga].  */
      new_data_region_end = (data_region_end - size);
      new_data_region_end = (unsigned char *)
	((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
      new_size = real_data_region_end - new_data_region_end;
      real_data_region_end = new_data_region_end;
      if (new_size > 0)
	{
	  /* Decommit size bytes from the end of the heap.  */
	  if (using_dynamic_heap
	      && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
	    return NULL;
 	}

      data_region_end -= size;
    }
  /* If size is positive, grow the heap by committing reserved pages.  */
  else if (size > 0)
    {
      /* Sanity checks.  */
      if ((data_region_end + size) >
	  (data_region_base + get_reserved_heap_size ()))
	return NULL;

      /* Commit more of our heap. */
      if (using_dynamic_heap
	  && VirtualAlloc (data_region_end, size, MEM_COMMIT,
			   PAGE_READWRITE) == NULL)
	return NULL;
      data_region_end += size;

      /* We really only commit full pages, so record where
	 the real end of committed memory is [cga].  */
      real_data_region_end = (unsigned char *)
	  ((long) (data_region_end + syspage_mask) & ~syspage_mask);
    }

  return result;
}

/* Initialize the internal heap variables used by sbrk.  When running in
   preload phase (ie. in the undumped executable), we rely entirely on a
   fixed size heap section included in the .exe itself; this is
   preserved during dumping, and truncated to the size actually used.

   When running in the dumped executable, we reserve as much as possible
   of the address range that is addressable by Lisp object pointers, to
   supplement what is left of the preload heap.  Although we cannot rely
   on the dynamically allocated arena being contiguous with the static
   heap area, it is not a problem because sbrk can pretend that the gap
   was allocated by something else; GNU malloc detects when there is a
   jump in the sbrk values, and starts a new heap block.  */
void
init_heap ()
{
  PIMAGE_DOS_HEADER dos_header;
  PIMAGE_NT_HEADERS nt_header;

  dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
  nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
				   dos_header->e_lfanew);
  preload_heap_section = find_section ("EMHEAP", nt_header);

  if (using_dynamic_heap)
    {
      data_region_base = allocate_heap ();
      if (!data_region_base)
	{
	  printf ("Error: Could not reserve dynamic heap area.\n");
	  exit (1);
	}

#if defined (NO_UNION_TYPE) && !defined (USE_LSB_TAG)
      /* Ensure that the addresses don't use the upper tag bits since
	 the Lisp type goes there.  */
      if (((unsigned long) data_region_base & ~VALMASK) != 0)
	{
	  printf ("Error: The heap was allocated in upper memory.\n");
	  exit (1);
	}
#endif
      data_region_end = data_region_base;
      real_data_region_end = data_region_end;
    }
  else
    {
      data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
      data_region_end = data_region_base;
      real_data_region_end = data_region_end;
      reserved_heap_size = preload_heap_section->Misc.VirtualSize;
    }

  /* Update system version information to match current system.  */
  cache_system_info ();
}

/* Round the heap up to the given alignment.  */
void
round_heap (unsigned long align)
{
  unsigned long needs_to_be;
  unsigned long need_to_alloc;

  needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
  need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();

  if (need_to_alloc)
    sbrk (need_to_alloc);
}

#if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined(USE_CRT_DLL))

/* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
   a heap via HeapCreate.  They are normally defined by the runtime,
   but we override them here so that the unnecessary HeapCreate call
   is not performed.  */

int __cdecl
_heap_init (void)
{
  /* Stepping through the assembly indicates that mainCRTStartup is
     expecting a nonzero success return value.  */
  return 1;
}

void __cdecl
_heap_term (void)
{
  return;
}

#endif

/* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
   (do not change this comment) */