testsuite: enable and fix swaps-p8 testcases
[gcc.git] / libvtv / vtv_malloc.cc
index 8aaa636e0e304b7e265affef0e5c3921e7d85cd8..ddc1a053f6f3aa28f7920dab08a2cf6ce31671f9 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (C) 2012-2013
-   Free Software Foundation
+/* Copyright (C) 2012-2020 Free Software Foundation, Inc.
 
    This file is part of GCC.
 
 
 #include <stdlib.h>
 #include <unistd.h>
+#if defined (__CYGWIN__) || defined (__MINGW32__)
+#include <windows.h>
+#else
 #include <sys/mman.h>
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -62,6 +65,18 @@ static void *current_chunk VTV_PROTECTED_VAR = 0;
 static size_t current_chunk_size VTV_PROTECTED_VAR = 0;
 static int malloc_initialized VTV_PROTECTED_VAR = 0;
 
+#if defined (__CYGWIN__) || defined (__MINGW32__)
+//sysconf(_SC_PAGE_SIZE) port
+long sysconf_SC_PAGE_SIZE()
+{
+  SYSTEM_INFO si;
+  GetSystemInfo(&si);
+  long pageSize = (long)si.dwPageSize;
+  return pageSize;
+  //return 4096; // standard usermode 32bit pagesize in bytes // FIXME
+}
+#endif
+
 /* The function goes through and counts all the pages we have allocated
    so far.  It returns the page count.  */
 
@@ -129,7 +144,7 @@ change_protections_on_data_chunks (int protection_flag)
     }
 
 #ifdef VTV_DEBUG
-    VTV_malloc_dump_stats ();
+    __vtv_malloc_dump_stats ();
 #endif
 }
 
@@ -162,8 +177,13 @@ obstack_chunk_alloc (size_t size)
   VTV_DEBUG_ASSERT ((size & (VTV_PAGE_SIZE - 1)) == 0);
   void *allocated;
 
+#if defined (__CYGWIN__) || defined (__MINGW32__)
+  if ((allocated = VirtualAlloc(NULL, size,  MEM_RESERVE|MEM_COMMIT,
+                         PAGE_READWRITE)) == 0)
+#else
   if ((allocated = mmap (NULL, size, PROT_READ | PROT_WRITE,
                          MAP_PRIVATE | MAP_ANONYMOUS,  -1, 0)) == 0)
+#endif
     VTV_error ();
 
   VTV_DEBUG_ASSERT (((unsigned long) allocated & (VTV_PAGE_SIZE - 1)) == 0);
@@ -174,7 +194,7 @@ obstack_chunk_alloc (size_t size)
 }
 
 static void
-obstack_chunk_free (size_t)
+obstack_chunk_free (void *)
 {
   /* Do nothing. For our purposes there should be very little
      de-allocation. */
@@ -190,17 +210,20 @@ __vtv_malloc_init (void)
   if (malloc_initialized)
     return;
 
+#if defined (__CYGWIN__) || defined (__MINGW32__)
+  if (VTV_PAGE_SIZE != sysconf_SC_PAGE_SIZE())
+#else
   if (VTV_PAGE_SIZE != sysconf (_SC_PAGE_SIZE))
+#endif
     VTV_error ();
 
-  obstack_chunk_size (&vtv_obstack) = VTV_PAGE_SIZE;
-  obstack_alignment_mask (&vtv_obstack) = sizeof (long) - 1;
   /* We guarantee that the obstack alloc failed handler will never be
      called because in case the allocation of the chunk fails, it will
      never return */
   obstack_alloc_failed_handler = NULL;
 
-  obstack_init (&vtv_obstack);
+  obstack_specify_allocation (&vtv_obstack, VTV_PAGE_SIZE, sizeof (long),
+                             obstack_chunk_alloc, obstack_chunk_free);
   malloc_initialized = 1;
 }