Loading...
--- libmalloc/libmalloc-646.0.13/src/magazine_tiny.c
+++ libmalloc/libmalloc-283.60.1/src/magazine_tiny.c
@@ -38,6 +38,25 @@
// reduce the msize part below zero (not checked).
#define TINY_MAG_LAST_FREE_PTR_DEC_MSIZE(x, msize_delta) (x) = ((void *)(x) - (msize_delta))
+static MALLOC_INLINE MALLOC_ALWAYS_INLINE
+mag_index_t
+tiny_mag_get_thread_index(void)
+{
+#if CONFIG_TINY_USES_HYPER_SHIFT
+ if (os_likely(_os_cpu_number_override == -1)) {
+ return _os_cpu_number() >> hyper_shift;
+ } else {
+ return _os_cpu_number_override >> hyper_shift;
+ }
+#else // CONFIG_SMALL_USES_HYPER_SHIFT
+ if (os_likely(_os_cpu_number_override == -1)) {
+ return _os_cpu_number();
+ } else {
+ return _os_cpu_number_override;
+ }
+#endif // CONFIG_SMALL_USES_HYPER_SHIFT
+}
+
static inline grain_t
tiny_slot_from_msize(msize_t msize)
{
@@ -55,18 +74,14 @@
// check whether the previous block is in the tiny region and a block header
// if so, then the size of the previous block is one, and there is no stored
// size.
- if (ptr != TINY_REGION_HEAP_BASE(TINY_REGION_FOR_PTR(ptr))) {
+ if (ptr != TINY_REGION_FOR_PTR(ptr)) {
void *prev_block = (void *)((uintptr_t)ptr - TINY_QUANTUM);
uint32_t *prev_header = TINY_BLOCK_HEADER_FOR_PTR(prev_block);
msize_t prev_index = TINY_INDEX_FOR_PTR(prev_block);
if (BITARRAY_BIT(prev_header, prev_index)) {
return 1;
}
- msize_t *prev_msize_ptr = &TINY_PREVIOUS_MSIZE(ptr);
- // This is a speculative read of potentially in-use app memory, we need
- // to use _malloc_read_uint16_via_rsp to avoid triggering warnings in
- // memory diagnostic tools.
- return _malloc_read_uint16_via_rsp(prev_msize_ptr);
+ return TINY_PREVIOUS_MSIZE(ptr);
}
// don't read possibly unmapped memory before the beginning of the region
return 0;
@@ -184,160 +199,6 @@
}
static MALLOC_INLINE void
-zero_tiny_free_inline_meta(void *ptr, msize_t msize)
-{
- if (malloc_zero_policy == MALLOC_ZERO_ON_FREE) {
- *((tiny_free_list_t *)ptr) = (tiny_free_list_t){ 0 };
- if (msize > 1) {
- TINY_FREE_SIZE(ptr) = 0;
- void *follower = FOLLOWING_TINY_PTR(ptr, msize);
- TINY_PREVIOUS_MSIZE(follower) = 0;
- } else if (msize == 0) {
- TINY_FREE_SIZE(ptr) = 0;
- }
- }
-}
-
-static MALLOC_INLINE void
-zero_tiny_free_inline_meta_following(void *ptr, msize_t msize)
-{
- if (malloc_zero_policy == MALLOC_ZERO_ON_FREE) {
- if (msize > 1) {
- void *follower = FOLLOWING_TINY_PTR(ptr, msize);
- TINY_PREVIOUS_MSIZE(follower) = 0;
- }
- }
-}
-
-static MALLOC_COLD MALLOC_NOINLINE void
-tiny_zero_corruption_abort(void *ptr, msize_t msize)
-{
- uint8_t *bytes = ptr;
- size_t size = TINY_BYTES_FOR_MSIZE(msize);
- uint8_t *start = bytes, *end = bytes + size;
- // scan to the first non-NUL byte
- while (*bytes == '\0') {
- bytes++;
- }
-
- unsigned int offset = (unsigned int)(bytes - start);
- malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
- "Corruption detected in block %p of size %u at offset %u, "
- "first 32 bytes at that offset are "
- "%02X %02X %02X %02X %02X %02X %02X %02X | "
- "%02X %02X %02X %02X %02X %02X %02X %02X | "
- "%02X %02X %02X %02X %02X %02X %02X %02X | "
- "%02X %02X %02X %02X %02X %02X %02X %02X\n",
- ptr, (unsigned int)size, offset,
- (bytes + 0) < end ? *(bytes + 0) : 0,
- (bytes + 1) < end ? *(bytes + 1) : 0,
- (bytes + 2) < end ? *(bytes + 2) : 0,
- (bytes + 3) < end ? *(bytes + 3) : 0,
- (bytes + 4) < end ? *(bytes + 4) : 0,
- (bytes + 5) < end ? *(bytes + 5) : 0,
- (bytes + 6) < end ? *(bytes + 6) : 0,
- (bytes + 7) < end ? *(bytes + 7) : 0,
- (bytes + 8) < end ? *(bytes + 8) : 0,
- (bytes + 9) < end ? *(bytes + 9) : 0,
- (bytes + 10) < end ? *(bytes + 10) : 0,
- (bytes + 11) < end ? *(bytes + 11) : 0,
- (bytes + 12) < end ? *(bytes + 12) : 0,
- (bytes + 13) < end ? *(bytes + 13) : 0,
- (bytes + 14) < end ? *(bytes + 14) : 0,
- (bytes + 15) < end ? *(bytes + 15) : 0,
- (bytes + 16) < end ? *(bytes + 16) : 0,
- (bytes + 17) < end ? *(bytes + 17) : 0,
- (bytes + 18) < end ? *(bytes + 18) : 0,
- (bytes + 19) < end ? *(bytes + 19) : 0,
- (bytes + 20) < end ? *(bytes + 20) : 0,
- (bytes + 21) < end ? *(bytes + 21) : 0,
- (bytes + 22) < end ? *(bytes + 22) : 0,
- (bytes + 23) < end ? *(bytes + 23) : 0,
- (bytes + 24) < end ? *(bytes + 24) : 0,
- (bytes + 25) < end ? *(bytes + 25) : 0,
- (bytes + 26) < end ? *(bytes + 26) : 0,
- (bytes + 27) < end ? *(bytes + 27) : 0,
- (bytes + 28) < end ? *(bytes + 28) : 0,
- (bytes + 29) < end ? *(bytes + 29) : 0,
- (bytes + 30) < end ? *(bytes + 30) : 0,
- (bytes + 31) < end ? *(bytes + 31) : 0);
-}
-
-static MALLOC_INLINE void
-tiny_check_zero_or_clear(void *ptr, msize_t msize, boolean_t clear)
-{
- switch (malloc_zero_policy) {
- case MALLOC_ZERO_ON_FREE:
- if (zero_on_free_should_sample() &&
- _malloc_memcmp_zero_aligned8(ptr, TINY_BYTES_FOR_MSIZE(msize))) {
- tiny_zero_corruption_abort(ptr, msize);
- }
- break;
- case MALLOC_ZERO_NONE:
- if (!clear) {
- break;
- }
- MALLOC_FALLTHROUGH;
- case MALLOC_ZERO_ON_ALLOC:
- memset(ptr, '\0', TINY_BYTES_FOR_MSIZE(msize));
- break;
- }
-}
-
-// Check the inline metadata of a free block that hasn't already been verified:
-// - the previous freelist pointer
-// - agreement of the inline msizes
-//
-// Then clear all the inline metadata.
-static MALLOC_NOINLINE void
-_tiny_check_and_zero_inline_meta_from_freelist(rack_t *rack, void *ptr,
- msize_t msize)
-{
- tiny_free_list_t *free_ptr = ptr;
-
- // check the previous pointer
- (void)free_list_unchecksum_ptr(rack, &free_ptr->previous);
- // zero both pointers
- *free_ptr = (tiny_free_list_t){ 0 };
-
- // check agreement between msizes and zero
- if (msize > 1) {
- msize_t leading_free_size = TINY_FREE_SIZE(ptr);
- void *follower = FOLLOWING_TINY_PTR(ptr, msize);
- msize_t trailing_free_size = TINY_PREVIOUS_MSIZE(follower);
-
- if (leading_free_size != trailing_free_size) {
- malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
- "Corruption of free object %p: msizes %u/%u disagree\n",
- ptr, leading_free_size, trailing_free_size);
- } else if (leading_free_size != msize) {
- malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
- "Corruption at %p: unexpected msizes %u/%u\n",
- ptr, leading_free_size, msize);
- }
-
- TINY_FREE_SIZE(ptr) = 0;
- TINY_PREVIOUS_MSIZE(follower) = 0;
- } else if (msize == 0 && TINY_FREE_SIZE(ptr) != 0) {
- malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
- "Corruption at %p: unexpected nonzero msize %u\n", ptr,
- TINY_FREE_SIZE(ptr));
- }
-}
-
-static MALLOC_ALWAYS_INLINE MALLOC_INLINE void
-tiny_check_and_zero_inline_meta_from_freelist(rack_t *rack, void *ptr,
- msize_t msize)
-{
- if (malloc_zero_policy != MALLOC_ZERO_ON_FREE) {
- return;
- }
-
- _tiny_check_and_zero_inline_meta_from_freelist(rack, ptr, msize);
-}
-
-
-static MALLOC_INLINE void
set_tiny_meta_header_free(const void *ptr, msize_t msize)
{
// !msize is acceptable and means 65536
@@ -416,7 +277,7 @@
}
previous_index = index - previous_msize;
- previous_ptr = TINY_PTR_FOR_INDEX(previous_index, TINY_REGION_FOR_PTR(ptr));
+ previous_ptr = (void *)((uintptr_t)TINY_REGION_FOR_PTR(ptr) + TINY_BYTES_FOR_MSIZE(previous_index));
if (!BITARRAY_BIT(block_header, previous_index)) {
return NULL;
}
@@ -705,7 +566,7 @@
//
if (tiny_mag_ptr->mag_bytes_free_at_end) {
- last_block = (void *)((uintptr_t)TINY_REGION_HEAP_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
+ last_block = (void *)((uintptr_t)TINY_REGION_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
last_msize = TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_end);
last_header = TINY_BLOCK_HEADER_FOR_PTR(last_block);
last_index = TINY_INDEX_FOR_PTR(last_block);
@@ -728,9 +589,6 @@
if (previous_block) {
set_tiny_meta_header_middle(last_block);
tiny_free_list_remove_ptr(rack, tiny_mag_ptr, previous_block, previous_msize);
- // zero out the trailing inline msize of the previous block to
- // connect its zero prefix to the last block
- zero_tiny_free_inline_meta_following(previous_block, previous_msize);
last_block = previous_block;
last_msize += previous_msize;
}
@@ -743,7 +601,7 @@
#if CONFIG_ASLR_INTERNAL
// Coalesce the big free block at start with any following free blocks
if (tiny_mag_ptr->mag_bytes_free_at_start) {
- last_block = TINY_REGION_HEAP_BASE(tiny_mag_ptr->mag_last_region);
+ last_block = TINY_REGION_ADDRESS(tiny_mag_ptr->mag_last_region);
last_msize = TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_start);
void *next_block = (void *)((uintptr_t)last_block + tiny_mag_ptr->mag_bytes_free_at_start);
@@ -756,9 +614,6 @@
msize_t next_msize = get_tiny_free_size(next_block);
set_tiny_meta_header_middle(next_block);
tiny_free_list_remove_ptr(rack, tiny_mag_ptr, next_block, next_msize);
- // zero inline metadata of next_block to continue the zero prefix of
- // the big starting free block
- zero_tiny_free_inline_meta(next_block, next_msize);
last_msize += next_msize;
}
@@ -774,9 +629,9 @@
int
tiny_free_detach_region(rack_t *rack, magazine_t *tiny_mag_ptr, region_t r)
{
- uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(r);
+ uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(r);
uintptr_t current = start;
- uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(r);
+ uintptr_t limit = (uintptr_t)TINY_REGION_END(r);
boolean_t is_free;
msize_t msize;
region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(r);
@@ -804,9 +659,9 @@
size_t
tiny_free_reattach_region(rack_t *rack, magazine_t *tiny_mag_ptr, region_t r)
{
- uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(r);
+ uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(r);
uintptr_t current = start;
- uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(r);
+ uintptr_t limit = (uintptr_t)TINY_REGION_END(r);
boolean_t is_free;
msize_t msize;
size_t bytes_used = REGION_TRAILER_FOR_TINY_REGION(r)->bytes_used;
@@ -838,12 +693,12 @@
void
tiny_free_scan_madvise_free(rack_t *rack, magazine_t *depot_ptr, region_t r)
{
- uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(r);
+ uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(r);
uintptr_t current = start;
- uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(r);
+ uintptr_t limit = (uintptr_t)TINY_REGION_END(r);
boolean_t is_free;
msize_t msize;
- tiny_pg_pair_t advisory[((TINY_HEAP_SIZE + vm_kernel_page_size - 1) >> vm_kernel_page_shift) >>
+ tiny_pg_pair_t advisory[((TINY_REGION_PAYLOAD_BYTES + vm_kernel_page_size - 1) >> vm_kernel_page_shift) >>
1]; // 256bytes stack allocated
int advisories = 0;
@@ -878,7 +733,7 @@
uintptr_t pgHi = trunc_page_kernel(current + TINY_BYTES_FOR_MSIZE(msize) - sizeof(msize_t));
if (pgLo < pgHi) {
- advisory[advisories].pnum = (pgLo - (uintptr_t)r) >> vm_kernel_page_shift;
+ advisory[advisories].pnum = (pgLo - start) >> vm_kernel_page_shift;
advisory[advisories].size = (pgHi - pgLo) >> vm_kernel_page_shift;
advisories++;
}
@@ -899,7 +754,7 @@
OSAtomicIncrement32Barrier(&(REGION_TRAILER_FOR_TINY_REGION(r)->pinned_to_depot));
SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
for (i = 0; i < advisories; ++i) {
- uintptr_t addr = (advisory[i].pnum << vm_kernel_page_shift) + (uintptr_t)r;
+ uintptr_t addr = (advisory[i].pnum << vm_kernel_page_shift) + start;
size_t size = advisory[i].size << vm_kernel_page_shift;
mvm_madvise_free(rack, r, addr, addr + size, NULL, rack->debug_flags & MALLOC_DO_SCRIBBLE);
@@ -981,30 +836,18 @@
for (mag_index = 0; mag_index < rack->num_magazines; mag_index++) {
size_t index;
for (index = 0; index < rack->region_generation->num_regions_allocated; ++index) {
- rack_region_lock(rack);
+ SZONE_LOCK(TINY_SZONE_FROM_RACK(rack));
region_t tiny = rack->region_generation->hashed_regions[index];
if (!tiny || tiny == HASHRING_REGION_DEALLOCATED) {
- rack_region_unlock(rack);
+ SZONE_UNLOCK(TINY_SZONE_FROM_RACK(rack));
continue;
}
- region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(tiny);
- // Make sure that the owning magazine doesn't try and take this out
- // from under our feet.
- trailer->dispose_flags |= RACK_DISPOSE_DELAY;
- rack_region_unlock(rack);
-
magazine_t *mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
- trailer, MAGAZINE_INDEX_FOR_TINY_REGION(tiny));
-
- // If acquiring the region lock was enough to prevent the owning
- // magazine from deallocating the region, free it now so we don't
- // do wasted work.
- if (rack_region_maybe_dispose(rack, tiny, TINY_REGION_SIZE, trailer)) {
- SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
- continue;
- }
+ REGION_TRAILER_FOR_TINY_REGION(tiny),
+ MAGAZINE_INDEX_FOR_TINY_REGION(tiny));
+ SZONE_UNLOCK(TINY_SZONE_FROM_RACK(rack));
/* Ordering is important here, the magazine of a region may potentially change
* during mag_lock_zine_for_region_trailer, so src_mag_index must be taken
@@ -1018,11 +861,6 @@
continue;
}
- if (REGION_TRAILER_FOR_TINY_REGION(tiny)->pinned_to_depot > 0) {
- SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
- continue;
- }
-
if (tiny == mag_ptr->mag_last_region && (mag_ptr->mag_bytes_free_at_end || mag_ptr->mag_bytes_free_at_start)) {
tiny_finalize_region(rack, mag_ptr);
}
@@ -1037,20 +875,20 @@
SZONE_MAGAZINE_PTR_LOCK(tiny_depot_ptr);
MAGAZINE_INDEX_FOR_TINY_REGION(tiny) = DEPOT_MAGAZINE_INDEX;
- MALLOC_ASSERT(REGION_TRAILER_FOR_TINY_REGION(tiny)->pinned_to_depot == 0);
+ REGION_TRAILER_FOR_TINY_REGION(tiny)->pinned_to_depot = 0;
size_t bytes_inplay = tiny_free_reattach_region(rack, tiny_depot_ptr, tiny);
/* Fix up the metadata of the target magazine while the region is in the depot. */
mag_ptr->mag_num_bytes_in_objects -= bytes_inplay;
- mag_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
+ mag_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
mag_ptr->mag_num_objects -= objects_in_use;
/* Now we can drop the magazine lock of the source mag. */
SZONE_MAGAZINE_PTR_UNLOCK(mag_ptr);
tiny_depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
- tiny_depot_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
+ tiny_depot_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
tiny_depot_ptr->mag_num_objects -= objects_in_use;
recirc_list_splice_last(rack, tiny_depot_ptr, REGION_TRAILER_FOR_TINY_REGION(tiny));
@@ -1126,6 +964,11 @@
{
magazine_t *depot_ptr = &(rack->magazines[DEPOT_MAGAZINE_INDEX]);
+ /* FIXME: Would Uniprocessor benefit from recirc and MADV_FREE? */
+ if (rack->num_magazines == 1) { // Uniprocessor, single magazine, so no recirculation necessary
+ return 0;
+ }
+
#if DEBUG_MALLOC
if (DEPOT_MAGAZINE_INDEX == mag_index) {
malloc_zone_error(rack->debug_flags, true, "tiny_get_region_from_depot called for magazine index -1\n");
@@ -1138,32 +981,22 @@
// Appropriate a Depot'd region that can satisfy requested msize.
region_trailer_t *node;
region_t sparse_region;
- msize_t try_msize = msize;
while (1) {
- sparse_region = tiny_find_msize_region(rack, depot_ptr, DEPOT_MAGAZINE_INDEX, try_msize);
+ sparse_region = tiny_find_msize_region(rack, depot_ptr, DEPOT_MAGAZINE_INDEX, msize);
if (NULL == sparse_region) { // Depot empty?
SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
return 0;
}
node = REGION_TRAILER_FOR_TINY_REGION(sparse_region);
- if (0 == node->pinned_to_depot) {
- // Found one!
+ if (0 >= node->pinned_to_depot) {
break;
}
- // Try the next msize up - maybe the head of its free list will be in
- // a region we can use. Once we get the region we'll still allocate the
- // original msize.
- try_msize++;
-
- if (try_msize > NUM_TINY_SLOTS) {
- // Tried all the msizes but couldn't get a usable region. Let's
- // give up for now and we'll allocate a new region from the kernel.
- SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
- return 0;
- }
+ SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
+ yield();
+ SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
}
// disconnect node from Depot
@@ -1174,17 +1007,17 @@
// Transfer ownership of the region
MAGAZINE_INDEX_FOR_TINY_REGION(sparse_region) = mag_index;
- MALLOC_ASSERT(node->pinned_to_depot == 0);
+ node->pinned_to_depot = 0;
// Iterate the region putting its free entries on its new (locked) magazine's free list
size_t bytes_inplay = tiny_free_reattach_region(rack, tiny_mag_ptr, sparse_region);
depot_ptr->mag_num_bytes_in_objects -= bytes_inplay;
- depot_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
+ depot_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
depot_ptr->mag_num_objects -= objects_in_use;
tiny_mag_ptr->mag_num_bytes_in_objects += bytes_inplay;
- tiny_mag_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
+ tiny_mag_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
tiny_mag_ptr->mag_num_objects += objects_in_use;
// connect to magazine as last node
@@ -1216,19 +1049,32 @@
int objects_in_use = tiny_free_detach_region(rack, depot_ptr, sparse_region);
if (0 == objects_in_use) {
- if (!rack_region_remove(rack, sparse_region, node)) {
+ // Invalidate the hash table entry for this region with HASHRING_REGION_DEALLOCATED.
+ // Using HASHRING_REGION_DEALLOCATED preserves the collision chain, using HASHRING_OPEN_ENTRY (0) would not.
+ rgnhdl_t pSlot = hash_lookup_region_no_lock(rack->region_generation->hashed_regions,
+ rack->region_generation->num_regions_allocated,
+ rack->region_generation->num_regions_allocated_shift,
+ sparse_region);
+
+ if (NULL == pSlot) {
+ malloc_zone_error(rack->debug_flags, true, "tiny_free_try_depot_unmap_no_lock hash lookup failed: %p\n", sparse_region);
return NULL;
}
- depot_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
+ *pSlot = HASHRING_REGION_DEALLOCATED;
+ depot_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
+
+ // Atomically increment num_regions_dealloc
+#ifdef __LP64___
+ OSAtomicIncrement64(&rack->num_regions_dealloc);
+#else
+ OSAtomicIncrement32((int32_t *)&rack->num_regions_dealloc);
+#endif
// Caller will transfer ownership of the region back to the OS with no locks held
- MAGMALLOC_DEALLOCREGION(TINY_SZONE_FROM_RACK(rack),
- (void *)sparse_region, TINY_REGION_SIZE); // DTrace USDT Probe
+ MAGMALLOC_DEALLOCREGION(TINY_SZONE_FROM_RACK(rack), (void *)sparse_region, TINY_REGION_SIZE); // DTrace USDT Probe
return sparse_region;
} else {
- malloc_zone_error(rack->debug_flags, true,
- "tiny_free_try_depot_unmap_no_lock objects_in_use not zero: %d\n",
- objects_in_use);
+ malloc_zone_error(rack->debug_flags, true, "tiny_free_try_depot_unmap_no_lock objects_in_use not zero: %d\n", objects_in_use);
return NULL;
}
}
@@ -1286,13 +1132,13 @@
size_t bytes_inplay = tiny_free_reattach_region(rack, depot_ptr, sparse_region);
tiny_mag_ptr->mag_num_bytes_in_objects -= bytes_inplay;
- tiny_mag_ptr->num_bytes_in_magazine -= TINY_HEAP_SIZE;
+ tiny_mag_ptr->num_bytes_in_magazine -= TINY_REGION_PAYLOAD_BYTES;
tiny_mag_ptr->mag_num_objects -= objects_in_use;
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr); // Unlock the originating magazine
depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
- depot_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
+ depot_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
depot_ptr->mag_num_objects += objects_in_use;
// connect to Depot as last node
@@ -1301,20 +1147,16 @@
MAGMALLOC_RECIRCREGION(TINY_SZONE_FROM_RACK(rack), (int)mag_index, (void *)sparse_region, TINY_REGION_SIZE,
(int)BYTES_USED_FOR_TINY_REGION(sparse_region)); // DTrace USDT Probe
-#if CONFIG_AGGRESSIVE_MADVISE
- if (!aggressive_madvise_enabled)
-#endif
- {
- // Mark free'd dirty pages with MADV_FREE to reduce memory pressure
- tiny_free_scan_madvise_free(rack, depot_ptr, sparse_region);
- }
+#if !CONFIG_AGGRESSIVE_MADVISE
+ // Mark free'd dirty pages with MADV_FREE to reduce memory pressure
+ tiny_free_scan_madvise_free(rack, depot_ptr, sparse_region);
+#endif
// If the region is entirely empty vm_deallocate() it outside the depot lock
region_t r_dealloc = tiny_free_try_depot_unmap_no_lock(rack, depot_ptr, node);
SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
if (r_dealloc) {
- mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE,
- MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags));
+ mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE, 0);
}
return FALSE; // Caller need not unlock the originating magazine
}
@@ -1332,7 +1174,11 @@
region_trailer_t *node = REGION_TRAILER_FOR_TINY_REGION(region);
size_t bytes_used = node->bytes_used;
- if (DEPOT_MAGAZINE_INDEX != mag_index) {
+ /* FIXME: Would Uniprocessor benefit from recirc and MADV_FREE? */
+ if (rack->num_magazines == 1) { // Uniprocessor, single magazine, so no recirculation necessary
+ /* NOTHING */
+ return TRUE; // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr)
+ } else if (DEPOT_MAGAZINE_INDEX != mag_index) {
// Emptiness discriminant
if (tiny_region_below_recirc_threshold(region)) {
/* Region has crossed threshold from density to sparsity. Mark it "suitable" on the
@@ -1350,14 +1196,11 @@
return tiny_free_do_recirc_to_depot(rack, tiny_mag_ptr, mag_index);
}
} else {
-#if CONFIG_AGGRESSIVE_MADVISE
- if (!aggressive_madvise_enabled)
-#endif
- {
- // We are free'ing into the depot, so madvise as we do so unless we were madvising every incoming
- // allocation anyway.
- tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, headptr, headsize, ptr, msize);
- }
+#if !CONFIG_AGGRESSIVE_MADVISE
+ // We are free'ing into the depot, so madvise as we do so unless we were madvising every incoming
+ // allocation anyway.
+ tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, headptr, headsize, ptr, msize);
+#endif
if (0 < bytes_used || 0 < node->pinned_to_depot) {
/* Depot'd region is still live. Leave it in place on the Depot's recirculation list
@@ -1368,8 +1211,7 @@
region_t r_dealloc = tiny_free_try_depot_unmap_no_lock(rack, tiny_mag_ptr, node);
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
if (r_dealloc) {
- mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE,
- MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags));
+ mvm_deallocate_pages(r_dealloc, TINY_REGION_SIZE, 0);
}
return FALSE; // Caller need not unlock
}
@@ -1378,12 +1220,8 @@
}
#endif // CONFIG_RECIRC_DEPOT
-#define TINY_FREE_FLAG_PARTIAL 0x1
-#define TINY_FREE_FLAG_FROM_CACHE 0x2
-
boolean_t
-tiny_free_no_lock(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index,
- region_t region, void *ptr, msize_t msize, uint32_t flags)
+tiny_free_no_lock(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index, region_t region, void *ptr, msize_t msize, boolean_t partial_free)
{
void *original_ptr = ptr;
size_t original_size = TINY_BYTES_FOR_MSIZE(msize);
@@ -1406,7 +1244,8 @@
#endif
// Check that the region cookie is intact.
- region_check_cookie(region, ®ION_COOKIE_FOR_TINY_REGION(region));
+ region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(region);
+ region_check_cookie(region, trailer);
// We try to coalesce this block with the preceeding one
previous = tiny_previous_preceding_free(ptr, &previous_msize);
@@ -1420,17 +1259,11 @@
// clear the meta_header since this is no longer the start of a block
set_tiny_meta_header_middle(ptr);
tiny_free_list_remove_ptr(rack, tiny_mag_ptr, previous, previous_msize);
-
- // zero out the trailing inline msize of the block to connect the zero
- // prefix of this block to the newly free block
- zero_tiny_free_inline_meta_following(previous, previous_msize);
-
ptr = previous;
msize += previous_msize;
}
-
// We try to coalesce with the next block
- if ((next_block < TINY_REGION_HEAP_END(region)) && tiny_meta_header_is_free(next_block)) {
+ if ((next_block < TINY_REGION_END(region)) && tiny_meta_header_is_free(next_block)) {
next_msize = get_tiny_free_size(next_block);
#if DEBUG_MALLOC
if (LOG(szone, ptr) || LOG(szone, next_block)) {
@@ -1465,12 +1298,6 @@
// clear the meta_header to enable coalescing backwards
set_tiny_meta_header_middle(big_free_block);
-
- // zero out inline metadata to continue the zero prefix of the
- // previous block - must happen before set_tiny_meta_header_free()
- // reinitializes the new inline metadata
- zero_tiny_free_inline_meta(big_free_block, next_msize);
-
set_tiny_meta_header_free(ptr, msize);
uint16_t next_block_index = TINY_INDEX_FOR_PTR(big_free_block) + 1;
@@ -1487,21 +1314,13 @@
}
tiny_free_list_remove_ptr(rack, tiny_mag_ptr, next_block, next_msize);
set_tiny_meta_header_middle(next_block); // clear the meta_header to enable coalescing backwards
- // zero out inline metadata to continue the zero prefix of the previous
- // block
- zero_tiny_free_inline_meta(next_block, next_msize);
msize += next_msize;
}
- if (malloc_zero_policy != MALLOC_ZERO_ON_FREE) {
- // The tiny cache already scribbles free blocks as they go through the
- // cache, so we do not need to do it here.
- //
- // XXX This should probably also be conditional on CONFIG_TINY_CACHE
- if ((rack->debug_flags & MALLOC_DO_SCRIBBLE) &&
- !(flags & TINY_FREE_FLAG_FROM_CACHE)) {
- memset(ptr, SCRABBLE_BYTE, TINY_BYTES_FOR_MSIZE(msize));
- }
+ // The tiny cache already scribbles free blocks as they go through the
+ // cache whenever msize < TINY_QUANTUM , so we do not need to do it here.
+ if ((rack->debug_flags & MALLOC_DO_SCRIBBLE) && msize && (msize >= TINY_QUANTUM)) {
+ memset(ptr, SCRABBLE_BYTE, TINY_BYTES_FOR_MSIZE(msize));
}
tiny_free_list_add_ptr(rack, tiny_mag_ptr, ptr, msize);
@@ -1511,7 +1330,6 @@
tiny_mag_ptr->mag_num_bytes_in_objects -= original_size;
// Update this region's bytes in use count
- region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(region);
size_t bytes_used = trailer->bytes_used - original_size;
trailer->bytes_used = (unsigned int)bytes_used;
@@ -1519,16 +1337,14 @@
// posix_memalign and then free some range of bytes at the start and/or
// the end. In that case, we aren't changing the number of allocated objects.
// Similarly for realloc() in the case where we shrink in place.
- if (!(flags & TINY_FREE_FLAG_PARTIAL)) {
+ if (!partial_free) {
trailer->objects_in_use--;
tiny_mag_ptr->mag_num_objects--;
}
#if CONFIG_AGGRESSIVE_MADVISE
- if (aggressive_madvise_enabled) {
- // Platforms that want to madvise every freed allocation do so here, even if we continue
- // on to use the recirc depot after.
- tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, original_ptr, original_size, ptr, msize);
- }
+ // Platforms that want to madvise every freed allocation do so here, even if we continue
+ // on to use the recirc depot after.
+ tiny_madvise_free_range_no_lock(rack, tiny_mag_ptr, region, original_ptr, original_size, ptr, msize);
#endif
// Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr) if this function
@@ -1556,28 +1372,26 @@
tiny_finalize_region(rack, tiny_mag_ptr);
}
- tiny_region_t region = (tiny_region_t)aligned_address;
-
// We set the unused bits of the header in the last pair to be all ones, and those of the inuse to zeroes.
#if NUM_TINY_BLOCKS & 31
const uint32_t header = 0xFFFFFFFFU << (NUM_TINY_BLOCKS & 31);
#else
const uint32_t header = 0;
#endif
- region->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].header = header;
- region->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].inuse = 0;
+ ((tiny_region_t)aligned_address)->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].header = header;
+ ((tiny_region_t)aligned_address)->pairs[CEIL_NUM_TINY_BLOCKS_WORDS - 1].inuse = 0;
// Tag the region at "aligned_address" as belonging to us,
// and so put it under the protection of the magazine lock we are holding.
// Do this before advertising "aligned_address" on the hash ring(!)
- MAGAZINE_INDEX_FOR_TINY_REGION(region) = mag_index;
+ MAGAZINE_INDEX_FOR_TINY_REGION(aligned_address) = mag_index;
// Insert the new region into the hash ring
- rack_region_insert(rack, region);
-
- tiny_mag_ptr->mag_last_region = region;
- BYTES_USED_FOR_TINY_REGION(region) = TINY_BYTES_FOR_MSIZE(msize);
- OBJECTS_IN_USE_FOR_TINY_REGION(region) = 1;
+ rack_region_insert(rack, (region_t)aligned_address);
+
+ tiny_mag_ptr->mag_last_region = aligned_address;
+ BYTES_USED_FOR_TINY_REGION(aligned_address) = TINY_BYTES_FOR_MSIZE(msize);
+ OBJECTS_IN_USE_FOR_TINY_REGION(aligned_address) = 1;
#if CONFIG_ASLR_INTERNAL
int offset_msize = malloc_entropy[0] & TINY_ENTROPY_MASK;
@@ -1586,17 +1400,17 @@
offset_msize = strtol(getenv("MallocASLRForce"), NULL, 0) & TINY_ENTROPY_MASK;
}
if (getenv("MallocASLRPrint")) {
- malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", region, offset_msize);
+ malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", aligned_address, offset_msize);
}
#endif
#else
int offset_msize = 0;
#endif
- ptr = (void *)(TINY_REGION_HEAP_BASE(region) + TINY_BYTES_FOR_MSIZE(offset_msize));
+ ptr = (void *)((uintptr_t)aligned_address + TINY_BYTES_FOR_MSIZE(offset_msize));
set_tiny_meta_header_in_use(ptr, msize);
tiny_mag_ptr->mag_num_objects++;
tiny_mag_ptr->mag_num_bytes_in_objects += TINY_BYTES_FOR_MSIZE(msize);
- tiny_mag_ptr->num_bytes_in_magazine += TINY_HEAP_SIZE;
+ tiny_mag_ptr->num_bytes_in_magazine += TINY_REGION_PAYLOAD_BYTES;
// We put a header on the last block so that it appears in use (for coalescing, etc...)
set_tiny_meta_header_in_use_1((void *)((uintptr_t)ptr + TINY_BYTES_FOR_MSIZE(msize)));
@@ -1613,7 +1427,7 @@
#endif
// connect to magazine as last node
- recirc_list_splice_last(rack, tiny_mag_ptr, REGION_TRAILER_FOR_TINY_REGION(region));
+ recirc_list_splice_last(rack, tiny_mag_ptr, REGION_TRAILER_FOR_TINY_REGION(aligned_address));
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
@@ -1684,8 +1498,7 @@
tiny_claimed_address(rack_t *rack, void *ptr)
{
region_t r = tiny_region_for_ptr_no_lock(rack, ptr);
- return r && ptr >= TINY_REGION_HEAP_BASE(r)
- && ptr < TINY_REGION_HEAP_END(r);
+ return r && ptr < TINY_REGION_END(r);
}
void *
@@ -1758,8 +1571,7 @@
// The block in mag_last_free is still marked as header and in-use, so copy that
// state to the block that remains. The state for the block that we're going to
// use is adjusted by the set_tiny_meta_header_middle() call below.
- void *leftover_ptr = ((char *)next_block) + TINY_BYTES_FOR_MSIZE(coalesced_msize);
- set_tiny_meta_header_in_use(leftover_ptr, leftover_msize);
+ set_tiny_meta_header_in_use(next_block + TINY_BYTES_FOR_MSIZE(coalesced_msize), leftover_msize);
} else {
// Using the whole block.
tiny_mag_ptr->mag_last_free = NULL;
@@ -1768,7 +1580,6 @@
trailer->objects_in_use--;
}
set_tiny_meta_header_middle(next_block);
- tiny_check_zero_or_clear(last_free_ptr, coalesced_msize, false);
coalesced_msize = 0; // No net change in memory use
} else {
#endif // CONFIG_TINY_CACHE
@@ -1776,7 +1587,7 @@
* Try to expand into unused space immediately after this block.
*/
msize_t unused_msize = TINY_MSIZE_FOR_BYTES(tiny_mag_ptr->mag_bytes_free_at_end);
- void *unused_start = TINY_REGION_HEAP_END(TINY_REGION_FOR_PTR(ptr)) - tiny_mag_ptr->mag_bytes_free_at_end;
+ void *unused_start = TINY_REGION_END(TINY_REGION_FOR_PTR(ptr)) - tiny_mag_ptr->mag_bytes_free_at_end;
if (tiny_mag_ptr->mag_last_region == TINY_REGION_FOR_PTR(ptr)
&& coalesced_msize < unused_msize && unused_start == ptr + old_size) {
// The block at the start of mag_bytes_free_at_end is marked as
@@ -1793,7 +1604,6 @@
// Mark the first block of the remaining free area as a header and in-use.
set_tiny_meta_header_in_use_1(ptr + TINY_BYTES_FOR_MSIZE(new_msize));
}
- tiny_check_zero_or_clear(unused_start, coalesced_msize, false);
} else {
/*
* Look for a free block immediately afterwards. If it's large
@@ -1815,10 +1625,6 @@
*/
tiny_free_list_remove_ptr(rack, tiny_mag_ptr, next_block, next_msize);
set_tiny_meta_header_middle(next_block); // clear the meta_header to enable coalescing backwards
-
- tiny_check_and_zero_inline_meta_from_freelist(rack, next_block, next_msize);
- tiny_check_zero_or_clear(next_block, coalesced_msize, false);
-
leftover_msize = next_msize - coalesced_msize;
if (leftover_msize) {
/* there's some left, so put the remainder back */
@@ -1843,7 +1649,7 @@
node->bytes_used = (unsigned int)bytes_used;
// Emptiness discriminant
- if (bytes_used < DENSITY_THRESHOLD(TINY_HEAP_SIZE)) {
+ if (bytes_used < DENSITY_THRESHOLD(TINY_REGION_PAYLOAD_BYTES)) {
/* After this reallocation the region is still sparse, so it must have been even more so before
* the reallocation. That implies the region is already correctly marked. Do nothing. */
} else {
@@ -1889,7 +1695,7 @@
}
/* establish region limits */
- start = (uintptr_t)TINY_REGION_HEAP_BASE(region);
+ start = (uintptr_t)TINY_REGION_ADDRESS(region);
ptr = start;
if (region == tiny_mag_ptr->mag_last_region) {
ptr += tiny_mag_ptr->mag_bytes_free_at_start;
@@ -1906,7 +1712,7 @@
}
}
}
- region_end = (uintptr_t)TINY_REGION_HEAP_END(region);
+ region_end = (uintptr_t)TINY_REGION_END(region);
/*
* The last region may have a trailing chunk which has not been converted into inuse/freelist
@@ -1980,9 +1786,9 @@
*/
follower = FOLLOWING_TINY_PTR(ptr, msize);
if (((uintptr_t)follower != region_end) && (get_tiny_previous_free_msize(follower) != msize)) {
- TINY_CHECK_FAIL("*** invariant broken for tiny free %p followed by %p in region %p [%p-%p] "
+ TINY_CHECK_FAIL("*** invariant broken for tiny free %p followed by %p in region [%p-%p] "
"(end marker incorrect) should be %d; in fact %d\n",
- (void *)ptr, follower, region, TINY_REGION_HEAP_BASE(region), (void *)region_end,
+ (void *)ptr, follower, TINY_REGION_ADDRESS(region), (void *)region_end,
msize, get_tiny_previous_free_msize(follower));
return 0;
}
@@ -2065,20 +1871,20 @@
for (index = 0; index < num_regions; ++index) {
region = regions[index];
if (HASHRING_OPEN_ENTRY != region && HASHRING_REGION_DEALLOCATED != region) {
- range.address = (vm_address_t)TINY_REGION_HEAP_BASE(region);
- range.size = (vm_size_t)TINY_HEAP_SIZE;
+ range.address = (vm_address_t)TINY_REGION_ADDRESS(region);
+ range.size = (vm_size_t)TINY_REGION_SIZE;
if (type_mask & MALLOC_ADMIN_REGION_RANGE_TYPE) {
- admin_range.address = TINY_REGION_METADATA(region);
+ admin_range.address = range.address + TINY_METADATA_START;
admin_range.size = TINY_METADATA_SIZE;
recorder(task, context, MALLOC_ADMIN_REGION_RANGE_TYPE, &admin_range, 1);
}
if (type_mask & (MALLOC_PTR_REGION_RANGE_TYPE | MALLOC_ADMIN_REGION_RANGE_TYPE)) {
ptr_range.address = range.address;
- ptr_range.size = TINY_HEAP_SIZE;
+ ptr_range.size = NUM_TINY_BLOCKS * TINY_QUANTUM;
recorder(task, context, MALLOC_PTR_REGION_RANGE_TYPE, &ptr_range, 1);
}
if (type_mask & MALLOC_PTR_IN_USE_RANGE_TYPE) {
- err = reader(task, (vm_address_t)region, (vm_size_t)TINY_REGION_SIZE, (void **)&mapped_region);
+ err = reader(task, range.address, range.size, (void **)&mapped_region);
if (err) {
return err;
}
@@ -2091,13 +1897,13 @@
// Each magazine could have a pointer to a cached free block from
// this region. Count the regions that have such a pointer.
for (mag_index = 0; mag_index < szone->tiny_rack.num_magazines; mag_index++) {
- if (region == (tiny_mag_base + mag_index)->mag_last_free_rgn) {
+ if ((void *)range.address == (tiny_mag_base + mag_index)->mag_last_free_rgn) {
cached_free_blocks++;
}
}
#endif // CONFIG_TINY_CACHE
- block_header = TINY_BLOCK_HEADER_FOR_REGION(mapped_region);
+ block_header = (uint32_t *)(mapped_region + TINY_METADATA_START + sizeof(region_trailer_t));
in_use = TINY_INUSE_FOR_HEADER(block_header);
block_index = 0;
block_limit = NUM_TINY_BLOCKS;
@@ -2110,7 +1916,7 @@
vm_size_t block_offset = TINY_BYTES_FOR_MSIZE(block_index);
is_free = !BITARRAY_BIT(in_use, block_index);
if (is_free) {
- mapped_ptr = TINY_REGION_HEAP_BASE(mapped_region) + block_offset;
+ mapped_ptr = mapped_region + block_offset;
// mapped_region, the address at which 'range' in 'task' has been
// mapped into our process, is not necessarily aligned to
@@ -2131,11 +1937,11 @@
// If there are still magazines that have cached free
// blocks in this region, check whether this is one of
// them and don't return the block pointer if it is.
- void *ptr = TINY_REGION_HEAP_BASE(region) + block_offset;
+ vm_address_t ptr = range.address + block_offset;
boolean_t block_cached = false;
if (cached_free_blocks) {
for (mag_index = 0; mag_index < szone->tiny_rack.num_magazines; mag_index++) {
- if (ptr == (tiny_mag_base + mag_index)->mag_last_free) {
+ if ((void *)ptr == (tiny_mag_base + mag_index)->mag_last_free) {
block_cached = true;
cached_free_blocks--;
msize = (tiny_mag_base + mag_index)->mag_last_free_msize;
@@ -2156,7 +1962,7 @@
bit++;
msize++;
}
- buffer[count].address = (vm_address_t)TINY_REGION_HEAP_BASE(region) + block_offset;
+ buffer[count].address = range.address + block_offset;
buffer[count].size = TINY_BYTES_FOR_MSIZE(msize);
count++;
if (count >= MAX_RECORDER_BUFFER) {
@@ -2218,7 +2024,6 @@
}
#endif
tiny_update_region_free_list_for_remove(slot, ptr, next);
- tiny_check_and_zero_inline_meta_from_freelist(rack, ptr, msize);
goto return_tiny_alloc;
}
@@ -2250,14 +2055,7 @@
BITMAPV_CLR(tiny_mag_ptr->mag_bitmap, slot);
}
this_msize = get_tiny_free_size(ptr);
- if (os_unlikely(this_msize < msize)) {
- malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
- "Corruption of tiny freelist %p: size too small (%u/%u)\n",
- ptr, this_msize, msize);
-
- }
tiny_update_region_free_list_for_remove(slot, ptr, next);
- tiny_check_and_zero_inline_meta_from_freelist(rack, ptr, this_msize);
goto add_leftover_and_proceed;
}
#if DEBUG_MALLOC
@@ -2272,27 +2070,18 @@
ptr = limit->p;
if (ptr) {
this_msize = get_tiny_free_size(ptr);
- if (os_unlikely(this_msize < msize)) {
- malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
- "Corruption of tiny freelist %p: size too small (%u/%u)\n",
- ptr, this_msize, msize);
- }
next = free_list_unchecksum_ptr(rack, &ptr->next);
if (this_msize - msize > NUM_TINY_SLOTS) {
// the leftover will go back to the free list, so we optimize by
// modifying the free list rather than a pop and push of the head
leftover_msize = this_msize - msize;
leftover_ptr = (tiny_free_list_t *)((unsigned char *)ptr + TINY_BYTES_FOR_MSIZE(msize));
-
- tiny_free_list_t tmp_ptr = *ptr;
- tiny_check_and_zero_inline_meta_from_freelist(rack, ptr, this_msize);
-
limit->p = leftover_ptr;
if (next) {
next->previous.u = free_list_checksum_ptr(rack, leftover_ptr);
}
- leftover_ptr->previous = tmp_ptr.previous;
- leftover_ptr->next = tmp_ptr.next;
+ leftover_ptr->previous = ptr->previous;
+ leftover_ptr->next = ptr->next;
set_tiny_meta_header_free(leftover_ptr, leftover_msize);
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
@@ -2310,7 +2099,6 @@
}
limit->p = next;
tiny_update_region_free_list_for_remove(slot, ptr, next);
- tiny_check_and_zero_inline_meta_from_freelist(rack, ptr, this_msize);
goto add_leftover_and_proceed;
/* NOTREACHED */
}
@@ -2318,7 +2106,7 @@
try_tiny_malloc_from_end:
// Let's see if we can use tiny_mag_ptr->mag_bytes_free_at_end
if (tiny_mag_ptr->mag_bytes_free_at_end >= TINY_BYTES_FOR_MSIZE(msize)) {
- ptr = (tiny_free_list_t *)((uintptr_t)TINY_REGION_HEAP_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
+ ptr = (tiny_free_list_t *)((uintptr_t)TINY_REGION_END(tiny_mag_ptr->mag_last_region) - tiny_mag_ptr->mag_bytes_free_at_end);
tiny_mag_ptr->mag_bytes_free_at_end -= TINY_BYTES_FOR_MSIZE(msize);
if (tiny_mag_ptr->mag_bytes_free_at_end) {
// let's add an in use block after ptr to serve as boundary
@@ -2335,7 +2123,7 @@
#if CONFIG_ASLR_INTERNAL
// Try from start if nothing left at end
if (tiny_mag_ptr->mag_bytes_free_at_start >= TINY_BYTES_FOR_MSIZE(msize)) {
- ptr = (tiny_free_list_t *)(TINY_REGION_HEAP_BASE(tiny_mag_ptr->mag_last_region) + tiny_mag_ptr->mag_bytes_free_at_start -
+ ptr = (tiny_free_list_t *)(TINY_REGION_ADDRESS(tiny_mag_ptr->mag_last_region) + tiny_mag_ptr->mag_bytes_free_at_start -
TINY_BYTES_FOR_MSIZE(msize));
tiny_mag_ptr->mag_bytes_free_at_start -= TINY_BYTES_FOR_MSIZE(msize);
if (tiny_mag_ptr->mag_bytes_free_at_start) {
@@ -2355,8 +2143,6 @@
add_leftover_and_proceed:
if (!this_msize || (this_msize > msize)) {
- // XXX This works even when (this_msize == 0) because the unsigned
- // subtraction wraps around to the correct result
leftover_msize = this_msize - msize;
leftover_ptr = (tiny_free_list_t *)((unsigned char *)ptr + TINY_BYTES_FOR_MSIZE(msize));
#if DEBUG_MALLOC
@@ -2373,16 +2159,15 @@
tiny_mag_ptr->mag_num_bytes_in_objects += TINY_BYTES_FOR_MSIZE(this_msize);
// Check that the region cookie is intact and update the region's bytes in use count
- tiny_region_t region = TINY_REGION_FOR_PTR(ptr);
- region_check_cookie(region, ®ION_COOKIE_FOR_TINY_REGION(region));
-
+ region_t *region = TINY_REGION_FOR_PTR(ptr);
region_trailer_t *trailer = REGION_TRAILER_FOR_TINY_REGION(region);
+ region_check_cookie(region, trailer);
size_t bytes_used = trailer->bytes_used + TINY_BYTES_FOR_MSIZE(this_msize);
trailer->bytes_used = (unsigned int)bytes_used;
trailer->objects_in_use++;
// Emptiness discriminant
- if (bytes_used < DENSITY_THRESHOLD(TINY_HEAP_SIZE)) {
+ if (bytes_used < DENSITY_THRESHOLD(TINY_REGION_PAYLOAD_BYTES)) {
/* After this allocation the region is still sparse, so it must have been even more so before
* the allocation. That implies the region is already correctly marked. Do nothing. */
} else {
@@ -2407,7 +2192,7 @@
tiny_malloc_should_clear(rack_t *rack, msize_t msize, boolean_t cleared_requested)
{
void *ptr;
- mag_index_t mag_index = rack_get_thread_index(rack) % rack->num_magazines;
+ mag_index_t mag_index = tiny_mag_get_thread_index() % rack->num_magazines;
magazine_t *tiny_mag_ptr = &(rack->magazines[mag_index]);
MALLOC_TRACE(TRACE_tiny_malloc, (uintptr_t)rack, TINY_BYTES_FOR_MSIZE(msize), (uintptr_t)tiny_mag_ptr, cleared_requested);
@@ -2436,9 +2221,9 @@
tiny_mag_ptr->mag_last_free_rgn = NULL;
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
-
- tiny_check_zero_or_clear(ptr, msize, cleared_requested);
-
+ if (cleared_requested) {
+ memset(ptr, 0, TINY_BYTES_FOR_MSIZE(msize));
+ }
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_should_clear(), tiny cache ptr=%p, msize=%d\n", ptr, msize);
@@ -2453,7 +2238,9 @@
if (ptr) {
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
- tiny_check_zero_or_clear(ptr, msize, cleared_requested);
+ if (cleared_requested) {
+ memset(ptr, 0, TINY_BYTES_FOR_MSIZE(msize));
+ }
return ptr;
}
@@ -2463,7 +2250,9 @@
if (ptr) {
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
- tiny_check_zero_or_clear(ptr, msize, cleared_requested);
+ if (cleared_requested) {
+ memset(ptr, 0, TINY_BYTES_FOR_MSIZE(msize));
+ }
return ptr;
}
}
@@ -2482,10 +2271,7 @@
tiny_mag_ptr->alloc_underway = TRUE;
OSMemoryBarrier();
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- fresh_region = mvm_allocate_pages(TINY_REGION_SIZE,
- TINY_BLOCKS_ALIGN,
- MALLOC_FIX_GUARD_PAGE_FLAGS(rack->debug_flags),
- VM_MEMORY_MALLOC_TINY);
+ fresh_region = mvm_allocate_pages_securely(TINY_REGION_SIZE, TINY_BLOCKS_ALIGN, VM_MEMORY_MALLOC_TINY, rack->debug_flags);
SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
// DTrace USDT Probe
@@ -2498,11 +2284,10 @@
return NULL;
}
- region_set_cookie(®ION_COOKIE_FOR_TINY_REGION(fresh_region));
+ region_set_cookie(REGION_TRAILER_FOR_TINY_REGION(fresh_region));
ptr = tiny_malloc_from_region_no_lock(rack, tiny_mag_ptr, mag_index, msize, fresh_region);
- // we don't clear or zero-check because this freshly allocated space
- // is pristine
+ // we don't clear because this freshly allocated space is pristine
tiny_mag_ptr->alloc_underway = FALSE;
OSMemoryBarrier();
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
@@ -2522,7 +2307,6 @@
{
if (tiny_region_for_ptr_no_lock(rack, ptr)) {
if (TINY_INDEX_FOR_PTR(ptr) >= NUM_TINY_BLOCKS) {
-malloc_printf("NO ZONE for ptr %p\n", ptr);
return 0;
}
@@ -2561,6 +2345,9 @@
static MALLOC_NOINLINE void
free_tiny_botch(rack_t *rack, tiny_free_list_t *ptr)
{
+ mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(TINY_REGION_FOR_PTR(ptr));
+ magazine_t *tiny_mag_ptr = &(rack->magazines[mag_index]);
+ SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
malloc_zone_error(rack->debug_flags, true, "Double free of object %p\n", ptr);
}
@@ -2572,7 +2359,6 @@
boolean_t is_free;
mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(tiny_region);
magazine_t *tiny_mag_ptr = &(rack->magazines[mag_index]);
- uint32_t flags = 0;
MALLOC_TRACE(TRACE_tiny_free, (uintptr_t)rack, (uintptr_t)ptr, (uintptr_t)tiny_mag_ptr, known_size);
@@ -2593,10 +2379,6 @@
}
#endif
- if (malloc_zero_policy == MALLOC_ZERO_ON_FREE) {
- memset(ptr, '\0', TINY_BYTES_FOR_MSIZE(msize));
- }
-
SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
#if CONFIG_TINY_CACHE
@@ -2609,15 +2391,12 @@
/* check that we don't already have this pointer in the cache */
if (ptr == ptr2) {
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
free_tiny_botch(rack, ptr);
return;
}
- if (malloc_zero_policy != MALLOC_ZERO_ON_FREE) {
- if ((rack->debug_flags & MALLOC_DO_SCRIBBLE) && msize) {
- memset(ptr, SCRABBLE_BYTE, TINY_BYTES_FOR_MSIZE(msize));
- }
+ if ((rack->debug_flags & MALLOC_DO_SCRIBBLE) && msize) {
+ memset(ptr, SCRABBLE_BYTE, TINY_BYTES_FOR_MSIZE(msize));
}
tiny_mag_ptr->mag_last_free = ptr;
@@ -2633,7 +2412,6 @@
msize = msize2;
ptr = ptr2;
tiny_region = rgn2;
- flags |= TINY_FREE_FLAG_FROM_CACHE;
}
}
#endif /* CONFIG_TINY_CACHE */
@@ -2652,12 +2430,8 @@
SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
}
- if (partial_free) {
- flags |= TINY_FREE_FLAG_PARTIAL;
- }
-
if (tiny_free_no_lock(rack, tiny_mag_ptr, mag_index, tiny_region, ptr,
- msize, flags)) {
+ msize, partial_free)) {
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
}
@@ -2669,8 +2443,7 @@
{
msize_t msize = TINY_MSIZE_FOR_BYTES(size + TINY_QUANTUM - 1);
unsigned found = 0;
-
- mag_index_t mag_index = rack_get_thread_index(&szone->tiny_rack) % szone->tiny_rack.num_magazines;
+ mag_index_t mag_index = tiny_mag_get_thread_index() % szone->tiny_rack.num_magazines;
magazine_t *tiny_mag_ptr = &(szone->tiny_rack.magazines[mag_index]);
// make sure to return objects at least one quantum in size
@@ -2745,10 +2518,7 @@
if (is_free) {
break; // a double free; let the standard free deal with it
}
- if (malloc_zero_policy == MALLOC_ZERO_ON_FREE) {
- memset(ptr, '\0', TINY_BYTES_FOR_MSIZE(msize));
- }
- if (!tiny_free_no_lock(&szone->tiny_rack, tiny_mag_ptr, mag_index, tiny_region, ptr, msize, 0)) {
+ if (!tiny_free_no_lock(&szone->tiny_rack, tiny_mag_ptr, mag_index, tiny_region, ptr, msize, false)) {
// Arrange to re-acquire magazine lock
tiny_mag_ptr = NULL;
tiny_region = NULL;
@@ -2825,9 +2595,9 @@
{
unsigned counts[1024];
unsigned in_use = 0;
- uintptr_t start = (uintptr_t)TINY_REGION_HEAP_BASE(region);
+ uintptr_t start = (uintptr_t)TINY_REGION_ADDRESS(region);
uintptr_t current = start + bytes_at_start;
- uintptr_t limit = (uintptr_t)TINY_REGION_HEAP_END(region) - bytes_at_end;
+ uintptr_t limit = (uintptr_t)TINY_REGION_END(region) - bytes_at_end;
uintptr_t mapped_start;
boolean_t is_free;
msize_t msize;
@@ -2890,7 +2660,7 @@
}
if ((b = _simple_salloc()) != NULL) {
mag_index_t mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(mapped_region);
- _simple_sprintf(b, "Tiny region [%p-%p, %y] \t", (void *)start, TINY_REGION_HEAP_END(region), (int)TINY_REGION_SIZE);
+ _simple_sprintf(b, "Tiny region [%p-%p, %y] \t", (void *)start, TINY_REGION_END(region), (int)TINY_REGION_SIZE);
if (mag_index == DEPOT_MAGAZINE_INDEX) {
_simple_sprintf(b, "Recirc depot \t");
} else {
@@ -2976,39 +2746,3 @@
}
return 1;
}
-
-boolean_t
-tiny_check(rack_t *rack, unsigned counter)
-{
- size_t index;
-
- /* check tiny regions - chould check region count */
- for (index = 0; index < rack->region_generation->num_regions_allocated; ++index) {
- region_t tiny = rack->region_generation->hashed_regions[index];
-
- if (HASHRING_REGION_DEALLOCATED == tiny) {
- continue;
- }
-
- if (tiny) {
- magazine_t *tiny_mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
- REGION_TRAILER_FOR_TINY_REGION(tiny),
- MAGAZINE_INDEX_FOR_TINY_REGION(tiny));
-
- if (!tiny_check_region(rack, tiny, index, counter)) {
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- return 0;
- }
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- }
- }
-
- /* check tiny free lists */
- for (index = 0; index < NUM_TINY_SLOTS; ++index) {
- if (!tiny_free_list_check(rack, (grain_t)index, counter)) {
- return 0;
- }
- }
-
- return 1;
-}