Loading...
--- libmalloc/libmalloc-166.251.2/src/magazine_tiny.c
+++ libmalloc/libmalloc-140.40.1/src/magazine_tiny.c
@@ -23,46 +23,6 @@
#include "internal.h"
-// The address and size of the block in mag_last_free are combined. These
-// macros abstract construction of the combined value and extraction of the
-// size and pointer.
-#define TINY_MAG_LAST_FREE_FROM_PTR_AND_MSIZE(ptr, msize) (void *)(((uintptr_t)(ptr))|((msize_t)msize))
-#define TINY_PTR_FROM_MAG_LAST_FREE(x) (void *)(((uintptr_t)(x)) & ~(TINY_QUANTUM - 1))
-#define TINY_MSIZE_FROM_MAG_LAST_FREE(x) (msize_t)(((uintptr_t)(x)) & (TINY_QUANTUM - 1))
-
-// Adjusts the pointer part of mag_last_free by a given amount in bytes. Must be
-// a multiple of the quantum size (not checked).
-#define TINY_MAG_LAST_FREE_PTR_ADJUST_PTR(x, size) (x) = ((void *)(x) + (size))
-
-// Decrements the size part of mag_last_free by a given msize value. Must not
-// 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)
-{
- return (!msize || (msize > NUM_TINY_SLOTS) ? NUM_TINY_SLOTS : msize - 1);
-}
-
/*
* Get the size of the previous free block, which is stored in the last two
* bytes of the block. If the previous block is not free, then the result is
@@ -97,14 +57,11 @@
uint32_t val = (1 << (index & 31));
#if DEBUG_MALLOC
- if (msize > NUM_TINY_SLOTS) {
- malloc_report(ASL_LEVEL_ERR, "set_tiny_meta_header_in_use() invariant broken %p %d\n", ptr, msize);
+ if (msize >= NUM_TINY_SLOTS) {
+ malloc_printf("set_tiny_meta_header_in_use() invariant broken %p %d\n", ptr, msize);
}
if ((unsigned)index + (unsigned)msize > 0x10000) {
- malloc_report(ASL_LEVEL_ERR, "set_tiny_meta_header_in_use() invariant broken (2) %p %d\n", ptr, msize);
- }
- if (msize > TINY_BITMAP_RANGE_LIMIT) {
- malloc_report(ASL_LEVEL_ERROR, "set_tiny_meta_header_in_use() invariant broken (3) %p %d\n", ptr, msize);
+ malloc_printf("set_tiny_meta_header_in_use() invariant broken (2) %p %d\n", ptr, msize);
}
#endif
@@ -158,8 +115,8 @@
mf = get_tiny_meta_header(ptr, &ff);
if (msize != mf) {
- malloc_report(ASL_LEVEL_INFO, "setting header for tiny in_use %p : %d\n", ptr, msize);
- malloc_report(ASL_LEVEL_INFO, "reading header for tiny %p : %d %d\n", ptr, mf, ff);
+ malloc_printf("setting header for tiny in_use %p : %d\n", ptr, msize);
+ malloc_printf("reading header for tiny %p : %d %d\n", ptr, mf, ff);
}
}
#endif
@@ -209,7 +166,7 @@
#if DEBUG_MALLOC
if ((unsigned)index + (unsigned)msize > 0x10000) {
- malloc_report(ASL_LEVEL_ERR, "setting header for tiny free %p msize too large: %d\n", ptr, msize);
+ malloc_printf("setting header for tiny free %p msize too large: %d\n", ptr, msize);
}
#endif
@@ -232,8 +189,8 @@
boolean_t ff;
msize_t mf = get_tiny_meta_header(ptr, &ff);
if ((msize != mf) || !ff) {
- malloc_report(ASL_LEVEL_INFO, "setting header for tiny free %p : %u\n", ptr, msize);
- malloc_report(ASL_LEVEL_INFO, "reading header for tiny %p : %u %u\n", ptr, mf, ff);
+ malloc_printf("setting header for tiny free %p : %u\n", ptr, msize);
+ malloc_printf("reading header for tiny %p : %u %u\n", ptr, mf, ff);
}
#endif
}
@@ -301,30 +258,30 @@
static void
tiny_free_list_add_ptr(rack_t *rack, magazine_t *tiny_mag_ptr, void *ptr, msize_t msize)
{
- grain_t slot = (!msize || (msize > NUM_TINY_SLOTS)) ? NUM_TINY_SLOTS : msize - 1;
+ grain_t slot = (!msize || (msize >= NUM_TINY_SLOTS)) ? NUM_TINY_SLOTS - 1 : msize - 1;
tiny_free_list_t *free_ptr = ptr;
tiny_free_list_t *free_head = tiny_mag_ptr->mag_free_list[slot].p;
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
+ malloc_printf("in %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
}
if (((uintptr_t)ptr) & (TINY_QUANTUM - 1)) {
- malloc_zone_error(rack->debug_flags, true, "tiny_free_list_add_ptr: Unaligned ptr: %p\n", ptr);
+ szone_error(rack->debug_flags, 1, "tiny_free_list_add_ptr: Unaligned ptr", ptr, NULL);
}
#endif
set_tiny_meta_header_free(ptr, msize);
if (free_head) {
#if DEBUG_MALLOC
if (free_list_unchecksum_ptr(szone, &free_head->previous)) {
- malloc_zone_error(rack->debug_flags, true,
- "tiny_free_list_add_ptr: Internal invariant broken (free_head->previous): "
- "ptr=%p slot=%d free_head=%p previous=%p\n", ptr, slot, (void *)free_head, free_head->previous.p);
+ szone_error(rack->debug_flags, 1,
+ "tiny_free_list_add_ptr: Internal invariant broken (free_head->previous)", ptr,
+ "ptr=%p slot=%d free_head=%p previous=%p\n", ptr, slot, (void *)free_head, free_head->previous.p);
}
if (!tiny_meta_header_is_free(free_head)) {
- malloc_zone_error(rack->debug_flags, true,
- "tiny_free_list_add_ptr: Internal invariant broken (free_head is not a free pointer): "
- "ptr=%p slot=%d free_head=%p\n", ptr, slot, (void *)free_head);
+ szone_error(rack->debug_flags, 1,
+ "tiny_free_list_add_ptr: Internal invariant broken (free_head is not a free pointer)", ptr,
+ "ptr=%p slot=%d free_head=%p\n", ptr, slot, (void *)free_head);
}
#endif
free_head->previous.u = free_list_checksum_ptr(rack, free_ptr);
@@ -344,7 +301,7 @@
static void
tiny_free_list_remove_ptr(rack_t *rack, magazine_t *tiny_mag_ptr, void *ptr, msize_t msize)
{
- grain_t slot = tiny_slot_from_msize(msize);
+ grain_t slot = (!msize || (msize >= NUM_TINY_SLOTS)) ? NUM_TINY_SLOTS - 1 : msize - 1;
tiny_free_list_t *free_ptr = ptr, *next, *previous;
next = free_list_unchecksum_ptr(rack, &free_ptr->next);
@@ -352,17 +309,17 @@
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "In %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
+ malloc_printf("In %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
}
#endif
if (!previous) {
// The block to remove is the head of the free list
#if DEBUG_MALLOC
if (tiny_mag_ptr->mag_free_list[slot] != ptr) {
- malloc_zone_error(rack->debug_flags, true,
- "tiny_free_list_remove_ptr: Internal invariant broken (tiny_mag_ptr->mag_free_list[slot]): "
- "ptr=%p slot=%d msize=%d tiny_mag_ptr->mag_free_list[slot]=%p\n", ptr, slot, msize,
- (void *)tiny_mag_ptr->mag_free_list[slot]);
+ szone_error(rack->debug_flags, 1,
+ "tiny_free_list_remove_ptr: Internal invariant broken (tiny_mag_ptr->mag_free_list[slot])", ptr,
+ "ptr=%p slot=%d msize=%d tiny_mag_ptr->mag_free_list[slot]=%p\n", ptr, slot, msize,
+ (void *)tiny_mag_ptr->mag_free_list[slot]);
return;
}
#endif
@@ -371,29 +328,11 @@
BITMAPV_CLR(tiny_mag_ptr->mag_bitmap, slot);
}
} else {
- // Check that the next pointer of "previous" points to free_ptr.
- tiny_free_list_t *prev_next = free_list_unchecksum_ptr(rack, &previous->next);
- if (prev_next != free_ptr) {
- malloc_zone_error(rack->debug_flags, true,
- "tiny_free_list_remove_ptr: Internal invariant broken (next ptr of prev): "
- "ptr=%p, prev_next=%p\n", ptr, prev_next);
- __builtin_unreachable(); // Always crashes in malloc_zone_error().
- }
-
// We know free_ptr is already checksummed, so we don't need to do it
// again.
previous->next = free_ptr->next;
}
if (next) {
- // Check that the previous pointer of "next" points to free_ptr.
- tiny_free_list_t *next_prev = free_list_unchecksum_ptr(rack, &next->previous);
- if (next_prev != free_ptr) {
- malloc_zone_error(rack->debug_flags, true,
- "tiny_free_list_remove_ptr: Internal invariant broken (prev ptr of next): "
- "ptr=%p, next_prev=%p\n", ptr, next_prev);
- __builtin_unreachable(); // Always crashes in malloc_zone_error().
- }
-
// We know free_ptr is already checksummed, so we don't need to do it
// again.
next->previous = free_ptr->previous;
@@ -497,7 +436,7 @@
}
if (!msize) {
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_ERR, "*** tiny_free_detach_region error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
+ malloc_printf("*** tiny_free_detach_region error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
#endif
break;
}
@@ -529,7 +468,7 @@
}
if (!msize) {
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_ERR, "*** tiny_free_reattach_region error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
+ malloc_printf("*** tiny_free_reattach_region error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
#endif
break;
}
@@ -566,7 +505,7 @@
if (is_free && !msize && (current == start)) {
// first block is all free
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_INFO, "*** tiny_free_scan_madvise_free first block is all free! %p: msize=%d is_free=%d\n", (void *)current,
+ malloc_printf("*** tiny_free_scan_madvise_free first block is all free! %p: msize=%d is_free =%d\n", (void *)current,
msize, is_free);
#endif
uintptr_t pgLo = round_page_kernel(start + sizeof(tiny_free_list_t) + sizeof(msize_t));
@@ -581,7 +520,7 @@
}
if (!msize) {
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_ERR, "*** tiny_free_scan_madvise_free error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
+ malloc_printf("*** tiny_free_scan_madvise_free error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
#endif
break;
}
@@ -614,7 +553,7 @@
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);
+ mvm_madvise_free(rack, r, addr, addr + size, NULL);
}
SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
OSAtomicDecrement32Barrier(&(REGION_TRAILER_FOR_TINY_REGION(r)->pinned_to_depot));
@@ -625,7 +564,7 @@
tiny_find_msize_region(rack_t *rack, magazine_t *tiny_mag_ptr, mag_index_t mag_index, msize_t msize)
{
tiny_free_list_t *ptr;
- grain_t slot = tiny_slot_from_msize(msize);
+ grain_t slot = msize - 1;
free_list_t *free_list = tiny_mag_ptr->mag_free_list;
free_list_t *the_slot = free_list + slot;
free_list_t *limit;
@@ -656,7 +595,7 @@
}
slot = BITMAPV_CTZ(bitmap);
- limit = free_list + NUM_TINY_SLOTS;
+ limit = free_list + NUM_TINY_SLOTS - 1;
free_list += slot;
if (free_list < limit) {
@@ -666,13 +605,13 @@
} else {
/* Shouldn't happen. Fall through to look at last slot. */
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_ERR, "in tiny_find_msize_region(), mag_bitmap out of sync, slot=%d\n", slot);
+ malloc_printf("in tiny_find_msize_region(), mag_bitmap out of sync, slot=%d\n", slot);
#endif
}
}
// We are now looking at the last slot, which contains blocks equal to, or
- // due to coalescing of free blocks, larger than NUM_TINY_SLOTS * tiny quantum size.
+ // due to coalescing of free blocks, larger than (NUM_TINY_SLOTS - 1) * tiny quantum size.
ptr = limit->p;
if (ptr) {
return TINY_REGION_FOR_PTR(ptr);
@@ -680,89 +619,6 @@
return NULL;
}
-
-#if CONFIG_MADVISE_PRESSURE_RELIEF
-void
-tiny_madvise_pressure_relief(rack_t *rack)
-{
- mag_index_t mag_index;
- magazine_t *tiny_depot_ptr = (&rack->magazines[DEPOT_MAGAZINE_INDEX]);
-
- 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) {
- SZONE_LOCK(TINY_SZONE_FROM_RACK(rack));
-
- region_t tiny = rack->region_generation->hashed_regions[index];
- if (!tiny || tiny == HASHRING_REGION_DEALLOCATED) {
- SZONE_UNLOCK(TINY_SZONE_FROM_RACK(rack));
- continue;
- }
-
- magazine_t *mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
- 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
- * after we've obtained the lock.
- */
- mag_index_t src_mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(tiny);
-
- /* We can (and must) ignore magazines that are already in the recirc depot. */
- if (src_mag_index == DEPOT_MAGAZINE_INDEX) {
- 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);
- }
-
- /* Because this region is currently in use, we can't safely madvise it while
- * it's attached to the magazine. For this operation we have to remove it from
- * the current mag, attach it to the depot and then madvise.
- */
-
- recirc_list_extract(rack, mag_ptr, REGION_TRAILER_FOR_TINY_REGION(tiny));
- int objects_in_use = tiny_free_detach_region(rack, mag_ptr, tiny);
-
- SZONE_MAGAZINE_PTR_LOCK(tiny_depot_ptr);
- MAGAZINE_INDEX_FOR_TINY_REGION(tiny) = DEPOT_MAGAZINE_INDEX;
- 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_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_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));
-
- /* Actually do the scan, done holding the depot lock, the call will drop the lock
- * around the actual madvise syscalls.
- */
- tiny_free_scan_madvise_free(rack, tiny_depot_ptr, tiny);
-
- /* Now the region is in the recirc depot, the next allocations to require more
- * blocks will come along and take one of these regions back out of the depot.
- * As OS X madvise's reuse on an per-region basis, we leave as many of these
- * regions in the depot as possible after memory pressure.
- */
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_depot_ptr);
- }
- }
-}
-#endif // CONFIG_MADVISE_PRESSURE_RELIEF
static MALLOC_INLINE void
tiny_madvise_free_range_no_lock(rack_t *rack,
@@ -803,7 +659,7 @@
OSAtomicIncrement32Barrier(&(node->pinned_to_depot));
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- mvm_madvise_free(rack, region, free_lo, free_hi, &rack->last_madvise, rack->debug_flags & MALLOC_DO_SCRIBBLE);
+ mvm_madvise_free(rack, region, free_lo, free_hi, &rack->last_madvise);
SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
OSAtomicDecrement32Barrier(&(node->pinned_to_depot));
@@ -825,7 +681,7 @@
#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");
+ szone_error(rack->debug_flags, 1, "tiny_get_region_from_depot called for magazine index -1", NULL, NULL);
return 0;
}
#endif
@@ -879,6 +735,10 @@
SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
+ // madvise() outside the Depot lock
+ (void)mvm_madvise_reuse(sparse_region, (uintptr_t)sparse_region,
+ (uintptr_t)sparse_region + TINY_REGION_PAYLOAD_BYTES, rack->debug_flags);
+
// DTrace USDT Probe
MAGMALLOC_DEPOTREGION(TINY_SZONE_FROM_RACK(rack), (int)mag_index, (void *)sparse_region,
TINY_REGION_SIZE, (int)BYTES_USED_FOR_TINY_REGION(sparse_region));
@@ -891,7 +751,7 @@
tiny_free_try_depot_unmap_no_lock(rack_t *rack, magazine_t *depot_ptr, region_trailer_t *node)
{
if (0 < node->bytes_used || 0 < node->pinned_to_depot ||
- depot_ptr->recirculation_entries < recirc_retained_regions)
+ depot_ptr->recirculation_entries < (rack->num_magazines * 2))
{
return NULL;
}
@@ -912,7 +772,7 @@
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);
+ szone_error(rack->debug_flags, 1, "tiny_free_try_depot_unmap_no_lock hash lookup failed:", NULL, "%p\n", sparse_region);
return NULL;
}
*pSlot = HASHRING_REGION_DEALLOCATED;
@@ -929,7 +789,7 @@
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);
+ szone_error(rack->debug_flags, 1, "tiny_free_try_depot_unmap_no_lock objects_in_use not zero:", NULL, "%d\n", objects_in_use);
return NULL;
}
}
@@ -942,19 +802,13 @@
// is at least fraction "f" empty.) Such a region will be marked "suitable" on the recirculation list.
region_trailer_t *node = tiny_mag_ptr->firstNode;
- while (node && (!node->recirc_suitable || node->pinned_to_depot)) {
- // If we skip a node due to pinned_to_depot being non-zero, it must be
- // because another thread is madvising the same region in
- // tiny_madvise_free_range_no_lock(), called from tiny_free_no_lock().
- // When that's done, the same thread will enter tiny_free_try_recirc_to_depot()
- // for the same region, which will come back here. So this just defers
- // recirculation of the region.
+ while (node && !node->recirc_suitable) {
node = node->next;
}
if (NULL == node) {
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_INFO, "*** tiny_free_do_recirc_to_depot end of list\n");
+ malloc_printf("*** tiny_free_do_recirc_to_depot end of list\n");
#endif
return TRUE; // Caller must SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
}
@@ -1090,12 +944,11 @@
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_free_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
+ malloc_printf("in tiny_free_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
}
if (!msize) {
- malloc_zone_error(rack->debug_flags, true,
- "trying to free tiny block that is too small in tiny_free_no_lock(), ptr=%p, msize=%d\n",
- ptr, msize);
+ szone_error(rack->debug_flags, 1, "trying to free tiny block that is too small", ptr, "in tiny_free_no_lock(), ptr=%p, msize=%d\n", ptr,
+ msize);
}
#endif
@@ -1104,7 +957,7 @@
if (previous) {
#if DEBUG_MALLOC
if (LOG(szone, ptr) || LOG(szone, previous)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_free_no_lock(), coalesced backwards for %p previous=%p\n", ptr, previous);
+ malloc_printf("in tiny_free_no_lock(), coalesced backwards for %p previous=%p\n", ptr, previous);
}
#endif
@@ -1119,15 +972,15 @@
next_msize = get_tiny_free_size(next_block);
#if DEBUG_MALLOC
if (LOG(szone, ptr) || LOG(szone, next_block)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_free_no_lock(), for ptr=%p, msize=%d coalesced forward=%p next_msize=%d\n", ptr, msize,
+ malloc_printf("in tiny_free_no_lock(), for ptr=%p, msize=%d coalesced forward=%p next_msize=%d\n", ptr, msize,
next_block, next_msize);
}
#endif
// If we are coalescing with the next block, and the next block is in
// the last slot of the free list, then we optimize this case here to
- // avoid removing next_block from the slot NUM_TINY_SLOTS and then adding ptr back
- // to slot NUM_TINY_SLOTS.
- if (next_msize > NUM_TINY_SLOTS) {
+ // avoid removing next_block from the slot (NUM_TINY_SLOTS - 1) and then adding ptr back
+ // to slot (NUM_TINY_SLOTS - 1).
+ if (next_msize >= NUM_TINY_SLOTS) {
msize += next_msize;
big_free_block = (tiny_free_list_t *)next_block;
@@ -1135,7 +988,7 @@
before_next_block = free_list_unchecksum_ptr(rack, &big_free_block->previous);
if (!before_next_block) {
- tiny_mag_ptr->mag_free_list[NUM_TINY_SLOTS].p = ptr;
+ tiny_mag_ptr->mag_free_list[NUM_TINY_SLOTS - 1].p = ptr;
} else {
before_next_block->next.u = free_list_checksum_ptr(rack, ptr);
}
@@ -1236,7 +1089,7 @@
offset_msize = strtol(getenv("MallocASLRForce"), NULL, 0) & TINY_ENTROPY_MASK;
}
if (getenv("MallocASLRPrint")) {
- malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", aligned_address, offset_msize);
+ malloc_printf("Region: %p offset: %d\n", aligned_address, offset_msize);
}
#endif
#else
@@ -1267,7 +1120,7 @@
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_region_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
+ malloc_printf("in tiny_malloc_from_region_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
}
#endif
return ptr;
@@ -1277,7 +1130,7 @@
tiny_memalign(szone_t *szone, size_t alignment, size_t size, size_t span)
{
msize_t mspan = TINY_MSIZE_FOR_BYTES(span + TINY_QUANTUM - 1);
- void *p = tiny_malloc_should_clear(&szone->tiny_rack, mspan, 0);
+ void *p = szone_malloc(szone, span); // avoids inlining tiny_malloc_should_clear(szone, mspan, 0);
if (NULL == p) {
return NULL;
@@ -1308,7 +1161,8 @@
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
// Give up mpad blocks beginning at p to the tiny free list
- free_tiny(&szone->tiny_rack, p, TINY_REGION_FOR_PTR(p), TINY_BYTES_FOR_MSIZE(mpad));
+ // region_t r = TINY_REGION_FOR_PTR(p);
+ szone_free(szone, p); // avoids inlining free_tiny(szone, p, &r);
p = q; // advance p to the desired alignment
}
@@ -1324,17 +1178,11 @@
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
// Give up mwaste blocks beginning at q to the tiny free list
- free_tiny(&szone->tiny_rack, q, TINY_REGION_FOR_PTR(q), TINY_BYTES_FOR_MSIZE(mwaste));
+ // region_t r = TINY_REGION_FOR_PTR(q);
+ szone_free(szone, q); // avoids inlining free_tiny(szone, q, &r);
}
return p; // p has the desired size and alignment, and can later be free()'d
-}
-
-boolean_t
-tiny_claimed_address(rack_t *rack, void *ptr)
-{
- region_t r = tiny_region_for_ptr_no_lock(rack, ptr);
- return r && ptr < TINY_REGION_END(r);
}
void *
@@ -1368,12 +1216,11 @@
unsigned next_index;
void *next_block;
boolean_t is_free;
- msize_t next_msize, coalesced_msize, leftover_msize, new_msize;
+ msize_t next_msize, coalesced_msize, leftover_msize;
void *leftover;
index = TINY_INDEX_FOR_PTR(ptr);
old_msize = TINY_MSIZE_FOR_BYTES(old_size);
- new_msize = TINY_MSIZE_FOR_BYTES(new_size + TINY_QUANTUM - 1);
next_index = index + old_msize;
if (next_index >= NUM_TINY_BLOCKS) {
@@ -1390,88 +1237,38 @@
return 0;
}
- coalesced_msize = new_msize - old_msize;
-#if CONFIG_TINY_CACHE
- void *last_free_ptr = tiny_mag_ptr->mag_last_free;
- msize_t last_free_msize = tiny_mag_ptr->mag_last_free_msize;
- if (last_free_ptr == next_block && old_msize + last_free_msize >= new_msize) {
- /*
- * There is a block in mag_last_free and it's immediately after
- * this block and it's large enough. We can use some or all of it.
- */
- leftover_msize = last_free_msize - coalesced_msize;
- if (leftover_msize) {
- tiny_mag_ptr->mag_last_free_msize -= coalesced_msize;
- tiny_mag_ptr->mag_last_free += new_size - old_size;
- // 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.
- 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;
- tiny_mag_ptr->mag_last_free_msize = 0;
- tiny_mag_ptr->mag_last_free_rgn = NULL;
- }
- set_tiny_meta_header_middle(next_block);
- } else {
-#endif // CONFIG_TINY_CACHE
- /*
- * 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_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
- // header/in-use and the next one has header/free. We need to
- // reset both the header and in-use bit in the first block and we
- // need to reset the header bit in the second block if it's part of
- // the new allocation.
- set_tiny_meta_header_middle(unused_start);
- if (coalesced_msize > 1) {
- set_tiny_meta_header_middle(unused_start + TINY_QUANTUM);
- }
- tiny_mag_ptr->mag_bytes_free_at_end -= TINY_BYTES_FOR_MSIZE(coalesced_msize);
- if (tiny_mag_ptr->mag_bytes_free_at_end) {
- // 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));
- }
- } else {
- /*
- * Look for a free block immediately afterwards. If it's large
- * enough, we can consume (part of) it.
- */
- is_free = tiny_meta_header_is_free(next_block);
- if (!is_free) {
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- return 0; // next_block is in use;
- }
- next_msize = get_tiny_free_size(next_block);
- if (old_msize + next_msize < new_msize) {
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- return 0; // even with next block, not enough
- }
- /*
- * The following block is big enough; pull it from its freelist and chop off enough to satisfy
- * our needs.
- */
- 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
- leftover_msize = next_msize - coalesced_msize;
- if (leftover_msize) {
- /* there's some left, so put the remainder back */
- leftover = (void *)((uintptr_t)next_block + TINY_BYTES_FOR_MSIZE(coalesced_msize));
- tiny_free_list_add_ptr(rack, tiny_mag_ptr, leftover, leftover_msize);
- }
- set_tiny_meta_header_in_use(ptr, old_msize + coalesced_msize);
- }
-#if CONFIG_TINY_CACHE
- }
-#endif // CONFIG_TINY_CACHE
+ /*
+ * Look for a free block immediately afterwards. If it's large enough, we can consume (part of)
+ * it.
+ */
+ is_free = tiny_meta_header_is_free(next_block);
+ if (!is_free) {
+ SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
+ return 0; // next_block is in use;
+ }
+ next_msize = get_tiny_free_size(next_block);
+ if (old_size + TINY_BYTES_FOR_MSIZE(next_msize) < new_size) {
+ SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
+ return 0; // even with next block, not enough
+ }
+ /*
+ * The following block is big enough; pull it from its freelist and chop off enough to satisfy
+ * our needs.
+ */
+ 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
+ coalesced_msize = TINY_MSIZE_FOR_BYTES(new_size - old_size + TINY_QUANTUM - 1);
+ leftover_msize = next_msize - coalesced_msize;
+ if (leftover_msize) {
+ /* there's some left, so put the remainder back */
+ leftover = (void *)((uintptr_t)next_block + TINY_BYTES_FOR_MSIZE(coalesced_msize));
+
+ tiny_free_list_add_ptr(rack, tiny_mag_ptr, leftover, leftover_msize);
+ }
+ set_tiny_meta_header_in_use(ptr, old_msize + coalesced_msize);
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_try_realloc_in_place(), ptr=%p, msize=%d\n", ptr, old_msize + coalesced_msize);
+ malloc_printf("in tiny_try_realloc_in_place(), ptr=%p, msize=%d\n", ptr, old_msize + coalesced_msize);
}
#endif
tiny_mag_ptr->mag_num_bytes_in_objects += TINY_BYTES_FOR_MSIZE(coalesced_msize);
@@ -1496,15 +1293,8 @@
return 1;
}
-static char *tiny_check_fail_msg = "*** check: incorrect tiny region ";
-
-#define TINY_CHECK_FAIL(fmt, ...) \
- malloc_zone_check_fail(tiny_check_fail_msg, \
- "%ld, counter=%d\n" fmt, region_index, counter, __VA_ARGS__);
-
boolean_t
-tiny_check_region(rack_t *rack, region_t region, size_t region_index,
- unsigned counter)
+tiny_check_region(rack_t *rack, region_t region)
{
uintptr_t start, ptr, region_end;
boolean_t prev_free = 0;
@@ -1518,15 +1308,6 @@
// Assumes locked
CHECK_MAGAZINE_PTR_LOCKED(szone, tiny_mag_ptr, __PRETTY_FUNCTION__);
- // Do not check the region if pinned_to_depot is not zero because it
- // may not be in a consistent state (specifically, if may have a
- // block marked as in-use that's longer than any legal allocation,
- // which upsets get_tiny_meta_header() because it can't determine the
- // block's length).
- if (!REGION_TRAILER_FOR_TINY_REGION(region)->pinned_to_depot) {
- return 1;
- }
-
/* establish region limits */
start = (uintptr_t)TINY_REGION_ADDRESS(region);
ptr = start;
@@ -1539,9 +1320,7 @@
if (tiny_mag_ptr->mag_bytes_free_at_start) {
msize = get_tiny_meta_header((void *)(ptr - TINY_QUANTUM), &is_free);
if (is_free || (msize != 1)) {
- TINY_CHECK_FAIL("*** invariant broken for leader block %p - %d %d\n",
- (void *)(ptr - TINY_QUANTUM), msize, is_free);
- return 0;
+ malloc_printf("*** invariant broken for leader block %p - %d %d\n", ptr - TINY_QUANTUM, msize, is_free);
}
}
}
@@ -1573,17 +1352,17 @@
* corrupt.
*/
if (!msize) {
- TINY_CHECK_FAIL("*** invariant broken for tiny block %p this msize=%d - size is too small\n", (void *)ptr, msize);
+ malloc_printf("*** invariant broken for tiny block %p this msize=%d - size is too small\n", ptr, msize);
return 0;
}
if (!is_free) {
/*
- * In use blocks cannot be more than NUM_TINY_SLOTS quanta large.
+ * In use blocks cannot be more than (NUM_TINY_SLOTS - 1) quanta large.
*/
prev_free = 0;
- if (msize > NUM_TINY_SLOTS) {
- TINY_CHECK_FAIL("*** invariant broken for %p this tiny msize=%d - size is too large\n", (void *)ptr, msize);
+ if (msize > (NUM_TINY_SLOTS - 1)) {
+ malloc_printf("*** invariant broken for %p this tiny msize=%d - size is too large\n", ptr, msize);
return 0;
}
/* move to next block */
@@ -1595,7 +1374,7 @@
* free block.
*/
if (prev_free) {
- TINY_CHECK_FAIL("*** invariant broken for free block %p this tiny msize=%d: two free blocks in a row\n", (void *)ptr, msize);
+ malloc_printf("*** invariant broken for free block %p this tiny msize=%d: two free blocks in a row\n", ptr, msize);
return 0;
}
#endif // CONFIG_RELAXED_INVARIANT_CHECKS
@@ -1607,11 +1386,11 @@
previous = free_list_unchecksum_ptr(rack, &free_head->previous);
next = free_list_unchecksum_ptr(rack, &free_head->next);
if (previous && !tiny_meta_header_is_free(previous)) {
- TINY_CHECK_FAIL("*** invariant broken for %p (previous %p is not a free pointer)\n", (void *)ptr, previous);
+ malloc_printf("*** invariant broken for %p (previous %p is not a free pointer)\n", ptr, previous);
return 0;
}
if (next && !tiny_meta_header_is_free(next)) {
- TINY_CHECK_FAIL("*** invariant broken for %p (next in free list %p is not a free pointer)\n", (void *)ptr, next);
+ malloc_printf("*** invariant broken for %p (next in free list %p is not a free pointer)\n", ptr, next);
return 0;
}
/*
@@ -1619,10 +1398,10 @@
*/
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] "
- "(end marker incorrect) should be %d; in fact %d\n",
- (void *)ptr, follower, TINY_REGION_ADDRESS(region), (void *)region_end,
- msize, get_tiny_previous_free_msize(follower));
+ malloc_printf(
+ "*** invariant broken for tiny free %p followed by %p in region [%p-%p] "
+ "(end marker incorrect) should be %d; in fact %d\n",
+ ptr, follower, TINY_REGION_ADDRESS(region), region_end, msize, get_tiny_previous_free_msize(follower));
return 0;
}
/* move to next block */
@@ -1633,7 +1412,7 @@
* Ensure that we scanned the entire region
*/
if (ptr != region_end) {
- TINY_CHECK_FAIL("*** invariant broken for region end %p - %p\n", (void *)ptr, (void *)region_end);
+ malloc_printf("*** invariant broken for region end %p - %p\n", ptr, region_end);
return 0;
}
/*
@@ -1643,8 +1422,7 @@
if (tiny_mag_ptr->mag_bytes_free_at_end) {
msize = get_tiny_meta_header((void *)ptr, &is_free);
if (is_free || (msize != 1)) {
- TINY_CHECK_FAIL("*** invariant broken for blocker block %p - %d %d\n", (void *)ptr, msize, is_free);
- return 0;
+ malloc_printf("*** invariant broken for blocker block %p - %d %d\n", ptr, msize, is_free);
}
}
}
@@ -1717,7 +1495,8 @@
recorder(task, context, MALLOC_PTR_REGION_RANGE_TYPE, &ptr_range, 1);
}
if (type_mask & MALLOC_PTR_IN_USE_RANGE_TYPE) {
- vm_address_t mag_last_free;
+ void *mag_last_free;
+ vm_address_t mag_last_free_ptr = 0;
msize_t mag_last_free_msize = 0;
err = reader(task, range.address, range.size, (void **)&mapped_region);
@@ -1729,13 +1508,19 @@
magazine_t *tiny_mag_ptr = tiny_mag_base + mag_index;
if (DEPOT_MAGAZINE_INDEX != mag_index) {
- mag_last_free = (uintptr_t)tiny_mag_ptr->mag_last_free;
- mag_last_free_msize = tiny_mag_ptr->mag_last_free_msize;
+ mag_last_free = tiny_mag_ptr->mag_last_free;
+ if (mag_last_free) {
+ mag_last_free_ptr = (uintptr_t)mag_last_free & ~(TINY_QUANTUM - 1);
+ mag_last_free_msize = (uintptr_t)mag_last_free & (TINY_QUANTUM - 1);
+ }
} else {
for (mag_index = 0; mag_index < szone->tiny_rack.num_magazines; mag_index++) {
if ((void *)range.address == (tiny_mag_base + mag_index)->mag_last_free_rgn) {
- mag_last_free = (uintptr_t)(tiny_mag_base + mag_index)->mag_last_free;
- mag_last_free_msize = (tiny_mag_base + mag_index)->mag_last_free_msize;
+ mag_last_free = (tiny_mag_base + mag_index)->mag_last_free;
+ if (mag_last_free) {
+ mag_last_free_ptr = (uintptr_t)mag_last_free & ~(TINY_QUANTUM - 1);
+ mag_last_free_msize = (uintptr_t)mag_last_free & (TINY_QUANTUM - 1);
+ }
}
}
}
@@ -1770,7 +1555,7 @@
msize = 1;
}
- } else if (range.address + block_offset != mag_last_free) {
+ } else if (range.address + block_offset != mag_last_free_ptr) {
msize = 1;
bit = block_index + 1;
while (!BITARRAY_BIT(block_header, bit)) {
@@ -1811,7 +1596,7 @@
{
tiny_free_list_t *ptr;
msize_t this_msize;
- grain_t slot = tiny_slot_from_msize(msize);
+ grain_t slot = msize - 1;
free_list_t *free_list = tiny_mag_ptr->mag_free_list;
free_list_t *the_slot = free_list + slot;
tiny_free_list_t *next;
@@ -1841,7 +1626,7 @@
this_msize = msize;
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_free_list(), exact match ptr=%p, this_msize=%d\n", ptr, this_msize);
+ malloc_printf("in tiny_malloc_from_free_list(), exact match ptr=%p, this_msize=%d\n", ptr, this_msize);
}
#endif
goto return_tiny_alloc;
@@ -1860,7 +1645,7 @@
}
slot = BITMAPV_CTZ(bitmap);
- limit = free_list + NUM_TINY_SLOTS;
+ limit = free_list + NUM_TINY_SLOTS - 1;
free_list += slot;
if (free_list < limit) {
@@ -1877,19 +1662,19 @@
goto add_leftover_and_proceed;
}
#if DEBUG_MALLOC
- malloc_report(ASL_LEVEL_ERR, "in tiny_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
+ malloc_printf("in tiny_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
#endif
}
// We are now looking at the last slot, which contains blocks equal to, or
- // due to coalescing of free blocks, larger than NUM_TINY_SLOTS * tiny quantum size.
+ // due to coalescing of free blocks, larger than (NUM_TINY_SLOTS - 1) * tiny quantum size.
// If the last freelist is not empty, and the head contains a block that is
// larger than our request, then the remainder is put back on the free list.
ptr = limit->p;
if (ptr) {
this_msize = get_tiny_free_size(ptr);
next = free_list_unchecksum_ptr(rack, &ptr->next);
- if (this_msize - msize > NUM_TINY_SLOTS) {
+ 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;
@@ -1903,7 +1688,7 @@
set_tiny_meta_header_free(leftover_ptr, leftover_msize);
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO,
+ malloc_printf(
"in tiny_malloc_from_free_list(), last slot ptr=%p, msize=%d this_msize=%d\n", ptr, msize, this_msize);
}
#endif
@@ -1930,7 +1715,7 @@
this_msize = msize;
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_free_list(), from end ptr=%p, msize=%d\n", ptr, msize);
+ malloc_printf("in tiny_malloc_from_free_list(), from end ptr=%p, msize=%d\n", ptr, msize);
}
#endif
goto return_tiny_alloc;
@@ -1948,7 +1733,7 @@
this_msize = msize;
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_free_list(), from start ptr=%p, msize=%d\n", ptr, msize);
+ malloc_printf("in tiny_malloc_from_free_list(), from start ptr=%p, msize=%d\n", ptr, msize);
}
#endif
goto return_tiny_alloc;
@@ -1962,7 +1747,7 @@
leftover_ptr = (tiny_free_list_t *)((unsigned char *)ptr + TINY_BYTES_FOR_MSIZE(msize));
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_free_list(), adding leftover ptr=%p, this_msize=%d\n", ptr, this_msize);
+ malloc_printf("in tiny_malloc_from_free_list(), adding leftover ptr=%p, this_msize=%d\n", ptr, this_msize);
}
#endif
tiny_free_list_add_ptr(rack, tiny_mag_ptr, leftover_ptr, leftover_msize);
@@ -1989,7 +1774,7 @@
}
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_report(ASL_LEVEL_INFO, "in tiny_malloc_from_free_list(), ptr=%p, this_msize=%d, msize=%d\n", ptr, this_msize, msize);
+ malloc_printf("in tiny_malloc_from_free_list(), ptr=%p, this_msize=%d, msize=%d\n", ptr, this_msize, msize);
}
#endif
if (this_msize > 1) {
@@ -2004,19 +1789,19 @@
tiny_malloc_should_clear(rack_t *rack, msize_t msize, boolean_t cleared_requested)
{
void *ptr;
- mag_index_t mag_index = tiny_mag_get_thread_index() % rack->num_magazines;
+ mag_index_t mag_index = 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);
#if DEBUG_MALLOC
if (DEPOT_MAGAZINE_INDEX == mag_index) {
- malloc_zone_error(rack->debug_flags, true, "malloc called for magazine index -1\n");
+ szone_error(rack->debug_flags, 1, "malloc called for magazine index -1", NULL, NULL);
return (NULL);
}
if (!msize) {
- malloc_zone_error(rack->debug_flags, true, "invariant broken (!msize) in allocation (region)\n");
+ szone_error(rack->debug_flags, 1, "invariant broken (!msize) in allocation (region)", NULL, NULL);
return (NULL);
}
#endif
@@ -2026,19 +1811,19 @@
#if CONFIG_TINY_CACHE
ptr = tiny_mag_ptr->mag_last_free;
- if (tiny_mag_ptr->mag_last_free_msize == msize) {
+ if ((((uintptr_t)ptr) & (TINY_QUANTUM - 1)) == msize) {
// we have a winner
tiny_mag_ptr->mag_last_free = NULL;
- tiny_mag_ptr->mag_last_free_msize = 0;
tiny_mag_ptr->mag_last_free_rgn = NULL;
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
+ ptr = (void *)((uintptr_t)ptr & ~(TINY_QUANTUM - 1));
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);
+ malloc_printf("in tiny_malloc_should_clear(), tiny cache ptr=%p, msize=%d\n", ptr, msize);
}
#endif
return ptr;
@@ -2131,14 +1916,14 @@
if (DEPOT_MAGAZINE_INDEX != mag_index) {
magazine_t *tiny_mag_ptr = &rack->magazines[mag_index];
- if (msize < TINY_QUANTUM && ptr == tiny_mag_ptr->mag_last_free) {
+ if (msize < TINY_QUANTUM && ptr == (void *)((uintptr_t)(tiny_mag_ptr->mag_last_free) & ~(TINY_QUANTUM - 1))) {
return 0;
}
} else {
for (mag_index = 0; mag_index < rack->num_magazines; mag_index++) {
magazine_t *tiny_mag_ptr = &(rack->magazines[mag_index]);
- if (msize < TINY_QUANTUM && ptr == tiny_mag_ptr->mag_last_free) {
+ if (msize < TINY_QUANTUM && ptr == (void *)((uintptr_t)(tiny_mag_ptr->mag_last_free) & ~(TINY_QUANTUM - 1))) {
return 0;
}
}
@@ -2157,7 +1942,7 @@
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);
+ szone_error(rack->debug_flags, 1, "double free", ptr, NULL);
}
void
@@ -2182,7 +1967,7 @@
}
#if DEBUG_MALLOC
if (!msize) {
- malloc_report(ASL_LEVEL_ERR, "*** free_tiny() block in use is too large: %p\n", ptr);
+ malloc_printf("*** free_tiny() block in use is too large: %p\n", ptr);
return;
}
#endif
@@ -2194,11 +1979,10 @@
if (DEPOT_MAGAZINE_INDEX != mag_index) {
if (msize < TINY_QUANTUM) { // to see if the bits fit in the last 4 bits
void *ptr2 = tiny_mag_ptr->mag_last_free; // Might be NULL
- msize_t msize2 = tiny_mag_ptr->mag_last_free_msize;
region_t rgn2 = tiny_mag_ptr->mag_last_free_rgn;
/* check that we don't already have this pointer in the cache */
- if (ptr == ptr2) {
+ if (ptr == (void *)((uintptr_t)ptr2 & ~(TINY_QUANTUM - 1))) {
free_tiny_botch(rack, ptr);
return;
}
@@ -2207,8 +1991,7 @@
memset(ptr, SCRABBLE_BYTE, TINY_BYTES_FOR_MSIZE(msize));
}
- tiny_mag_ptr->mag_last_free = ptr;
- tiny_mag_ptr->mag_last_free_msize = msize;
+ tiny_mag_ptr->mag_last_free = (void *)(((uintptr_t)ptr) | msize);
tiny_mag_ptr->mag_last_free_rgn = tiny_region;
if (!ptr2) {
@@ -2217,8 +2000,8 @@
return;
}
- msize = msize2;
- ptr = ptr2;
+ msize = (uintptr_t)ptr2 & (TINY_QUANTUM - 1);
+ ptr = (void *)(((uintptr_t)ptr2) & ~(TINY_QUANTUM - 1));
tiny_region = rgn2;
}
}
@@ -2244,107 +2027,6 @@
CHECK(szone, __PRETTY_FUNCTION__);
}
-
-unsigned
-tiny_batch_malloc(szone_t *szone, size_t size, void **results, unsigned count)
-{
- msize_t msize = TINY_MSIZE_FOR_BYTES(size + TINY_QUANTUM - 1);
- unsigned found = 0;
- 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
- if (!msize) {
- msize = 1;
- }
-
- CHECK(szone, __PRETTY_FUNCTION__);
-
- // We must lock the zone now, since tiny_malloc_from_free_list assumes that
- // the caller has done so.
- SZONE_MAGAZINE_PTR_LOCK(tiny_mag_ptr);
-
- // with the zone locked, allocate objects from the free list until all
- // sufficiently large objects have been exhausted, or we have met our quota
- // of objects to allocate.
- while (found < count) {
- void *ptr = tiny_malloc_from_free_list(&szone->tiny_rack, tiny_mag_ptr, mag_index, msize);
- if (!ptr) {
- break;
- }
-
- *results++ = ptr;
- found++;
- }
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- return found;
-}
-
-void
-tiny_batch_free(szone_t *szone, void **to_be_freed, unsigned count)
-{
- unsigned cc = 0;
- void *ptr;
- region_t tiny_region = NULL;
- boolean_t is_free;
- msize_t msize;
- magazine_t *tiny_mag_ptr = NULL;
- mag_index_t mag_index = -1;
-
- // frees all the pointers in to_be_freed
- // note that to_be_freed may be overwritten during the process
- if (!count) {
- return;
- }
-
- CHECK(szone, __PRETTY_FUNCTION__);
- while (cc < count) {
- ptr = to_be_freed[cc];
- if (ptr) {
- if (NULL == tiny_region || tiny_region != TINY_REGION_FOR_PTR(ptr)) { // region same as last iteration?
- if (tiny_mag_ptr) { // non-NULL iff magazine lock taken
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- tiny_mag_ptr = NULL;
- }
-
- tiny_region = tiny_region_for_ptr_no_lock(&szone->tiny_rack, ptr);
-
- if (tiny_region) {
- tiny_mag_ptr = mag_lock_zine_for_region_trailer(szone->tiny_rack.magazines,
- REGION_TRAILER_FOR_TINY_REGION(tiny_region),
- MAGAZINE_INDEX_FOR_TINY_REGION(tiny_region));
- mag_index = MAGAZINE_INDEX_FOR_TINY_REGION(tiny_region);
- }
- }
- if (tiny_region) {
- // this is a tiny pointer
- if (TINY_INDEX_FOR_PTR(ptr) >= NUM_TINY_BLOCKS) {
- break; // pointer to metadata; let the standard free deal with it
- }
- msize = get_tiny_meta_header(ptr, &is_free);
- if (is_free) {
- break; // a double free; let the standard free deal with it
- }
- if (!tiny_free_no_lock(&szone->tiny_rack, tiny_mag_ptr, mag_index, tiny_region, ptr, msize)) {
- // Arrange to re-acquire magazine lock
- tiny_mag_ptr = NULL;
- tiny_region = NULL;
- }
- to_be_freed[cc] = NULL;
- } else {
- // No region in this zone claims ptr; let the standard free deal with it
- break;
- }
- }
- cc++;
- }
-
- if (tiny_mag_ptr) {
- SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
- tiny_mag_ptr = NULL;
- }
-}
-
void
print_tiny_free_list(rack_t *rack)
@@ -2361,14 +2043,14 @@
while (slot < NUM_TINY_SLOTS) {
ptr = rack->magazines[mag_index].mag_free_list[slot].p;
if (ptr) {
- _simple_sprintf(b, "%s%y[%d]; ", (slot == NUM_TINY_SLOTS) ? ">=" : "", (slot + 1) * TINY_QUANTUM,
+ _simple_sprintf(b, "%s%y[%d]; ", (slot == NUM_TINY_SLOTS - 1) ? ">=" : "", (slot + 1) * TINY_QUANTUM,
free_list_count(rack, (free_list_t){ .p = ptr }));
}
slot++;
}
_simple_sappend(b, "\n");
}
- malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
+ _malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
_simple_sfree(b);
}
}
@@ -2390,7 +2072,7 @@
if (region == HASHRING_REGION_DEALLOCATED) {
if ((b = _simple_salloc()) != NULL) {
_simple_sprintf(b, "Tiny region [unknown address] was returned to the OS\n");
- malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
+ _malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
_simple_sfree(b);
}
return;
@@ -2410,13 +2092,13 @@
break;
}
if (!msize) {
- malloc_report(ASL_LEVEL_ERR, "*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
+ malloc_printf("*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
break;
}
if (!is_free) {
// block in use
if (msize > NUM_TINY_SLOTS) {
- malloc_report(ASL_LEVEL_ERR, "*** error at %p msize for in_use is %d\n", (void *)current, msize);
+ malloc_printf("*** error at %p msize for in_use is %d\n", (void *)current, msize);
}
if (msize < 1024) {
counts[msize]++;
@@ -2452,19 +2134,13 @@
}
}
}
- malloc_report(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, "%s\n", _simple_string(b));
+ _malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
_simple_sfree(b);
}
}
-static char *tiny_freelist_fail_msg = "check: tiny free list incorrect ";
-
-#define TINY_FREELIST_FAIL(fmt, ...) \
- malloc_zone_check_fail(tiny_freelist_fail_msg, \
- " (slot=%u), counter=%d\n" fmt, slot, counter, __VA_ARGS__);
-
boolean_t
-tiny_free_list_check(rack_t *rack, grain_t slot, unsigned counter)
+tiny_free_list_check(rack_t *rack, grain_t slot)
{
mag_index_t mag_index;
@@ -2480,22 +2156,22 @@
while (ptr) {
is_free = tiny_meta_header_is_free(ptr);
if (!is_free) {
- TINY_FREELIST_FAIL("*** in-use ptr in free list slot=%u count=%d ptr=%p\n", slot, count, ptr);
+ malloc_printf("*** in-use ptr in free list slot=%d count=%d ptr=%p\n", slot, count, ptr);
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
return 0;
}
if (((uintptr_t)ptr) & (TINY_QUANTUM - 1)) {
- TINY_FREELIST_FAIL("*** unaligned ptr in free list slot=%u count=%d ptr=%p\n", slot, count, ptr);
+ malloc_printf("*** unaligned ptr in free list slot=%d count=%d ptr=%p\n", slot, count, ptr);
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
return 0;
}
if (!tiny_region_for_ptr_no_lock(rack, ptr)) {
- TINY_FREELIST_FAIL("*** ptr not in szone slot=%d count=%u ptr=%p\n", slot, count, ptr);
+ malloc_printf("*** ptr not in szone slot=%d count=%d ptr=%p\n", slot, count, ptr);
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
return 0;
}
if (free_list_unchecksum_ptr(rack, &ptr->previous) != previous) {
- TINY_FREELIST_FAIL("*** previous incorrectly set slot=%u count=%d ptr=%p\n", slot, count, ptr);
+ malloc_printf("*** previous incorrectly set slot=%d count=%d ptr=%p\n", slot, count, ptr);
SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr);
return 0;
}