[runtime] Fix DISABLE_REFLECTION_EMIT build.
[mono.git] / mono / utils / mono-mmap.c
index ccfcb0b8ac029d3e64c318cc5d98f447d9aea6cf..3fa797bbd99dcb06b756647189b34b4b1420d476 100644 (file)
@@ -81,16 +81,29 @@ aligned_address (char *mem, size_t size, size_t alignment)
        return aligned;
 }
 
-static volatile size_t allocation_count [MONO_MEM_ACCOUNT_MAX];
+static size_t allocation_count [MONO_MEM_ACCOUNT_MAX];
+static size_t total_allocation_count;
+static size_t alloc_limit;
 
 void
 account_mem (MonoMemAccountType type, ssize_t size)
 {
-#if SIZEOF_VOID_P == 4
-       InterlockedAdd ((volatile gint32*)&allocation_count [type], (gint32)size);
-#else
-       InterlockedAdd64 ((volatile gint64*)&allocation_count [type], (gint64)size);
-#endif
+       InterlockedAddP (&allocation_count [type], size);
+       InterlockedAddP (&total_allocation_count, size);
+}
+
+void
+mono_valloc_set_limit (size_t size)
+{
+       alloc_limit = size;
+}
+
+gboolean
+mono_valloc_can_alloc (size_t size)
+{
+       if (alloc_limit)
+               return (total_allocation_count + size) < alloc_limit;
+       return TRUE;
 }
 
 const char*
@@ -150,9 +163,17 @@ int
 mono_pagesize (void)
 {
        static int saved_pagesize = 0;
+
        if (saved_pagesize)
                return saved_pagesize;
+
+       // Prefer sysconf () as it's signal safe.
+#if defined (HAVE_SYSCONF) && defined (_SC_PAGESIZE)
+       saved_pagesize = sysconf (_SC_PAGESIZE);
+#else
        saved_pagesize = getpagesize ();
+#endif
+
        return saved_pagesize;
 }
 
@@ -194,6 +215,10 @@ mono_valloc (void *addr, size_t length, int flags, MonoMemAccountType type)
        void *ptr;
        int mflags = 0;
        int prot = prot_from_flags (flags);
+
+       if (!mono_valloc_can_alloc (length))
+               return NULL;
+
        /* translate the flags */
        if (flags & MONO_MMAP_FIXED)
                mflags |= MAP_FIXED;
@@ -314,20 +339,6 @@ mono_file_unmap (void *addr, void *handle)
  * \p length must be a multiple of the page size.
  * \returns \c 0 on success.
  */
-#if defined(__native_client__)
-int
-mono_mprotect (void *addr, size_t length, int flags)
-{
-       int prot = prot_from_flags (flags);
-       void *new_addr;
-
-       if (flags & MONO_MMAP_DISCARD) memset (addr, 0, length);
-
-       new_addr = mmap(addr, length, prot, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
-       if (new_addr == addr) return 0;
-        return -1;
-}
-#else
 int
 mono_mprotect (void *addr, size_t length, int flags)
 {
@@ -350,7 +361,6 @@ mono_mprotect (void *addr, size_t length, int flags)
        }
        return mprotect (addr, length, prot);
 }
-#endif // __native_client__
 
 #else
 
@@ -370,13 +380,17 @@ mono_valloc_granule (void)
 void*
 mono_valloc (void *addr, size_t length, int flags, MonoMemAccountType type)
 {
-       return g_malloc (length);
+       g_assert (addr == NULL);
+       return mono_valloc_aligned (length, mono_pagesize (), flags, type);
 }
 
 void*
 mono_valloc_aligned (size_t size, size_t alignment, int flags, MonoMemAccountType type)
 {
-       g_assert_not_reached ();
+       void *res = NULL;
+       if (posix_memalign (&res, alignment, size))
+               return NULL;
+       return res;
 }
 
 #define HAVE_VALLOC_ALIGNED
@@ -407,7 +421,7 @@ static gboolean
 shared_area_disabled (void)
 {
        if (!use_shared_area) {
-               if (g_getenv ("MONO_DISABLE_SHARED_AREA"))
+               if (g_hasenv ("MONO_DISABLE_SHARED_AREA"))
                        use_shared_area = -1;
                else
                        use_shared_area = 1;