2 * sgen-pointer-queue.c: A pointer queue that can be sorted.
4 * Copyright (C) 2014 Xamarin Inc
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License 2.0 as published by the Free Software Foundation;
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License 2.0 along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "mono/sgen/sgen-gc.h"
25 #include "mono/sgen/sgen-pointer-queue.h"
28 sgen_pointer_queue_clear (SgenPointerQueue *queue)
34 sgen_pointer_queue_init (SgenPointerQueue *queue, int mem_type)
39 queue->mem_type = mem_type;
43 realloc_queue (SgenPointerQueue *queue)
45 size_t new_size = queue->size ? queue->size + queue->size/2 : 1024;
46 void **new_data = sgen_alloc_internal_dynamic (sizeof (void*) * new_size, queue->mem_type, TRUE);
48 memcpy (new_data, queue->data, sizeof (void*) * queue->next_slot);
49 sgen_free_internal_dynamic (queue->data, sizeof (void*) * queue->size, queue->mem_type);
50 queue->data = new_data;
51 queue->size = new_size;
52 SGEN_LOG (4, "Reallocated pointer queue to size: %lu", new_size);
56 sgen_pointer_queue_will_grow (SgenPointerQueue *queue)
58 return queue->next_slot >= queue->size;
62 sgen_pointer_queue_add (SgenPointerQueue *queue, void *ptr)
64 if (sgen_pointer_queue_will_grow (queue))
65 realloc_queue (queue);
67 queue->data [queue->next_slot++] = ptr;
71 sgen_pointer_queue_pop (SgenPointerQueue *queue)
73 g_assert (queue->next_slot);
75 return queue->data [--queue->next_slot];
79 sgen_pointer_queue_search (SgenPointerQueue *queue, void *addr)
81 size_t first = 0, last = queue->next_slot;
82 while (first < last) {
83 size_t middle = first + ((last - first) >> 1);
84 if (addr <= queue->data [middle])
89 g_assert (first == last);
94 * Removes all NULL pointers from the queue.
97 sgen_pointer_queue_remove_nulls (SgenPointerQueue *queue)
99 void **start, **cur, **end;
100 start = cur = queue->data;
101 end = queue->data + queue->next_slot;
108 queue->next_slot = start - queue->data;
112 * Sorts the pointers in the queue, then removes duplicates.
115 sgen_pointer_queue_sort_uniq (SgenPointerQueue *queue)
117 void **start, **cur, **end;
118 /* sort and uniq pin_queue: we just sort and we let the rest discard multiple values */
119 /* it may be better to keep ranges of pinned memory instead of individually pinning objects */
120 SGEN_LOG (5, "Sorting pointer queue, size: %lu", queue->next_slot);
121 if (queue->next_slot > 1)
122 sgen_sort_addresses (queue->data, queue->next_slot);
123 start = cur = queue->data;
124 end = queue->data + queue->next_slot;
127 while (cur < end && *start == *cur)
131 queue->next_slot = start - queue->data;
132 SGEN_LOG (5, "Pointer queue reduced to size: %lu", queue->next_slot);
136 * Does a linear search through the pointer queue to find `ptr`. Returns the index if
137 * found, otherwise (size_t)-1.
140 sgen_pointer_queue_find (SgenPointerQueue *queue, void *ptr)
143 for (i = 0; i < queue->next_slot; ++i)
144 if (queue->data [i] == ptr)
150 sgen_pointer_queue_is_empty (SgenPointerQueue *queue)
152 return !queue->next_slot;
156 sgen_pointer_queue_free (SgenPointerQueue *queue)
158 sgen_free_internal_dynamic (queue->data, sizeof (void*) * queue->size, queue->mem_type);