3 * A pointer array that doesn't use reallocs.
5 * Copyright (C) 2016 Xamarin Inc
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 #ifndef __MONO_SGEN_ARRAY_LIST_H__
11 #define __MONO_SGEN_ARRAY_LIST_H__
15 #define SGEN_ARRAY_LIST_BUCKETS (32)
16 #define SGEN_ARRAY_LIST_MIN_BUCKET_BITS (5)
17 #define SGEN_ARRAY_LIST_MIN_BUCKET_SIZE (1 << SGEN_ARRAY_LIST_MIN_BUCKET_BITS)
19 typedef void (*SgenArrayListBucketAllocCallback
) (gpointer
*bucket
, guint32 new_bucket_size
, gboolean alloc
);
20 typedef gboolean (*SgenArrayListIsSlotSetFunc
) (volatile gpointer
*slot
);
21 typedef gboolean (*SgenArrayListSetSlotFunc
) (volatile gpointer
*slot
, gpointer ptr
, int data
);
24 * 'entries' is an array of pointers to buckets of increasing size. The first
25 * bucket has size 'MIN_BUCKET_SIZE', and each bucket is twice the size of the
28 * |-------|-- MIN_BUCKET_SIZE
30 * [1] -> xxxxxxxxxxxxxxxx
31 * [2] -> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
34 * 'slot_hint' denotes the position of the last allocation, so that the
35 * whole array needn't be searched on every allocation.
37 * The size of the spine, 'SGEN_ARRAY_LIST_BUCKETS', is chosen so
38 * that the maximum number of entries is no less than G_MAXUINT32.
42 volatile gpointer
*volatile entries
[SGEN_ARRAY_LIST_BUCKETS
];
43 volatile guint32 capacity
;
44 volatile guint32 slot_hint
;
45 volatile guint32 next_slot
;
46 SgenArrayListBucketAllocCallback bucket_alloc_callback
;
47 SgenArrayListIsSlotSetFunc is_slot_set_func
;
48 SgenArrayListSetSlotFunc set_slot_func
;
49 int mem_type
; /* sgen internal mem type or -1 for malloc allocation */
53 * Computes floor(log2(index + MIN_BUCKET_SIZE)) - 1, giving the index
54 * of the bucket containing a slot.
57 sgen_array_list_index_bucket (guint32 index
)
60 return CHAR_BIT
* sizeof (index
) - __builtin_clz (index
+ SGEN_ARRAY_LIST_MIN_BUCKET_SIZE
) - 1 - SGEN_ARRAY_LIST_MIN_BUCKET_BITS
;
63 index
+= SGEN_ARRAY_LIST_MIN_BUCKET_SIZE
;
68 return count
- 1 - SGEN_ARRAY_LIST_MIN_BUCKET_BITS
;
73 sgen_array_list_bucket_size (guint32 index
)
75 return 1 << (index
+ SGEN_ARRAY_LIST_MIN_BUCKET_BITS
);
79 sgen_array_list_bucketize (guint32 index
, guint32
*bucket
, guint32
*offset
)
81 *bucket
= sgen_array_list_index_bucket (index
);
82 *offset
= index
- sgen_array_list_bucket_size (*bucket
) + SGEN_ARRAY_LIST_MIN_BUCKET_SIZE
;
85 static inline volatile gpointer
*
86 sgen_array_list_get_slot (SgenArrayList
*array
, guint32 index
)
88 guint32 bucket
, offset
;
90 SGEN_ASSERT (0, index
< array
->capacity
, "Why are we accessing an entry that is not allocated");
92 sgen_array_list_bucketize (index
, &bucket
, &offset
);
93 return &(array
->entries
[bucket
] [offset
]);
96 #define SGEN_ARRAY_LIST_INIT(bucket_alloc_callback, is_slot_set_func, set_slot_func, mem_type) { { NULL }, 0, 0, 0, (bucket_alloc_callback), (is_slot_set_func), (set_slot_func), (mem_type) }
98 #define SGEN_ARRAY_LIST_FOREACH_SLOT(array, slot) { \
99 guint32 __bucket, __offset; \
100 const guint32 __max_bucket = sgen_array_list_index_bucket ((array)->capacity); \
101 guint32 __index = 0; \
102 const guint32 __next_slot = (array)->next_slot; \
103 for (__bucket = 0; __bucket < __max_bucket; ++__bucket) { \
104 volatile gpointer *__entries = (array)->entries [__bucket]; \
105 for (__offset = 0; __offset < sgen_array_list_bucket_size (__bucket); ++__offset, ++__index) { \
106 if (__index >= __next_slot) \
108 slot = &__entries [__offset];
110 #define SGEN_ARRAY_LIST_END_FOREACH_SLOT } } }
112 #define SGEN_ARRAY_LIST_FOREACH_SLOT_RANGE(array, begin, end, slot, index) { \
113 for (index = (begin); index < (end); index++) { \
114 guint32 __bucket, __offset; \
115 volatile gpointer *__entries; \
116 sgen_array_list_bucketize (index, &__bucket, &__offset); \
117 __entries = (array)->entries [__bucket]; \
118 slot = &__entries [__offset];
120 #define SGEN_ARRAY_LIST_END_FOREACH_SLOT_RANGE } }
122 guint32
sgen_array_list_alloc_block (SgenArrayList
*array
, guint32 slots_to_add
);
123 guint32
sgen_array_list_add (SgenArrayList
*array
, gpointer ptr
, int data
, gboolean increase_size_before_set
);
124 guint32
sgen_array_list_find (SgenArrayList
*array
, gpointer ptr
);
125 gboolean
sgen_array_list_default_cas_setter (volatile gpointer
*slot
, gpointer ptr
, int data
);
126 gboolean
sgen_array_list_default_is_slot_set (volatile gpointer
*slot
);
127 void sgen_array_list_remove_nulls (SgenArrayList
*array
);