(code_convert_region): Fix previous change. Adjusted
[bpt/emacs.git] / src / w32heap.c
index 776f857..21a2fce 100644 (file)
@@ -1,4 +1,4 @@
-/* Heap management routines for GNU Emacs on Windows NT.
+/* 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.
@@ -26,7 +26,7 @@ Boston, MA 02111-1307, USA.
 #include <stdlib.h>
 #include <stdio.h>
 
-#include "ntheap.h"
+#include "w32heap.h"
 #include "lisp.h"  /* for VALMASK */
 
 /* This gives us the page size and the size of the allocation unit on NT.  */
@@ -38,8 +38,11 @@ int edata;
 int etext;
 
 /* The major and minor versions of NT.  */
-int nt_major_version;
-int nt_minor_version;
+int w32_major_version;
+int w32_minor_version;
+
+/* Distinguish between Windows NT and Windows 95.  */
+int os_subtype;
 
 /* Cache information describing the NT system for later use.  */
 void
@@ -58,14 +61,26 @@ cache_system_info (void)
 
   /* Cache the version of the operating system.  */
   version.data = GetVersion ();
-  nt_major_version = version.info.major;
-  nt_minor_version = version.info.minor;
+  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;
 }
 
+/* Emulate getpagesize.  */
+int
+getpagesize (void)
+{
+  return sysinfo_cache.dwPageSize;
+}
+
 /* Round ADDRESS up to be aligned with ALIGN.  */
 unsigned char *
 round_to_next (unsigned char *address, unsigned long align)
@@ -107,12 +122,12 @@ allocate_heap (void)
      the initial default process heap size and the executable image base
      address.  The link settings and the malloc heap base below must all
      correspond; the relationship between these values depends on how NT
-     and Win95 arrange the virtual address space for a process (and on
+     and Windows 95 arrange the virtual address space for a process (and on
      the size of the code and data segments in temacs.exe).
 
      The most important thing is to make base address for the executable
      image high enough to leave enough room between it and the 4MB floor
-     of the process address space on Win95 for the primary thread stack,
+     of the process address space on Windows 95 for the primary thread stack,
      the process default heap, and other assorted odds and ends
      (eg. environment strings, private system dll memory etc) that are
      allocated before temacs has a chance to grab its malloc arena.  The
@@ -132,7 +147,7 @@ allocate_heap (void)
      we will have plenty of room for expansion.
 
      Thus we would like to set the malloc heap base to 20MB.  However,
-     Win95 refuses to allocate the heap starting at this address, so we
+     Windows 95 refuses to allocate the heap starting at this address, so we
      set the base to 27MB to make it happy.  Since Emacs now leaves
      28 bits available for pointers, this lets us use the remainder of
      the region below the 256MB line for our malloc arena - 229MB is
@@ -142,19 +157,25 @@ allocate_heap (void)
   unsigned long end  = 1 << VALBITS; /* 256MB */
   void *ptr = NULL;
 
-#ifdef NTHEAP_PROBE_BASE
+#if NTHEAP_PROBE_BASE /* This is never normally defined */
+  /* Try various addresses looking for one the kernel will let us have.  */
   while (!ptr && (base < end))
     {
-#endif
       reserved_heap_size = end - base;
       ptr = VirtualAlloc ((void *) base,
                          get_reserved_heap_size (),
                          MEM_RESERVE,
                          PAGE_NOACCESS);
-#ifdef NTHEAP_PROBE_BASE
       base += 0x00100000;  /* 1MB increment */
     }
+#else
+  reserved_heap_size = end - base;
+  ptr = VirtualAlloc ((void *) base,
+                     get_reserved_heap_size (),
+                     MEM_RESERVE,
+                     PAGE_NOACCESS);
 #endif
+
   return ptr;
 }
 
@@ -261,6 +282,9 @@ recreate_heap (char *executable_path)
      any funny interactions between file I/O and file mapping.  */
   read_in_bss (executable_path);
   map_in_heap (executable_path);
+
+  /* Update system version information to match current system.  */
+  cache_system_info ();
 }
 
 /* Round the heap up to the given alignment.  */
@@ -276,3 +300,26 @@ round_heap (unsigned long align)
   if (need_to_alloc) 
     sbrk (need_to_alloc);
 }
+
+#if (_MSC_VER >= 1000)
+
+/* 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