@@ -72,7 +72,7 @@ INTERNAL_HIDDEN void create_canary_chunks(iso_alloc_zone_t *zone) {
7272 bitmap_index_t * bm = (bitmap_index_t * ) zone -> bitmap_start ;
7373 bit_slot_t bit_slot ;
7474
75- const bitmap_index_t max_bitmap_idx = GET_MAX_BITMASK_INDEX (zone ) - 1 ;
75+ const bitmap_index_t max_bitmap_idx = (zone -> max_bitmap_idx - 1 ) ;
7676
7777 /* Roughly %1 of the chunks in this zone will become a canary */
7878 const uint64_t canary_count = (zone -> chunk_count >> CANARY_COUNT_DIV );
@@ -174,7 +174,6 @@ INTERNAL_HIDDEN void _verify_all_zones(void) {
174174INTERNAL_HIDDEN void _verify_zone (iso_alloc_zone_t * zone ) {
175175 UNMASK_ZONE_PTRS (zone );
176176 const bitmap_index_t * bm = (bitmap_index_t * ) zone -> bitmap_start ;
177- const bitmap_index_t max_bm_idx = GET_MAX_BITMASK_INDEX (zone );
178177 bit_slot_t bit_slot ;
179178
180179 if (zone -> next_sz_index > _root -> zones_used ) {
@@ -188,7 +187,7 @@ INTERNAL_HIDDEN void _verify_zone(iso_alloc_zone_t *zone) {
188187 }
189188 }
190189
191- for (bitmap_index_t i = 0 ; i < max_bm_idx ; i ++ ) {
190+ for (bitmap_index_t i = 0 ; i < zone -> max_bitmap_idx ; i ++ ) {
192191 for (int64_t j = 1 ; j < BITS_PER_QWORD ; j += BITS_PER_CHUNK ) {
193192 /* If this bit is set it is either a free chunk or
194193 * a canary chunk. Either way it should have a set
@@ -213,18 +212,17 @@ INTERNAL_HIDDEN void _verify_zone(iso_alloc_zone_t *zone) {
213212 * find any free slots. */
214213INTERNAL_HIDDEN INLINE void fill_free_bit_slot_cache (iso_alloc_zone_t * zone ) {
215214 const bitmap_index_t * bm = (bitmap_index_t * ) zone -> bitmap_start ;
216- const bitmap_index_t max_bitmap_idx = GET_MAX_BITMASK_INDEX (zone );
217215
218216 /* This gives us an arbitrary spot in the bitmap to
219217 * start searching but may mean we end up with a smaller
220218 * cache. This may negatively affect performance but
221219 * leads to a less predictable free list */
222220 bitmap_index_t bm_idx ;
223221
224- /* The largest max_bitmap_idx we will ever
222+ /* The largest zone-> max_bitmap_idx we will ever
225223 * have is 8192 for SMALLEST_CHUNK_SZ (16) */
226- if (max_bitmap_idx > ALIGNMENT ) {
227- bm_idx = ((uint32_t ) rand_uint64 () * (max_bitmap_idx - 1 ) >> 32 );
224+ if (zone -> max_bitmap_idx > ALIGNMENT ) {
225+ bm_idx = ((uint32_t ) rand_uint64 () * (zone -> max_bitmap_idx - 1 ) >> 32 );
228226 } else {
229227 bm_idx = 0 ;
230228 }
@@ -236,7 +234,7 @@ INTERNAL_HIDDEN INLINE void fill_free_bit_slot_cache(iso_alloc_zone_t *zone) {
236234 for (free_bit_slot_cache_index = 0 ; free_bit_slot_cache_index < BIT_SLOT_CACHE_SZ ; bm_idx ++ ) {
237235 /* Don't index outside of the bitmap or
238236 * we will return inaccurate bit slots */
239- if (UNLIKELY (bm_idx >= max_bitmap_idx )) {
237+ if (UNLIKELY (bm_idx >= zone -> max_bitmap_idx )) {
240238 zone -> free_bit_slot_cache_index = free_bit_slot_cache_index ;
241239 return ;
242240 }
@@ -452,18 +450,11 @@ INTERNAL_HIDDEN void _unmap_zone(iso_alloc_zone_t *zone) {
452450 chunk_lookup_table [ADDR_TO_CHUNK_TABLE (zone -> user_pages_start )] = 0 ;
453451
454452 munmap (zone -> bitmap_start , zone -> bitmap_size );
455- madvise (zone -> bitmap_start , zone -> bitmap_size , MADV_DONTNEED );
456453 munmap (zone -> bitmap_start - _root -> system_page_size , _root -> system_page_size );
457- madvise (zone -> bitmap_start - _root -> system_page_size , _root -> system_page_size , MADV_DONTNEED );
458454 munmap (zone -> bitmap_start + zone -> bitmap_size , _root -> system_page_size );
459- madvise (zone -> bitmap_start + zone -> bitmap_size , _root -> system_page_size , MADV_DONTNEED );
460-
461455 munmap (zone -> user_pages_start , ZONE_USER_SIZE );
462- madvise (zone -> user_pages_start , ZONE_USER_SIZE , MADV_DONTNEED );
463456 munmap (zone -> user_pages_start - _root -> system_page_size , _root -> system_page_size );
464- madvise (zone -> user_pages_start - _root -> system_page_size , _root -> system_page_size , MADV_DONTNEED );
465457 munmap (zone -> user_pages_start + ZONE_USER_SIZE , _root -> system_page_size );
466- madvise (zone -> user_pages_start + ZONE_USER_SIZE , _root -> system_page_size , MADV_DONTNEED );
467458}
468459
469460INTERNAL_HIDDEN void _iso_alloc_destroy_zone (iso_alloc_zone_t * zone ) {
@@ -690,6 +681,7 @@ INTERNAL_HIDDEN iso_alloc_zone_t *_iso_new_zone(size_t size, bool internal) {
690681 * then we need to allocate a minimum size bitmap */
691682 uint32_t bitmap_size = (new_zone -> chunk_count << BITS_PER_CHUNK_SHIFT ) >> BITS_PER_BYTE_SHIFT ;
692683 new_zone -> bitmap_size = (bitmap_size > sizeof (bitmap_index_t )) ? bitmap_size : sizeof (bitmap_index_t );
684+ new_zone -> max_bitmap_idx = new_zone -> bitmap_size >> 3 ;
693685
694686 /* All of the following fields are immutable
695687 * and should not change once they are set */
@@ -840,10 +832,9 @@ INTERNAL_HIDDEN iso_alloc_zone_t *_iso_new_zone(size_t size, bool internal) {
840832 * looking for empty holes (i.e. slot == 0) */
841833INTERNAL_HIDDEN bit_slot_t iso_scan_zone_free_slot (iso_alloc_zone_t * zone ) {
842834 const bitmap_index_t * bm = (bitmap_index_t * ) zone -> bitmap_start ;
843- const bitmap_index_t max_bm_idx = GET_MAX_BITMASK_INDEX (zone );
844835
845836 /* Iterate the entire bitmap a qword at a time */
846- for (bitmap_index_t i = 0 ; i < max_bm_idx ; i ++ ) {
837+ for (bitmap_index_t i = 0 ; i < zone -> max_bitmap_idx ; i ++ ) {
847838 /* If the byte is 0 then there are some free
848839 * slots we can use at this location */
849840 if (bm [i ] == 0x0 ) {
@@ -861,9 +852,8 @@ INTERNAL_HIDDEN bit_slot_t iso_scan_zone_free_slot(iso_alloc_zone_t *zone) {
861852 * that indicates there is at least 1 free bit slot */
862853INTERNAL_HIDDEN bit_slot_t iso_scan_zone_free_slot_slow (iso_alloc_zone_t * zone ) {
863854 const bitmap_index_t * bm = (bitmap_index_t * ) zone -> bitmap_start ;
864- const bitmap_index_t max_bm_idx = GET_MAX_BITMASK_INDEX (zone );
865855
866- for (bitmap_index_t i = 0 ; i < max_bm_idx ; i ++ ) {
856+ for (bitmap_index_t i = 0 ; i < zone -> max_bitmap_idx ; i ++ ) {
867857 for (int64_t j = 0 ; j < BITS_PER_QWORD ; j += BITS_PER_CHUNK ) {
868858 /* We can easily check if every bitslot represented by
869859 * this qword is allocated with or without canaries */
@@ -1175,10 +1165,6 @@ INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_alloc_bitslot_from_zone(bit_slot_t bit
11751165 void * p = POINTER_FROM_BITSLOT (zone , bitslot );
11761166 UNPOISON_ZONE_CHUNK (zone , p );
11771167
1178- #if !ENABLE_ASAN && !DISABLE_CANARY
1179- __builtin_prefetch (p , 1 );
1180- #endif
1181-
11821168 bitmap_index_t * bm = (bitmap_index_t * ) zone -> bitmap_start ;
11831169
11841170 /* Read out 64 bits from the bitmap. We will write
@@ -1187,9 +1173,9 @@ INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_alloc_bitslot_from_zone(bit_slot_t bit
11871173 * which could result in a page fault */
11881174 bitmap_index_t b = bm [dwords_to_bit_slot ];
11891175
1190- if (UNLIKELY (p > zone -> user_pages_start + ZONE_USER_SIZE )) {
1176+ if (UNLIKELY (p >= zone -> user_pages_start + ZONE_USER_SIZE )) {
11911177 LOG_AND_ABORT ("Allocating an address 0x%p from zone[%d], bit slot %lu %ld bytes %ld pages outside zones user pages 0x%p 0x%p" ,
1192- p , zone -> index , bitslot , p - ( zone -> user_pages_start + ZONE_USER_SIZE ) , (p - ( zone -> user_pages_start + ZONE_USER_SIZE ) ) / _root -> system_page_size ,
1178+ p , zone -> index , bitslot , p - zone -> user_pages_start + ZONE_USER_SIZE , (p - zone -> user_pages_start + ZONE_USER_SIZE ) / _root -> system_page_size ,
11931179 zone -> user_pages_start , zone -> user_pages_start + ZONE_USER_SIZE );
11941180 }
11951181
@@ -1257,7 +1243,7 @@ INTERNAL_HIDDEN uint8_t _iso_alloc_get_mem_tag(void *p, iso_alloc_zone_t *zone)
12571243 LOG_AND_ABORT ("Chunk offset %d not an alignment of %d" , chunk_offset , zone -> chunk_size );
12581244 }
12591245
1260- _mtp += (chunk_offset / zone -> chunk_size );
1246+ _mtp += (chunk_offset >> zone -> chunk_size_pow2 );
12611247 return * _mtp ;
12621248#else
12631249 return 0 ;
@@ -1351,7 +1337,7 @@ INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_alloc(iso_alloc_zone_t *zone, size_t s
13511337 * thread recently used for an alloc/free operation.
13521338 * It's likely we are allocating a similar size chunk
13531339 * and this will speed up that operation */
1354- for (int64_t i = 0 ; i < zone_cache_count ; i ++ ) {
1340+ for (size_t i = 0 ; i < zone_cache_count ; i ++ ) {
13551341 if (zone_cache [i ].chunk_size >= size ) {
13561342 bool fit = iso_does_zone_fit (zone_cache [i ].zone , size );
13571343
@@ -1504,7 +1490,7 @@ INTERNAL_HIDDEN iso_alloc_zone_t *iso_find_zone_bitmap_range(const void *restric
15041490 iso_alloc_zone_t * tmp_zone = NULL ;
15051491
15061492 /* Now we check the MRU thread zone cache */
1507- for (int64_t i = 0 ; i < zone_cache_count ; i ++ ) {
1493+ for (size_t i = 0 ; i < zone_cache_count ; i ++ ) {
15081494 tmp_zone = zone_cache [i ].zone ;
15091495 bitmap_start = UNMASK_BITMAP_PTR (tmp_zone );
15101496
@@ -1548,7 +1534,7 @@ INTERNAL_HIDDEN iso_alloc_zone_t *iso_find_zone_range(const void *restrict p) {
15481534 iso_alloc_zone_t * tmp_zone = NULL ;
15491535
15501536 /* Now we check the MRU thread zone cache */
1551- for (int64_t i = 0 ; i < zone_cache_count ; i ++ ) {
1537+ for (size_t i = 0 ; i < zone_cache_count ; i ++ ) {
15521538 tmp_zone = zone_cache [i ].zone ;
15531539 user_pages_start = UNMASK_USER_PTR (tmp_zone );
15541540
@@ -1757,10 +1743,6 @@ INTERNAL_HIDDEN void iso_free_chunk_from_zone(iso_alloc_zone_t *zone, void *rest
17571743 /* Set the next bit so we know this chunk was used */
17581744 SET_BIT (b , (which_bit + 1 ));
17591745
1760- #if !ENABLE_ASAN && (!DISABLE_CANARY || SANITIZE_CHUNKS )
1761- __builtin_prefetch (p , 1 );
1762- #endif
1763-
17641746 /* Unset the bit and write the value into the bitmap
17651747 * if this is not a permanent free. A permanent free
17661748 * means this chunk will be marked as if it is a canary */
@@ -1850,7 +1832,7 @@ INTERNAL_HIDDEN void _iso_free(void *p, bool permanent) {
18501832 }
18511833
18521834#if NO_ZERO_ALLOCATIONS
1853- if (p == _zero_alloc_page ) {
1835+ if (UNLIKELY ( p == _zero_alloc_page ) ) {
18541836 return ;
18551837 }
18561838#endif
@@ -1894,7 +1876,7 @@ INTERNAL_HIDDEN void _iso_free_size(void *p, size_t size) {
18941876 }
18951877
18961878#if NO_ZERO_ALLOCATIONS
1897- if (p == _zero_alloc_page && size != 0 ) {
1879+ if (UNLIKELY ( p == _zero_alloc_page && size != 0 ) ) {
18981880 LOG_AND_ABORT ("Zero sized chunk (0x%p) with non-zero (%d) size passed to free" , p , size );
18991881 }
19001882
0 commit comments