Merge pull request #3547 from cmp-/remove-obsolete-stack-checks-win32
[mono.git] / mono / utils / lock-free-array-queue.c
index 6f692f976e8f169f54c51ca5057fb2aeec2044f9..5e48558a0d94c438b9afd9db4994ce8c538b7002 100644 (file)
@@ -3,6 +3,7 @@
  * require hazard pointers.
  *
  * (C) Copyright 2011 Xamarin Inc.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 /*
  * entry data, and then sets the state to USED or FREE.
  */
 
-#include <mono/io-layer/io-layer.h>
+#include <string.h>
+
+#include <mono/utils/atomic.h>
 #include <mono/utils/mono-membar.h>
+#ifdef SGEN_WITHOUT_MONO
+#include <mono/sgen/sgen-gc.h>
+#include <mono/sgen/sgen-client.h>
+#else
 #include <mono/utils/mono-mmap.h>
+#endif
 
 #include <mono/utils/lock-free-array-queue.h>
 
@@ -37,16 +45,16 @@ alloc_chunk (MonoLockFreeArray *arr)
 {
        int size = mono_pagesize ();
        int num_entries = (size - (sizeof (Chunk) - arr->entry_size * MONO_ZERO_LEN_ARRAY)) / arr->entry_size;
-       Chunk *chunk = mono_valloc (0, size, MONO_MMAP_READ | MONO_MMAP_WRITE);
+       Chunk *chunk = (Chunk *) mono_valloc (NULL, size, MONO_MMAP_READ | MONO_MMAP_WRITE, arr->account_type);
        g_assert (chunk);
        chunk->num_entries = num_entries;
        return chunk;
 }
 
 static void
-free_chunk (Chunk *chunk)
+free_chunk (Chunk *chunk, MonoMemAccountType type)
 {
-       mono_vfree (chunk, mono_pagesize ());
+       mono_vfree (chunk, mono_pagesize (), type);
 }
 
 gpointer
@@ -60,7 +68,7 @@ mono_lock_free_array_nth (MonoLockFreeArray *arr, int index)
                chunk = alloc_chunk (arr);
                mono_memory_write_barrier ();
                if (InterlockedCompareExchangePointer ((volatile gpointer *)&arr->chunk_list, chunk, NULL) != NULL)
-                       free_chunk (chunk);
+                       free_chunk (chunk, arr->account_type);
        }
 
        chunk = arr->chunk_list;
@@ -72,7 +80,7 @@ mono_lock_free_array_nth (MonoLockFreeArray *arr, int index)
                        next = alloc_chunk (arr);
                        mono_memory_write_barrier ();
                        if (InterlockedCompareExchangePointer ((volatile gpointer *) &chunk->next, next, NULL) != NULL) {
-                               free_chunk (next);
+                               free_chunk (next, arr->account_type);
                                next = chunk->next;
                                g_assert (next);
                        }
@@ -108,7 +116,7 @@ mono_lock_free_array_cleanup (MonoLockFreeArray *arr)
        arr->chunk_list = NULL;
        while (chunk) {
                Chunk *next = chunk->next;
-               free_chunk (chunk);
+               free_chunk (chunk, arr->account_type);
                chunk = next;
        }
 }
@@ -137,7 +145,7 @@ mono_lock_free_array_queue_push (MonoLockFreeArrayQueue *q, gpointer entry_data_
 
        do {
                index = InterlockedIncrement (&q->num_used_entries) - 1;
-               entry = mono_lock_free_array_nth (&q->array, index);
+               entry = (Entry *) mono_lock_free_array_nth (&q->array, index);
        } while (InterlockedCompareExchange (&entry->state, STATE_BUSY, STATE_FREE) != STATE_FREE);
 
        mono_memory_write_barrier ();
@@ -172,7 +180,7 @@ mono_lock_free_array_queue_pop (MonoLockFreeArrayQueue *q, gpointer entry_data_p
                                return FALSE;
                } while (InterlockedCompareExchange (&q->num_used_entries, index - 1, index) != index);
 
-               entry = mono_lock_free_array_nth (&q->array, index - 1);
+               entry = (Entry *) mono_lock_free_array_nth (&q->array, index - 1);
        } while (InterlockedCompareExchange (&entry->state, STATE_BUSY, STATE_USED) != STATE_USED);
 
        /* Reading the item must happen before CASing the state. */