2008-06-13 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / rawbuffer.c
index 16874628c720c5ea72929e4ef510ac3519ca9153..167d9b8391d9480767114e007cb819af3cf03467 100644 (file)
@@ -11,7 +11,9 @@
 #define USE_WIN32_API          1
 #endif
 
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <errno.h>
 #ifdef USE_WIN32_API
 #include <winsock2.h>
@@ -36,6 +38,8 @@
 
 static GHashTable *mmap_map = NULL;
 static size_t alignment = 0;
+#define mono_mmap_lock() EnterCriticalSection (&mmap_mutex)
+#define mono_mmap_unlock() LeaveCriticalSection (&mmap_mutex)
 static CRITICAL_SECTION mmap_mutex;
 static gboolean make_unreadable = FALSE;
 static guint32 n_pagefaults = 0;
@@ -87,6 +91,14 @@ mono_raw_buffer_init (void)
        mmap_map = g_hash_table_new (NULL, NULL);
 }
 
+void
+mono_raw_buffer_cleanup (void)
+{
+       g_hash_table_destroy (mmap_map);
+
+       DeleteCriticalSection (&mmap_mutex);
+}
+
 static void *
 mono_raw_buffer_load_mmap (int fd, int is_writable, guint32 base, size_t size)
 {
@@ -121,9 +133,9 @@ mono_raw_buffer_load_mmap (int fd, int is_writable, guint32 base, size_t size)
                return 0;
        }
 
-       EnterCriticalSection (&mmap_mutex);
+       mono_mmap_lock ();
        g_hash_table_insert (mmap_map, ptr, GINT_TO_POINTER (mapping));
-       LeaveCriticalSection (&mmap_mutex);
+       mono_mmap_unlock ();
        
        return ((char *)ptr) + (base - start);
 
@@ -157,18 +169,16 @@ mono_raw_buffer_load_mmap (int fd, int is_writable, guint32 base, size_t size)
         * http://bugzilla.ximian.com/show_bug.cgi?id=49499
         * for more info.
         */
-       if (mprotect (ptr, end - start, prot | PROT_EXEC) != 0)
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                                  ": mprotect failed: %s", g_strerror (errno));
+       mprotect (ptr, end - start, prot | PROT_EXEC);
 
        if (make_unreadable) {
                int res = mprotect (ptr, end - start, 0);
                g_assert (res == 0);
        }
 
-       EnterCriticalSection (&mmap_mutex);
+       mono_mmap_lock ();
        g_hash_table_insert (mmap_map, ptr, GINT_TO_POINTER (size));
-       LeaveCriticalSection (&mmap_mutex);
+       mono_mmap_unlock ();
 
        return ((char *)ptr) + (base - start);
 #endif
@@ -179,9 +189,9 @@ mono_raw_buffer_free_mmap (void *base)
 {
        int value;
 
-       EnterCriticalSection (&mmap_mutex);
+       mono_mmap_lock ();
        value = GPOINTER_TO_INT (g_hash_table_lookup (mmap_map, base));
-       LeaveCriticalSection (&mmap_mutex);
+       mono_mmap_unlock ();
 
 #ifdef USE_WIN32_API
        UnmapViewOfFile (base);
@@ -221,9 +231,9 @@ mono_raw_buffer_update (void *buffer, size_t size)
 
        mmap_base =  (gpointer)(ROUND_DOWN ((UINTPTR_TYPE) (buffer), alignment));
 
-       EnterCriticalSection (&mmap_mutex);
+       mono_mmap_lock ();
        exists = g_hash_table_lookup (mmap_map, mmap_base) != NULL;
-       LeaveCriticalSection (&mmap_mutex);
+       mono_mmap_unlock ();
        if (exists)
                mono_raw_buffer_update_mmap (mmap_base, size);
 }
@@ -287,9 +297,9 @@ mono_raw_buffer_is_pagefault (void *ptr)
        data.found = FALSE;
        data.ptr = ptr;
 
-       EnterCriticalSection (&mmap_mutex);
+       mono_mmap_lock ();
        g_hash_table_foreach (mmap_map, (GHFunc)find_map, &data);
-       LeaveCriticalSection (&mmap_mutex);
+       mono_mmap_unlock ();
 
        return data.found;
 }
@@ -306,12 +316,12 @@ mono_raw_buffer_handle_pagefault (void *ptr)
        guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), alignment);
        int res;
 
-       EnterCriticalSection (&mmap_mutex);
+       mono_mmap_lock ();
        res = mprotect (start, alignment, PROT_READ);
        g_assert (res == 0);
 
        n_pagefaults ++;
-       LeaveCriticalSection (&mmap_mutex);
+       mono_mmap_unlock ();
 #endif
 }