Loading...
--- libmalloc/libmalloc-116.50.8/src/magazine_small.c
+++ libmalloc/libmalloc-283/src/magazine_small.c
@@ -23,6 +23,7 @@
#include "internal.h"
+
/********************* SMALL FREE LIST UTILITIES ************************/
#pragma mark meta header helpers
@@ -38,6 +39,15 @@
}
/*
+ * Mark a block as not free, preserving its size.
+ */
+static MALLOC_INLINE void
+small_meta_header_set_not_free(msize_t *meta_headers, msize_t index)
+{
+ meta_headers[index] &= ~SMALL_IS_FREE;
+}
+
+/*
* Mark a block as in use. Only the first quantum of a block is marked thusly,
* the remainder are marked "middle".
*/
@@ -56,21 +66,40 @@
meta_headers[index] = 0;
}
+static MALLOC_INLINE MALLOC_ALWAYS_INLINE
+mag_index_t
+small_mag_get_thread_index(void)
+{
+#if CONFIG_SMALL_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
+}
+
#pragma mark in-place free list
static MALLOC_INLINE void
-small_inplace_checksum_ptr(szone_t *szone, inplace_linkage_s *linkage, void *ptr)
-{
- uintptr_t checksum = free_list_gen_checksum((uintptr_t)ptr ^ szone->cookie ^ (uintptr_t)szone);
+small_inplace_checksum_ptr(rack_t *rack, inplace_linkage_s *linkage, void *ptr)
+{
+ uintptr_t checksum = free_list_gen_checksum((uintptr_t)ptr ^ rack->cookie ^ (uintptr_t)rack);
linkage->checksum = checksum;
linkage->ptr = ptr;
}
static MALLOC_INLINE free_list_t
-small_inplace_unchecksum_ptr(szone_t *szone, inplace_linkage_s *linkage)
-{
- if (linkage->checksum != free_list_gen_checksum((uintptr_t)linkage->ptr ^ szone->cookie ^ (uintptr_t)szone)) {
- free_list_checksum_botch(szone, linkage);
+small_inplace_unchecksum_ptr(rack_t *rack, inplace_linkage_s *linkage)
+{
+ if (linkage->checksum != (uint8_t)free_list_gen_checksum((uintptr_t)linkage->ptr ^ rack->cookie ^ (uintptr_t)rack)) {
+ free_list_checksum_botch(rack, linkage, linkage->ptr);
__builtin_trap();
}
@@ -78,27 +107,63 @@
}
static MALLOC_INLINE free_list_t
-small_inplace_free_entry_get_previous(szone_t *szone, small_inplace_free_entry_t ptr)
-{
- return small_inplace_unchecksum_ptr(szone, &ptr->previous);
+small_inplace_unchecksum_ptr_task(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, rack_t *rack, inplace_linkage_s *linkage)
+{
+ inplace_linkage_s *mapped_linkage;
+ rack_t *mapped_rack;
+ if (reader(task, (vm_address_t)linkage, sizeof(*linkage),
+ (void **)&mapped_linkage)) {
+ printer("Unable to map small linkage pointer %p\n", linkage);
+ return (free_list_t){ .p = NULL };
+ }
+
+ if (reader(task, (vm_address_t)rack,
+ sizeof(struct rack_s), (void **)&mapped_rack)) {
+ printer("Failed to map small rack\n");
+ return (free_list_t){ .p = NULL };
+ }
+
+ if (mapped_linkage->checksum != (uint8_t)free_list_gen_checksum(
+ (uintptr_t)mapped_linkage->ptr ^ mapped_rack->cookie ^ (uintptr_t)rack)) {
+ free_list_checksum_botch(rack, linkage, mapped_linkage->ptr);
+ __builtin_trap();
+ }
+
+ return (free_list_t){ .p = mapped_linkage->ptr };
+}
+
+static MALLOC_INLINE free_list_t
+small_inplace_free_entry_get_previous(rack_t *rack, small_inplace_free_entry_t ptr)
+{
+ return small_inplace_unchecksum_ptr(rack, &ptr->previous);
}
static MALLOC_INLINE void
-small_inplace_free_entry_set_previous(szone_t *szone, small_inplace_free_entry_t entry, free_list_t previous)
-{
- small_inplace_checksum_ptr(szone, &entry->previous, previous.p);
+small_inplace_free_entry_set_previous(rack_t *rack, small_inplace_free_entry_t entry, free_list_t previous)
+{
+ small_inplace_checksum_ptr(rack, &entry->previous, previous.p);
}
static MALLOC_INLINE free_list_t
-small_inplace_free_entry_get_next(szone_t *szone, small_inplace_free_entry_t ptr)
-{
- return small_inplace_unchecksum_ptr(szone, &ptr->next);
+small_inplace_free_entry_get_next(rack_t *rack, small_inplace_free_entry_t ptr)
+{
+ return small_inplace_unchecksum_ptr(rack, &ptr->next);
+}
+
+static MALLOC_INLINE free_list_t
+small_inplace_free_entry_get_next_task(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, rack_t *rack,
+ small_inplace_free_entry_t ptr)
+{
+ return small_inplace_unchecksum_ptr_task(task, reader, printer, rack,
+ &ptr->next);
}
static MALLOC_INLINE void
-small_inplace_free_entry_set_next(szone_t *szone, small_inplace_free_entry_t entry, free_list_t next)
-{
- small_inplace_checksum_ptr(szone, &entry->next, next.p);
+small_inplace_free_entry_set_next(rack_t *rack, small_inplace_free_entry_t entry, free_list_t next)
+{
+ small_inplace_checksum_ptr(rack, &entry->next, next.p);
}
#pragma mark OOB free list
@@ -145,21 +210,60 @@
return (free_list_t){ .p = (void *)oobe->next };
}
+static MALLOC_INLINE free_list_t
+small_oob_free_entry_get_next_task(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, oob_free_entry_t oobe)
+{
+ oob_free_entry_t mapped_oobe;
+ if (reader(task, (vm_address_t)oobe, sizeof(*oobe),
+ (void **)&mapped_oobe)) {
+ printer("Failed to map small oobe pointer\n");
+ return (free_list_t){ .p = NULL };
+ }
+ return (free_list_t){ .p = (void *)mapped_oobe->next };
+}
+
static MALLOC_INLINE void *
small_oob_free_entry_get_ptr(oob_free_entry_t oobe)
{
+ if (!(oobe->ptr & SMALL_IS_OOB)) {
+ return NULL;
+ }
small_region_t region = SMALL_REGION_FOR_PTR(oobe);
uint16_t block = oobe->ptr & ~SMALL_IS_OOB;
return (void *)((uintptr_t)region + (block << SHIFT_SMALL_QUANTUM));
}
static MALLOC_INLINE void *
+small_oob_free_entry_get_ptr_task(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, oob_free_entry_t oobe)
+{
+ // We need to map the oob_free_entry_t to read the pointer value.
+ oob_free_entry_t mapped_oobe;
+ if (reader(task, (vm_address_t)oobe, sizeof(*oobe),
+ (void **)&mapped_oobe)) {
+ printer("Failed to map small oobe pointer\n");
+ return NULL;
+ }
+
+ if (!(mapped_oobe->ptr & SMALL_IS_OOB)) {
+ return NULL;
+ }
+
+ // The rest of this code works with target process addresses and returns an
+ // address in the target process.
+ small_region_t region = SMALL_REGION_FOR_PTR(oobe);
+ uint16_t block = mapped_oobe->ptr & ~SMALL_IS_OOB;
+ return (void *)((uintptr_t)region + (block << SHIFT_SMALL_QUANTUM));
+}
+
+static MALLOC_INLINE void
small_oob_free_entry_set_ptr(oob_free_entry_t oobe, void *ptr)
{
oobe->ptr = SMALL_IS_OOB | (SMALL_OFFSET_FOR_PTR(ptr) >> SHIFT_SMALL_QUANTUM);
}
-static MALLOC_INLINE void *
+static MALLOC_INLINE void
small_oob_free_entry_set_free(oob_free_entry_t oobe)
{
oobe->prev = ~0;
@@ -184,7 +288,7 @@
}
#if DEBUG_MALLOC
- malloc_printf("used all slots of OOB entries\n");
+ malloc_report(ASL_LEVEL_INFO, "used all slots of OOB entries\n");
#endif
return NULL;
}
@@ -198,7 +302,9 @@
// If this changes, then a linear search through the list may
// become an unsuitable choice.
for (int i=0; i < SMALL_OOB_COUNT; i++) {
- if (small_oob_free_entry_get_ptr(®ion->small_oob_free_entries[i]) == ptr) {
+ oob_free_entry_t oob = ®ion->small_oob_free_entries[i];
+ if (small_oob_free_entry_get_ptr(oob) == ptr &&
+ oob->ptr & SMALL_IS_OOB) {
return ®ion->small_oob_free_entries[i];
}
}
@@ -209,49 +315,62 @@
#pragma mark generic free list
static MALLOC_INLINE void
-small_free_list_set_previous(szone_t *szone, free_list_t entry, free_list_t previous)
+small_free_list_set_previous(rack_t *rack, free_list_t entry, free_list_t previous)
{
if (small_is_oob_free_entry(entry)) {
small_oob_free_entry_set_previous(entry.oob, previous);
} else {
- small_inplace_free_entry_set_previous(szone, entry.small_inplace, previous);
+ small_inplace_free_entry_set_previous(rack, entry.small_inplace, previous);
}
}
static MALLOC_INLINE free_list_t
-small_free_list_get_previous(szone_t *szone, free_list_t ptr)
+small_free_list_get_previous(rack_t *rack, free_list_t ptr)
{
MALLOC_ASSERT(ptr.p);
if (small_is_oob_free_entry(ptr)) {
return small_oob_free_entry_get_previous(ptr.oob);
} else {
- return small_inplace_free_entry_get_previous(szone, ptr.small_inplace);
+ return small_inplace_free_entry_get_previous(rack, ptr.small_inplace);
}
}
static MALLOC_INLINE void
-small_free_list_set_next(szone_t *szone, free_list_t entry, free_list_t next)
+small_free_list_set_next(rack_t *rack, free_list_t entry, free_list_t next)
{
if (small_is_oob_free_entry(entry)) {
small_oob_free_entry_set_next(entry.oob, next);
} else {
- small_inplace_free_entry_set_next(szone, entry.small_inplace, next);
+ small_inplace_free_entry_set_next(rack, entry.small_inplace, next);
}
}
static MALLOC_INLINE free_list_t
-small_free_list_get_next(szone_t *szone, free_list_t ptr)
+small_free_list_get_next(rack_t *rack, free_list_t ptr)
{
MALLOC_ASSERT(ptr.p);
if (small_is_oob_free_entry(ptr)) {
return small_oob_free_entry_get_next(ptr.oob);
} else {
- return small_inplace_free_entry_get_next(szone, ptr.small_inplace);
+ return small_inplace_free_entry_get_next(rack, ptr.small_inplace);
+ }
+}
+
+static MALLOC_INLINE free_list_t
+small_free_list_get_next_task(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, rack_t *rack, free_list_t ptr)
+{
+ MALLOC_ASSERT(ptr.p);
+ if (small_is_oob_free_entry(ptr)) {
+ return small_oob_free_entry_get_next_task(task, reader, printer, ptr.oob);
+ } else {
+ return small_inplace_free_entry_get_next_task(task, reader, printer,
+ rack, ptr.small_inplace);
}
}
static MALLOC_INLINE void *
-small_free_list_get_ptr(szone_t *szone, free_list_t ptr)
+small_free_list_get_ptr(free_list_t ptr)
{
if (!ptr.p) {
return NULL;
@@ -262,10 +381,23 @@
}
}
+static MALLOC_INLINE void *
+small_free_list_get_ptr_task(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, free_list_t ptr)
+{
+ if (!ptr.p) {
+ return NULL;
+ } else if (small_is_oob_free_entry(ptr)) {
+ return small_oob_free_entry_get_ptr_task(task, reader, printer, ptr.oob);
+ } else {
+ return (void *)ptr.p;
+ }
+}
+
// Returns a free_list_t that is either inline or not based on the
// pointer and msize.
static MALLOC_INLINE free_list_t
-small_free_list_from_ptr(szone_t *szone, void *ptr, msize_t msize)
+small_free_list_from_ptr(rack_t *rack, void *ptr, msize_t msize)
{
MALLOC_ASSERT(msize);
@@ -289,10 +421,10 @@
}
static MALLOC_INLINE void
-small_free_mark_free(szone_t *szone, free_list_t entry, msize_t msize)
+small_free_mark_free(rack_t *rack, free_list_t entry, msize_t msize)
{
// Marks both the start and end block of a free-list entry as free.
- void *ptr = small_free_list_get_ptr(szone, entry);
+ void *ptr = small_free_list_get_ptr(entry);
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
uintptr_t start_index = SMALL_META_INDEX_FOR_PTR(ptr);
uintptr_t end_index = SMALL_META_INDEX_FOR_PTR(ptr + SMALL_BYTES_FOR_MSIZE(msize) - 1);
@@ -303,26 +435,42 @@
}
static MALLOC_INLINE void
-small_free_mark_unfree(szone_t *szone, free_list_t entry, msize_t msize)
+small_free_mark_middle(rack_t *rack, free_list_t entry, msize_t msize)
{
// Marks both the start and end block of a free-list entry as "middle" (unfree).
- void *ptr = small_free_list_get_ptr(szone, entry);
+ void *ptr = small_free_list_get_ptr(entry);
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
uintptr_t start_index = SMALL_META_INDEX_FOR_PTR(ptr);
uintptr_t end_index = SMALL_META_INDEX_FOR_PTR(ptr + SMALL_BYTES_FOR_MSIZE(msize) - 1);
MALLOC_ASSERT(start_index <= end_index);
+ MALLOC_ASSERT((meta_headers[start_index] & ~SMALL_IS_FREE) == msize);
small_meta_header_set_middle(meta_headers, start_index);
small_meta_header_set_middle(meta_headers, end_index);
}
+static MALLOC_INLINE void
+small_free_mark_unfree(rack_t *rack, free_list_t entry, msize_t msize)
+{
+ // Marks both the start and end block of a free-list entry as not free.
+ void *ptr = small_free_list_get_ptr(entry);
+ msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
+ uintptr_t start_index = SMALL_META_INDEX_FOR_PTR(ptr);
+ uintptr_t end_index = SMALL_META_INDEX_FOR_PTR(ptr + SMALL_BYTES_FOR_MSIZE(msize) - 1);
+ MALLOC_ASSERT(start_index <= end_index);
+
+ small_meta_header_set_not_free(meta_headers, start_index);
+ small_meta_header_set_not_free(meta_headers, end_index);
+}
+
static MALLOC_INLINE unsigned int
-small_free_list_count(szone_t *szone, free_list_t ptr)
+small_free_list_count(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, rack_t *rack, free_list_t ptr)
{
unsigned int count = 0;
while (ptr.p) {
count++;
- ptr = small_free_list_get_next(szone, ptr);
+ ptr = small_free_list_get_next_task(task, reader, printer, rack, ptr);
}
return count;
}
@@ -333,43 +481,43 @@
* Assumes szone has been locked
*/
static free_list_t
-small_free_list_add_ptr(szone_t *szone, magazine_t *small_mag_ptr, void *ptr, msize_t msize)
-{
- grain_t slot = (msize <= szone->num_small_slots) ? msize - 1 : szone->num_small_slots - 1;
+small_free_list_add_ptr(rack_t *rack, magazine_t *small_mag_ptr, void *ptr, msize_t msize)
+{
+ grain_t slot = SMALL_FREE_SLOT_FOR_MSIZE(rack, msize);
free_list_t free_head = small_mag_ptr->mag_free_list[slot];
// This will either return the free_list_t for the current pointer, or attempt
// to reserve an OOB entry for us.
- free_list_t free_ptr = small_free_list_from_ptr(szone, ptr, msize);
+ free_list_t free_ptr = small_free_list_from_ptr(rack, ptr, msize);
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_printf("in %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
+ malloc_report(ASL_LEVEL_INFO, "in %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
}
if (((uintptr_t)ptr) & (SMALL_QUANTUM - 1)) {
- szone_error(szone, 1, "small_free_list_add_ptr: Unaligned ptr", ptr, NULL);
- }
-#endif
-
- small_free_list_set_previous(szone, free_ptr, (free_list_t){ .p = NULL });
- small_free_list_set_next(szone, free_ptr, free_head);
+ malloc_zone_error(rack->debug_flags, true, "small_free_list_add_ptr: Unaligned ptr %p\n", ptr);
+ }
+#endif
+
+ small_free_list_set_previous(rack, free_ptr, (free_list_t){ .p = NULL });
+ small_free_list_set_next(rack, free_ptr, free_head);
// Set the start and end blocks of the meta header as "free". Marking the last block
// allows coalescing the regions when we free adjacent regions.
- small_free_mark_free(szone, free_ptr, msize);
-
- if (small_free_list_get_ptr(szone, free_head)) {
+ small_free_mark_free(rack, free_ptr, msize);
+
+ if (small_free_list_get_ptr(free_head)) {
#if DEBUG_MALLOC
if (small_free_list_get_previous(szone, free_head)) {
- szone_error(szone, 1, "small_free_list_add_ptr: Internal invariant broken (free_head->previous != NULL)", ptr,
+ malloc_zone_error(rack->debug_flags, true, "small_free_list_add_ptr: Internal invariant broken (free_head->previous != NULL)\n"
"ptr=%p slot=%d free_head=%p previous=%p\n", ptr, slot, (void *)free_head, free_head->previous.p);
}
- if (!SMALL_PTR_IS_FREE(small_free_list_get_ptr(szone, free_head))) {
- szone_error(szone, 1, "small_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 *)small_free_list_get_ptr(szone, free_head));
- }
-#endif
- small_free_list_set_previous(szone, free_head, free_ptr);
+ if (!SMALL_PTR_IS_FREE(small_free_list_get_ptr(free_head))) {
+ malloc_zone_error(rack->debug_flags, true, "small_free_list_add_ptr: Internal invariant broken (free_head is not a free pointer)\n"
+ "ptr=%p slot=%d free_head=%p\n", ptr, slot, (void *)small_free_list_get_ptr(free_head));
+ }
+#endif
+ small_free_list_set_previous(rack, free_head, free_ptr);
} else {
BITMAPN_SET(small_mag_ptr->mag_bitmap, slot);
}
@@ -383,40 +531,57 @@
* Assumes szone has been locked
*/
static void
-small_free_list_remove_ptr_no_clear(szone_t *szone, magazine_t *small_mag_ptr, free_list_t entry, msize_t msize)
-{
- grain_t slot = (msize <= szone->num_small_slots) ? msize - 1 : szone->num_small_slots - 1;
+small_free_list_remove_ptr_no_clear(rack_t *rack, magazine_t *small_mag_ptr, free_list_t entry, msize_t msize)
+{
+ grain_t slot = SMALL_FREE_SLOT_FOR_MSIZE(rack, msize);
free_list_t next, previous;
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_printf("In %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
- }
-#endif
-
- previous = small_free_list_get_previous(szone, entry);
- next = small_free_list_get_next(szone, entry);
-
- if (!small_free_list_get_ptr(szone, previous)) {
+ malloc_report(ASL_LEVEL_INFO, "In %s, ptr=%p, msize=%d\n", __FUNCTION__, ptr, msize);
+ }
+#endif
+
+ previous = small_free_list_get_previous(rack, entry);
+ next = small_free_list_get_next(rack, entry);
+
+ if (!small_free_list_get_ptr(previous)) {
// The block to remove is the head of the free list
#if DEBUG_MALLOC
if (small_mag_ptr->mag_free_list[slot] != ptr) {
- szone_error(szone, 1, "small_free_list_remove_ptr: Internal invariant broken (small_mag_ptr->mag_free_list[slot])", ptr,
- "ptr=%p slot=%d msize=%d small_mag_ptr->mag_free_list[slot]=%p\n", ptr, slot, msize,
- (void *)small_mag_ptr->mag_free_list[slot]);
+ malloc_zone_error(rack->debug_flags, true,
+ "small_free_list_remove_ptr_no_clear: Internal invariant broken (small_mag_ptr->mag_free_list[slot])\n"
+ "ptr=%p slot=%d msize=%d small_mag_ptr->mag_free_list[slot]=%p\n", ptr, slot, msize,
+ (void *)small_mag_ptr->mag_free_list[slot]);
return;
}
#endif
small_mag_ptr->mag_free_list[slot] = next;
- if (!small_free_list_get_ptr(szone, next)) {
+ if (!small_free_list_get_ptr(next)) {
BITMAPN_CLR(small_mag_ptr->mag_bitmap, slot);
}
} else {
- small_free_list_set_next(szone, previous, next);
- }
-
- if (small_free_list_get_ptr(szone, next)) {
- small_free_list_set_previous(szone, next, previous);
+ // Check that the next pointer of "previous" points to "entry".
+ free_list_t prev_next = small_free_list_get_next(rack, previous);
+ if (small_free_list_get_ptr(prev_next) != small_free_list_get_ptr(entry)) {
+ malloc_zone_error(rack->debug_flags, true,
+ "small_free_list_remove_ptr_no_clear: Internal invariant broken (next ptr of prev) for %p, prev_next=%p\n",
+ small_free_list_get_ptr(entry), small_free_list_get_ptr(prev_next));
+ __builtin_unreachable(); // Always crashes in malloc_zone_error().
+ }
+ small_free_list_set_next(rack, previous, next);
+ }
+
+ if (small_free_list_get_ptr(next)) {
+ // Check that the previous pointer of "next" points to "entry".
+ free_list_t next_prev = small_free_list_get_previous(rack, next);
+ if (small_free_list_get_ptr(next_prev) != small_free_list_get_ptr(entry)) {
+ malloc_zone_error(rack->debug_flags, true,
+ "small_free_list_remove_ptr_no_clear: Internal invariant broken (prev ptr of next) for %p, next_prev=%p\n",
+ small_free_list_get_ptr(entry), small_free_list_get_ptr(next_prev));
+ __builtin_unreachable(); // Always crashes in malloc_zone_error().
+ }
+ small_free_list_set_previous(rack, next, previous);
}
if (small_is_oob_free_entry(entry)) {
@@ -425,22 +590,22 @@
}
static void
-small_free_list_remove_ptr(szone_t *szone, magazine_t *small_mag_ptr, free_list_t entry, msize_t msize)
+small_free_list_remove_ptr(rack_t *rack, magazine_t *small_mag_ptr, free_list_t entry, msize_t msize)
{
// In the general case we want to ensure we marked these entries as "middle"
// while we are in this function. However, when we're moving free list entries
// from/to the recirc depot we rely on the metadata bits being intact to
// reconstruct the free list. In that case we have to be able to skip this
// metadata manipulation.
- small_free_mark_unfree(szone, entry, msize);
- small_free_list_remove_ptr_no_clear(szone, small_mag_ptr, entry, msize);
+ small_free_mark_middle(rack, entry, msize);
+ small_free_list_remove_ptr_no_clear(rack, small_mag_ptr, entry, msize);
}
// Find a free list entry by its pointer address. This should only really be used
// by small_finalize_region, or similar, where the free_list_t entry of a known
// pointer is desired. Otherwise it is cheaper to always pull off the free lists.
static free_list_t
-small_free_list_find_by_ptr(szone_t *szone, magazine_t *small_mag_ptr, void *ptr, msize_t msize)
+small_free_list_find_by_ptr(rack_t *rack, magazine_t *small_mag_ptr, void *ptr, msize_t msize)
{
if (*SMALL_METADATA_FOR_PTR(ptr) == (SMALL_IS_FREE | msize)) {
// If the block is marked free, and of size `msize`, then we first must check
@@ -462,12 +627,14 @@
return (free_list_t){ .p = ptr };
}
- szone_error(szone, 1, "small_free_list_find_by_ptr: ptr is not free (ptr metadata !SMALL_IS_FREE)", ptr,
- "ptr=%p msize=%d metadata=0x%x", ptr, msize, *SMALL_METADATA_FOR_PTR(ptr));
+ malloc_zone_error(rack->debug_flags, true,
+ "small_free_list_find_by_ptr: ptr is not free (ptr metadata !SMALL_IS_FREE), "
+ "ptr=%p msize=%d metadata=0x%x\n", ptr, msize, *SMALL_METADATA_FOR_PTR(ptr));
+ __builtin_trap();
}
void
-small_finalize_region(szone_t *szone, magazine_t *small_mag_ptr)
+small_finalize_region(rack_t *rack, magazine_t *small_mag_ptr)
{
void *last_block, *previous_block;
msize_t last_msize, previous_msize, last_index;
@@ -499,15 +666,15 @@
previous_block = (void *)((uintptr_t)last_block - SMALL_BYTES_FOR_MSIZE(previous_msize));
if (SMALL_PTR_IS_FREE(previous_block)) {
- previous = small_free_list_find_by_ptr(szone, small_mag_ptr, previous_block, previous_msize);
- small_free_list_remove_ptr(szone, small_mag_ptr, previous, previous_msize);
+ previous = small_free_list_find_by_ptr(rack, small_mag_ptr, previous_block, previous_msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, previous, previous_msize);
last_block = previous_block;
last_msize += previous_msize;
}
}
// splice last_block into the free list
- small_free_list_add_ptr(szone, small_mag_ptr, last_block, last_msize);
+ small_free_list_add_ptr(rack, small_mag_ptr, last_block, last_msize);
small_mag_ptr->mag_bytes_free_at_end = 0;
}
@@ -521,13 +688,13 @@
void *next_block = (void *)((uintptr_t)last_block + small_mag_ptr->mag_bytes_free_at_start);
if (SMALL_PTR_IS_FREE(next_block)) {
msize_t next_msize = SMALL_PTR_SIZE(next_block);
- next = small_free_list_find_by_ptr(szone, small_mag_ptr, next_block, next_msize);
- small_free_list_remove_ptr(szone, small_mag_ptr, next, next_msize);
+ next = small_free_list_find_by_ptr(rack, small_mag_ptr, next_block, next_msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, next, next_msize);
last_msize += next_msize;
}
// splice last_block into the free list
- small_free_list_add_ptr(szone, small_mag_ptr, last_block, last_msize);
+ small_free_list_add_ptr(rack, small_mag_ptr, last_block, last_msize);
small_mag_ptr->mag_bytes_free_at_start = 0;
}
#endif
@@ -537,7 +704,7 @@
}
int
-small_free_detach_region(szone_t *szone, magazine_t *small_mag_ptr, region_t r)
+small_free_detach_region(rack_t *rack, magazine_t *small_mag_ptr, region_t r)
{
unsigned char *ptr = SMALL_REGION_ADDRESS(r);
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
@@ -555,14 +722,14 @@
if (!msize) {
#if DEBUG_MALLOC
boolean_t is_free = msize_and_free & SMALL_IS_FREE;
- malloc_printf("*** small_free_detach_region error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
+ malloc_report(ASL_LEVEL_ERR, "*** small_free_detach_region error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
#endif
break;
}
if (is_free) {
- free_list_t entry = small_free_list_find_by_ptr(szone, small_mag_ptr, (void *)current, msize);
- small_free_list_remove_ptr_no_clear(szone, small_mag_ptr, entry, msize);
+ free_list_t entry = small_free_list_find_by_ptr(rack, small_mag_ptr, (void *)current, msize);
+ small_free_list_remove_ptr_no_clear(rack, small_mag_ptr, entry, msize);
} else {
total_alloc++;
}
@@ -572,7 +739,7 @@
}
size_t
-small_free_reattach_region(szone_t *szone, magazine_t *small_mag_ptr, region_t r)
+small_free_reattach_region(rack_t *rack, magazine_t *small_mag_ptr, region_t r)
{
unsigned char *ptr = SMALL_REGION_ADDRESS(r);
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
@@ -589,12 +756,12 @@
if (!msize) {
#if DEBUG_MALLOC
- malloc_printf("*** small_free_reattach_region error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
+ malloc_report(ASL_LEVEL_ERR, "*** small_free_reattach_region error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
#endif
break;
}
if (is_free) {
- small_free_list_add_ptr(szone, small_mag_ptr, (void *)current, msize);
+ small_free_list_add_ptr(rack, small_mag_ptr, (void *)current, msize);
} else {
total_alloc += SMALL_BYTES_FOR_MSIZE(msize);
}
@@ -608,7 +775,7 @@
} small_pg_pair_t;
void
-small_free_scan_madvise_free(szone_t *szone, magazine_t *depot_ptr, region_t r)
+small_free_scan_madvise_free(rack_t *rack, magazine_t *depot_ptr, region_t r)
{
uintptr_t start = (uintptr_t)SMALL_REGION_ADDRESS(r);
uintptr_t current = start;
@@ -629,7 +796,7 @@
if (is_free && !msize && (current == start)) {
#if DEBUG_MALLOC
// first block is all free
- malloc_printf("*** small_free_scan_madvise_free first block is all free! %p: msize=%d is_free =%d\n", (void *)current,
+ malloc_report(ASL_LEVEL_ERR, "*** small_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(free_list_t) + sizeof(msize_t));
@@ -644,8 +811,8 @@
}
if (!msize) {
#if DEBUG_MALLOC
- malloc_printf(
- "*** small_free_scan_madvise_free error with %p: msize=%d is_free =%d\n", (void *)current, msize, is_free);
+ malloc_report(ASL_LEVEL_ERR,
+ "*** small_free_scan_madvise_free error with %p: msize=%d is_free=%d\n", (void *)current, msize, is_free);
#endif
break;
}
@@ -666,23 +833,24 @@
int i;
OSAtomicIncrement32Barrier(&(REGION_TRAILER_FOR_SMALL_REGION(r)->pinned_to_depot));
- SZONE_MAGAZINE_PTR_UNLOCK(szone, depot_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
for (i = 0; i < advisories; ++i) {
uintptr_t addr = (advisory[i].pnum << vm_page_quanta_shift) + start;
size_t size = advisory[i].size << vm_page_quanta_shift;
- madvise_free_range(szone, r, addr, addr + size, NULL);
- }
- SZONE_MAGAZINE_PTR_LOCK(szone, depot_ptr);
+ mvm_madvise_free(rack, r, addr, addr + size, NULL, rack->debug_flags & MALLOC_DO_SCRIBBLE);
+ }
+ SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
OSAtomicDecrement32Barrier(&(REGION_TRAILER_FOR_SMALL_REGION(r)->pinned_to_depot));
}
}
+#if CONFIG_RECIRC_DEPOT
static region_t
-small_find_msize_region(szone_t *szone, magazine_t *small_mag_ptr, mag_index_t mag_index, msize_t msize)
+small_find_msize_region(rack_t *rack, magazine_t *small_mag_ptr, mag_index_t mag_index, msize_t msize)
{
void *ptr;
- grain_t slot = (msize <= szone->num_small_slots) ? msize - 1 : szone->num_small_slots - 1;
+ grain_t slot = SMALL_FREE_SLOT_FOR_MSIZE(rack, msize);
free_list_t *free_list = small_mag_ptr->mag_free_list;
free_list_t *the_slot = free_list + slot;
free_list_t *limit;
@@ -692,59 +860,52 @@
CHECK_MAGAZINE_PTR_LOCKED(szone, small_mag_ptr, __PRETTY_FUNCTION__);
// Look for an exact match by checking the freelist for this msize.
- ptr = small_free_list_get_ptr(szone, *the_slot);
+ ptr = small_free_list_get_ptr(*the_slot);
if (ptr) {
return SMALL_REGION_FOR_PTR(ptr);
}
// Mask off the bits representing slots holding free blocks smaller than
// the size we need.
- if (szone->is_largemem) {
- // BITMAPN_CTZ implementation
- unsigned idx = slot >> 5;
- bitmap = 0;
- unsigned mask = ~((1 << (slot & 31)) - 1);
- for (; idx < SMALL_BITMAP_WORDS; ++idx) {
- bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
- if (bitmap != 0) {
- break;
- }
- mask = ~0U;
- }
- // Check for fallthrough: No bits set in bitmap
- if ((bitmap == 0) && (idx == SMALL_BITMAP_WORDS)) {
- return NULL;
- }
-
- // Start looking at the first set bit, plus 32 bits for every word of
- // zeroes or entries that were too small.
- slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
- } else {
- bitmap = small_mag_ptr->mag_bitmap[0] & ~((1 << slot) - 1);
- if (!bitmap) {
- return NULL;
- }
-
- slot = BITMAP32_CTZ((&bitmap));
- }
- limit = free_list + szone->num_small_slots - 1;
+ //
+ // BITMAPN_CTZ implementation
+ unsigned idx = slot >> 5;
+ bitmap = 0;
+ unsigned mask = ~((1 << (slot & 31)) - 1);
+ for (; idx < SMALL_FREELIST_BITMAP_WORDS(rack); ++idx) {
+ bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
+ if (bitmap != 0) {
+ break;
+ }
+ mask = ~0U;
+ }
+ // Check for fallthrough: No bits set in bitmap
+ if ((bitmap == 0) && (idx == SMALL_FREELIST_BITMAP_WORDS(rack))) {
+ return NULL;
+ }
+
+ // Start looking at the first set bit, plus 32 bits for every word of
+ // zeroes or entries that were too small.
+ slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
+
+ limit = free_list + SMALL_FREE_SLOT_COUNT(rack) - 1;
free_list += slot;
if (free_list < limit) {
- ptr = small_free_list_get_ptr(szone, *free_list);
+ ptr = small_free_list_get_ptr(*free_list);
if (ptr) {
return SMALL_REGION_FOR_PTR(ptr);
} else {
/* Shouldn't happen. Fall through to look at last slot. */
#if DEBUG_MALLOC
- malloc_printf("in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
+ malloc_report(ASL_LEVEL_ERR, "in small_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_small_slots - 1) * (small quantum size).
- ptr = small_free_list_get_ptr(szone, *limit);
+ ptr = small_free_list_get_ptr(*limit);
if (ptr) {
return SMALL_REGION_FOR_PTR(ptr);
}
@@ -753,32 +914,32 @@
}
static boolean_t
-small_get_region_from_depot(szone_t *szone, magazine_t *small_mag_ptr, mag_index_t mag_index, msize_t msize)
-{
- magazine_t *depot_ptr = &(szone->small_magazines[DEPOT_MAGAZINE_INDEX]);
+small_get_region_from_depot(rack_t *rack, magazine_t *small_mag_ptr, mag_index_t mag_index, msize_t msize)
+{
+ magazine_t *depot_ptr = &(rack->magazines[DEPOT_MAGAZINE_INDEX]);
/* FIXME: Would Uniprocessor benefit from recirc and MADV_FREE? */
- if (szone->num_small_magazines == 1) { // Uniprocessor, single magazine, so no recirculation necessary
+ if (rack->num_magazines == 1) { // Uniprocessor, single magazine, so no recirculation necessary
return 0;
}
#if DEBUG_MALLOC
if (DEPOT_MAGAZINE_INDEX == mag_index) {
- szone_error(szone, 1, "small_get_region_from_depot called for magazine index -1", NULL, NULL);
+ malloc_zone_error(rack->debug_flags, true, "small_get_region_from_depot called for magazine index -1\n", NULL, NULL);
return 0;
}
#endif
- SZONE_MAGAZINE_PTR_LOCK(szone, depot_ptr);
+ SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
// Appropriate a Depot'd region that can satisfy requested msize.
region_trailer_t *node;
region_t sparse_region;
while (1) {
- sparse_region = small_find_msize_region(szone, depot_ptr, DEPOT_MAGAZINE_INDEX, msize);
+ sparse_region = small_find_msize_region(rack, depot_ptr, DEPOT_MAGAZINE_INDEX, msize);
if (NULL == sparse_region) { // Depot empty?
- SZONE_MAGAZINE_PTR_UNLOCK(szone, depot_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
return 0;
}
@@ -787,23 +948,23 @@
break;
}
- SZONE_MAGAZINE_PTR_UNLOCK(szone, depot_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
yield();
- SZONE_MAGAZINE_PTR_LOCK(szone, depot_ptr);
+ SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
}
// disconnect node from Depot
- recirc_list_extract(szone, depot_ptr, node);
+ recirc_list_extract(rack, depot_ptr, node);
// Iterate the region pulling its free entries off the (locked) Depot's free list
- int objects_in_use = small_free_detach_region(szone, depot_ptr, sparse_region);
+ int objects_in_use = small_free_detach_region(rack, depot_ptr, sparse_region);
// Transfer ownership of the region
MAGAZINE_INDEX_FOR_SMALL_REGION(sparse_region) = mag_index;
node->pinned_to_depot = 0;
// Iterate the region putting its free entries on its new (locked) magazine's free list
- size_t bytes_inplay = small_free_reattach_region(szone, small_mag_ptr, sparse_region);
+ size_t bytes_inplay = small_free_reattach_region(rack, small_mag_ptr, sparse_region);
depot_ptr->mag_num_bytes_in_objects -= bytes_inplay;
depot_ptr->num_bytes_in_magazine -= SMALL_REGION_PAYLOAD_BYTES;
@@ -813,24 +974,104 @@
small_mag_ptr->num_bytes_in_magazine += SMALL_REGION_PAYLOAD_BYTES;
small_mag_ptr->mag_num_objects += objects_in_use;
- // connect to magazine as first node
- recirc_list_splice_first(szone, small_mag_ptr, node);
-
- SZONE_MAGAZINE_PTR_UNLOCK(szone, depot_ptr);
-
- // madvise() outside the Depot lock
- (void)madvise_reuse_range(
- szone, sparse_region, (uintptr_t)sparse_region, (uintptr_t)sparse_region + SMALL_REGION_PAYLOAD_BYTES);
-
- MAGMALLOC_DEPOTREGION((void *)szone, (int)mag_index, (void *)sparse_region, SMALL_REGION_SIZE,
+ // connect to magazine as last node
+ recirc_list_splice_last(rack, small_mag_ptr, node);
+
+ SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
+
+ MAGMALLOC_DEPOTREGION(SMALL_SZONE_FROM_RACK(rack), (int)mag_index, (void *)sparse_region, SMALL_REGION_SIZE,
(int)BYTES_USED_FOR_SMALL_REGION(sparse_region)); // DTrace USDT Probe
return 1;
}
+#endif // CONFIG_RECIRC_DEPOT
+
+#if CONFIG_MADVISE_PRESSURE_RELIEF
+void
+small_madvise_pressure_relief(rack_t *rack)
+{
+ mag_index_t mag_index;
+ magazine_t *small_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(SMALL_SZONE_FROM_RACK(rack));
+
+ region_t small = rack->region_generation->hashed_regions[index];
+ if (!small || small == HASHRING_REGION_DEALLOCATED) {
+ SZONE_UNLOCK(SMALL_SZONE_FROM_RACK(rack));
+ continue;
+ }
+
+ magazine_t *mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
+ REGION_TRAILER_FOR_SMALL_REGION(small),
+ MAGAZINE_INDEX_FOR_SMALL_REGION(small));
+ SZONE_UNLOCK(SMALL_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_SMALL_REGION(small);
+
+ /* 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 (small == mag_ptr->mag_last_region && (mag_ptr->mag_bytes_free_at_end || mag_ptr->mag_bytes_free_at_start)) {
+ small_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_SMALL_REGION(small));
+ int objects_in_use = small_free_detach_region(rack, mag_ptr, small);
+
+ SZONE_MAGAZINE_PTR_LOCK(small_depot_ptr);
+ MAGAZINE_INDEX_FOR_SMALL_REGION(small) = DEPOT_MAGAZINE_INDEX;
+ REGION_TRAILER_FOR_SMALL_REGION(small)->pinned_to_depot = 0;
+
+ size_t bytes_inplay = small_free_reattach_region(rack, small_depot_ptr, small);
+
+ /* 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 -= SMALL_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);
+
+ small_depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
+ small_depot_ptr->num_bytes_in_magazine += SMALL_REGION_PAYLOAD_BYTES;
+ small_depot_ptr->mag_num_objects -= objects_in_use;
+
+ recirc_list_splice_last(rack, small_depot_ptr, REGION_TRAILER_FOR_SMALL_REGION(small));
+
+ /* Actually do the scan, done holding the depot lock, the call will drop the lock
+ * around the actual madvise syscalls.
+ */
+ small_free_scan_madvise_free(rack, small_depot_ptr, small);
+
+ /* 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(small_depot_ptr);
+ }
+ }
+}
+#endif // CONFIG_MADVISE_PRESSURE_RELIEF
#if CONFIG_AGGRESSIVE_MADVISE || CONFIG_RECIRC_DEPOT
static MALLOC_INLINE void
-small_madvise_free_range_no_lock(szone_t *szone,
+small_madvise_free_range_no_lock(rack_t *rack,
magazine_t *small_mag_ptr,
region_t region,
free_list_t freee,
@@ -838,7 +1079,7 @@
void *headptr,
size_t headsize)
{
- void *ptr = small_free_list_get_ptr(szone, freee);
+ void *ptr = small_free_list_get_ptr(freee);
region_trailer_t *node = REGION_TRAILER_FOR_SMALL_REGION(region);
// Lock on small_magazines[mag_index] is already held here.
@@ -870,13 +1111,19 @@
uintptr_t free_hi = MIN(trunc_extent, hi);
if (free_lo < free_hi) {
- small_free_list_remove_ptr(szone, small_mag_ptr, freee, fmsize);
+ // Before unlocking, ensure that the metadata for the freed region
+ // makes it look not free but includes the length. This ensures that
+ // any code that inspects the metadata while we are unlocked sees
+ // a valid state and will not try to use or coalesce freed memory
+ // into it.
+ small_free_mark_unfree(rack, freee, fmsize);
+ small_free_list_remove_ptr_no_clear(rack, small_mag_ptr, freee, fmsize);
OSAtomicIncrement32Barrier(&(node->pinned_to_depot));
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
- madvise_free_range(szone, region, free_lo, free_hi, &szone->last_small_advise);
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+ mvm_madvise_free(rack, region, free_lo, free_hi, &rack->last_madvise, rack->debug_flags & MALLOC_DO_SCRIBBLE);
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
OSAtomicDecrement32Barrier(&(node->pinned_to_depot));
- small_free_list_add_ptr(szone, small_mag_ptr, ptr, fmsize);
+ small_free_list_add_ptr(rack, small_mag_ptr, ptr, fmsize);
}
}
}
@@ -884,60 +1131,66 @@
#if CONFIG_RECIRC_DEPOT
static region_t
-small_free_try_depot_unmap_no_lock(szone_t *szone, magazine_t *depot_ptr, region_trailer_t *node)
-{
- if (0 < node->bytes_used || 0 < node->pinned_to_depot || depot_ptr->recirculation_entries < (szone->num_small_magazines * 2)) {
+small_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) {
return NULL;
}
// disconnect first node from Depot
- recirc_list_extract(szone, depot_ptr, node);
+ recirc_list_extract(rack, depot_ptr, node);
// Iterate the region pulling its free entries off the (locked) Depot's free list
region_t sparse_region = SMALL_REGION_FOR_PTR(node);
- int objects_in_use = small_free_detach_region(szone, depot_ptr, sparse_region);
+ int objects_in_use = small_free_detach_region(rack, depot_ptr, sparse_region);
if (0 == objects_in_use) {
// 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(szone->small_region_generation->hashed_regions,
- szone->small_region_generation->num_regions_allocated, szone->small_region_generation->num_regions_allocated_shift,
+ 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) {
- szone_error(szone, 1, "small_free_try_depot_unmap_no_lock hash lookup failed:", NULL, "%p\n", sparse_region);
+ malloc_zone_error(rack->debug_flags, true, "small_free_try_depot_unmap_no_lock hash lookup failed: %p\n", sparse_region);
return NULL;
}
*pSlot = HASHRING_REGION_DEALLOCATED;
depot_ptr->num_bytes_in_magazine -= SMALL_REGION_PAYLOAD_BYTES;
- __sync_fetch_and_add(&(szone->num_small_regions_dealloc), 1); // Atomically increment num_small_regions_dealloc
+ // 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((void *)szone, (void *)sparse_region, SMALL_REGION_SIZE); // DTrace USDT Probe
+ MAGMALLOC_DEALLOCREGION(SMALL_SZONE_FROM_RACK(rack), (void *)sparse_region, (int)SMALL_REGION_SIZE); // DTrace USDT Probe
return sparse_region;
} else {
- szone_error(szone, 1, "small_free_try_depot_unmap_no_lock objects_in_use not zero:", NULL, "%d\n", objects_in_use);
+ malloc_zone_error(rack->debug_flags, true, "small_free_try_depot_unmap_no_lock objects_in_use not zero: %d\n", objects_in_use);
return NULL;
}
}
static boolean_t
-small_free_do_recirc_to_depot(szone_t *szone, magazine_t *small_mag_ptr, mag_index_t mag_index)
+small_free_do_recirc_to_depot(rack_t *rack, magazine_t *small_mag_ptr, mag_index_t mag_index)
{
// The entire magazine crossed the "emptiness threshold". Transfer a region
// from this magazine to the Depot. Choose a region that itself has crossed the emptiness threshold (i.e
// is at least fraction "f" empty.) Such a region will be marked "suitable" on the recirculation list.
region_trailer_t *node = small_mag_ptr->firstNode;
- while (node && !node->recirc_suitable) {
+ while (node && (!node->recirc_suitable || node->pinned_to_depot)) {
node = node->next;
}
if (NULL == node) {
#if DEBUG_MALLOC
- malloc_printf("*** small_free_do_recirc_to_depot end of list\n");
-#endif
- return TRUE; // Caller must SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ malloc_report(ASL_LEVEL_ERR, "*** small_free_do_recirc_to_depot end of list\n");
+#endif
+ return TRUE; // Caller must SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
}
region_t sparse_region = SMALL_REGION_FOR_PTR(node);
@@ -945,58 +1198,58 @@
// Deal with unclaimed memory -- mag_bytes_free_at_end or mag_bytes_free_at start
if (sparse_region == small_mag_ptr->mag_last_region &&
(small_mag_ptr->mag_bytes_free_at_end || small_mag_ptr->mag_bytes_free_at_start)) {
- small_finalize_region(szone, small_mag_ptr);
+ small_finalize_region(rack, small_mag_ptr);
}
// disconnect "suitable" node from magazine
- recirc_list_extract(szone, small_mag_ptr, node);
+ recirc_list_extract(rack, small_mag_ptr, node);
// Iterate the region pulling its free entries off its (locked) magazine's free list
- int objects_in_use = small_free_detach_region(szone, small_mag_ptr, sparse_region);
- magazine_t *depot_ptr = &(szone->small_magazines[DEPOT_MAGAZINE_INDEX]);
+ int objects_in_use = small_free_detach_region(rack, small_mag_ptr, sparse_region);
+ magazine_t *depot_ptr = &(rack->magazines[DEPOT_MAGAZINE_INDEX]);
// hand over the region to the (locked) Depot
- SZONE_MAGAZINE_PTR_LOCK(szone, depot_ptr);
+ SZONE_MAGAZINE_PTR_LOCK(depot_ptr);
// this will cause small_free_list_add_ptr called by small_free_reattach_region to use
// the depot as its target magazine, rather than magazine formerly associated with sparse_region
MAGAZINE_INDEX_FOR_SMALL_REGION(sparse_region) = DEPOT_MAGAZINE_INDEX;
node->pinned_to_depot = 0;
// Iterate the region putting its free entries on Depot's free list
- size_t bytes_inplay = small_free_reattach_region(szone, depot_ptr, sparse_region);
+ size_t bytes_inplay = small_free_reattach_region(rack, depot_ptr, sparse_region);
small_mag_ptr->mag_num_bytes_in_objects -= bytes_inplay;
small_mag_ptr->num_bytes_in_magazine -= SMALL_REGION_PAYLOAD_BYTES;
small_mag_ptr->mag_num_objects -= objects_in_use;
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr); // Unlock the originating magazine
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr); // Unlock the originating magazine
depot_ptr->mag_num_bytes_in_objects += bytes_inplay;
depot_ptr->num_bytes_in_magazine += SMALL_REGION_PAYLOAD_BYTES;
depot_ptr->mag_num_objects += objects_in_use;
// connect to Depot as last node
- recirc_list_splice_last(szone, depot_ptr, node);
-
- MAGMALLOC_RECIRCREGION((void *)szone, (int)mag_index, (void *)sparse_region, SMALL_REGION_SIZE,
+ recirc_list_splice_last(rack, depot_ptr, node);
+
+ MAGMALLOC_RECIRCREGION(SMALL_SZONE_FROM_RACK(rack), (int)mag_index, (void *)sparse_region, SMALL_REGION_SIZE,
(int)BYTES_USED_FOR_SMALL_REGION(sparse_region)); // DTrace USDT Probe
#if !CONFIG_AGGRESSIVE_MADVISE
// Mark free'd dirty pages with MADV_FREE to reduce memory pressure
- small_free_scan_madvise_free(szone, depot_ptr, sparse_region);
+ small_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 = small_free_try_depot_unmap_no_lock(szone, depot_ptr, node);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, depot_ptr);
+ region_t r_dealloc = small_free_try_depot_unmap_no_lock(rack, depot_ptr, node);
+ SZONE_MAGAZINE_PTR_UNLOCK(depot_ptr);
if (r_dealloc) {
- deallocate_pages(szone, r_dealloc, SMALL_REGION_SIZE, 0);
+ mvm_deallocate_pages(r_dealloc, SMALL_REGION_SIZE, 0);
}
return FALSE; // Caller need not unlock the originating magazine
}
static MALLOC_INLINE boolean_t
-small_free_try_recirc_to_depot(szone_t *szone,
+small_free_try_recirc_to_depot(rack_t *rack,
magazine_t *small_mag_ptr,
mag_index_t mag_index,
region_t region,
@@ -1009,12 +1262,12 @@
size_t bytes_used = node->bytes_used;
/* FIXME: Would Uniprocessor benefit from recirc and MADV_FREE? */
- if (szone->num_small_magazines == 1) { // Uniprocessor, single magazine, so no recirculation necessary
+ if (rack->num_magazines == 1) { // Uniprocessor, single magazine, so no recirculation necessary
/* NOTHING */
- return TRUE; // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(szone, tiny_mag_ptr)
+ return TRUE; // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr)
} else if (DEPOT_MAGAZINE_INDEX != mag_index) {
// Emptiness discriminant
- if (bytes_used < DENSITY_THRESHOLD(SMALL_REGION_PAYLOAD_BYTES)) {
+ if (small_region_below_recirc_threshold(region)) {
/* Region has crossed threshold from density to sparsity. Mark it "suitable" on the
* recirculation candidates list. */
node->recirc_suitable = TRUE;
@@ -1026,19 +1279,14 @@
// Has the entire magazine crossed the "emptiness threshold"? If so, transfer a region
// from this magazine to the Depot. Choose a region that itself has crossed the emptiness threshold (i.e
// is at least fraction "f" empty.) Such a region will be marked "suitable" on the recirculation list.
-
- size_t a = small_mag_ptr->num_bytes_in_magazine; // Total bytes allocated to this magazine
- size_t u = small_mag_ptr->mag_num_bytes_in_objects; // In use (malloc'd) from this magaqzine
-
- if (a - u > ((3 * SMALL_REGION_PAYLOAD_BYTES) / 2) && u < DENSITY_THRESHOLD(a)) {
- return small_free_do_recirc_to_depot(szone, small_mag_ptr, mag_index);
- }
-
+ if (small_magazine_below_recirc_threshold(small_mag_ptr)) {
+ return small_free_do_recirc_to_depot(rack, small_mag_ptr, mag_index);
+ }
} else {
#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.
- small_madvise_free_range_no_lock(szone, small_mag_ptr, region, freee, msize, headptr, headsize);
+ small_madvise_free_range_no_lock(rack, small_mag_ptr, region, freee, msize, headptr, headsize);
#endif
if (0 < bytes_used || 0 < node->pinned_to_depot) {
@@ -1047,20 +1295,20 @@
* with detach_region/reattach_region */
} else {
/* Depot'd region is just now empty. Consider return to OS. */
- region_t r_dealloc = small_free_try_depot_unmap_no_lock(szone, small_mag_ptr, node);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ region_t r_dealloc = small_free_try_depot_unmap_no_lock(rack, small_mag_ptr, node);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
if (r_dealloc) {
- deallocate_pages(szone, r_dealloc, SMALL_REGION_SIZE, 0);
+ mvm_deallocate_pages(r_dealloc, SMALL_REGION_SIZE, 0);
}
return FALSE; // Caller need not unlock
}
}
- return TRUE; // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(szone, tiny_mag_ptr)
+ return TRUE; // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr)
}
#endif // CONFIG_RECIRC_DEPOT
static MALLOC_INLINE boolean_t
-small_free_no_lock(szone_t *szone, magazine_t *small_mag_ptr, mag_index_t mag_index, region_t region, void *ptr, msize_t msize)
+small_free_no_lock(rack_t *rack, magazine_t *small_mag_ptr, mag_index_t mag_index, region_t region, void *ptr, msize_t msize)
{
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
unsigned index = SMALL_META_INDEX_FOR_PTR(ptr);
@@ -1068,33 +1316,38 @@
unsigned char *next_block = ((unsigned char *)ptr + original_size);
msize_t next_index = index + msize;
- MALLOC_TRACE(TRACE_small_free, (uintptr_t)szone, (uintptr_t)small_mag_ptr, (uintptr_t)ptr, SMALL_BYTES_FOR_MSIZE(msize));
+ MALLOC_TRACE(TRACE_small_free, (uintptr_t)rack, (uintptr_t)small_mag_ptr, (uintptr_t)ptr, SMALL_BYTES_FOR_MSIZE(msize));
#if CONFIG_AGGRESSIVE_MADVISE || CONFIG_RECIRC_DEPOT
void *original_ptr = ptr;
-#endif
+#endif // CONFIG_AGGRESSIVE_MADVISE || CONFIG_RECIRC_DEPOT
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_printf("in small_free_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
+ malloc_report(ASL_LEVEL_ERR, "in small_free_no_lock(), ptr=%p, msize=%d\n", ptr, msize);
}
if (!msize) {
- szone_error(szone, 1, "trying to free small block that is too small", ptr, "in small_free_no_lock(), ptr=%p, msize=%d\n",
- ptr, msize);
- }
-#endif
+ malloc_zone_error(rack->debug_flags, true, "trying to free small block that is too small in small_free_no_lock(), ptr=%p, msize=%d\n",
+ ptr, msize);
+ }
+#endif
+
+ // Check that the region cookie is intact.
+ region_trailer_t *trailer = REGION_TRAILER_FOR_SMALL_REGION(region);
+ region_check_cookie(region, trailer);
// We try to coalesce this block with the preceeding one
if (index > 0 && (meta_headers[index - 1] & SMALL_IS_FREE)) {
msize_t previous_msize = meta_headers[index - 1] & ~SMALL_IS_FREE;
grain_t previous_index = index - previous_msize;
- // Check if the metadata for the start of the region is also free.
+ // Check if the metadata for the start of the block is also free.
if (meta_headers[previous_index] == (previous_msize | SMALL_IS_FREE)) {
void *previous_ptr = (void *)((uintptr_t)ptr - SMALL_BYTES_FOR_MSIZE(previous_msize));
- free_list_t previous = small_free_list_find_by_ptr(szone, small_mag_ptr, previous_ptr, previous_msize);
- small_free_list_remove_ptr(szone, small_mag_ptr, previous, previous_msize);
+ free_list_t previous = small_free_list_find_by_ptr(rack, small_mag_ptr, previous_ptr, previous_msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, previous, previous_msize);
ptr = previous_ptr;
+ small_meta_header_set_middle(meta_headers, index); // This block is now a middle block.
msize += previous_msize;
index -= previous_msize;
} else {
@@ -1106,47 +1359,45 @@
// Try to coalesce with this block with the next block
if ((next_block < SMALL_REGION_END(region)) && (meta_headers[next_index] & SMALL_IS_FREE)) {
msize_t next_msize = meta_headers[next_index] & ~SMALL_IS_FREE;
- free_list_t next = small_free_list_find_by_ptr(szone, small_mag_ptr, next_block, next_msize);
- small_free_list_remove_ptr(szone, small_mag_ptr, next, next_msize);
+ free_list_t next = small_free_list_find_by_ptr(rack, small_mag_ptr, next_block, next_msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, next, next_msize);
msize += next_msize;
}
- if (szone->debug_flags & MALLOC_DO_SCRIBBLE) {
+ if (rack->debug_flags & MALLOC_DO_SCRIBBLE) {
if (!msize) {
- szone_error(szone, 1, "incorrect size information - block header was damaged", ptr, NULL);
+ malloc_zone_error(rack->debug_flags, true, "incorrect size information for %p - block header was damaged\n", ptr);
} else {
memset(ptr, SCRABBLE_BYTE, SMALL_BYTES_FOR_MSIZE(msize));
}
}
- free_list_t freee = small_free_list_add_ptr(szone, small_mag_ptr, ptr, msize);
+ free_list_t freee = small_free_list_add_ptr(rack, small_mag_ptr, ptr, msize);
// use original_size and not msize to avoid double counting the coalesced blocks
small_mag_ptr->mag_num_bytes_in_objects -= original_size;
- small_mag_ptr->mag_num_objects--;
// Update this region's bytes in use count
- region_trailer_t *node = REGION_TRAILER_FOR_SMALL_REGION(region);
- size_t bytes_used = node->bytes_used - original_size;
- node->bytes_used = (unsigned int)bytes_used;
+ size_t bytes_used = trailer->bytes_used - original_size;
+ trailer->bytes_used = (unsigned int)bytes_used;
#if CONFIG_AGGRESSIVE_MADVISE
- small_madvise_free_range_no_lock(szone, small_mag_ptr, region, freee, msize, original_ptr, original_size);
-#endif
-
- // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(szone, tiny_mag_ptr) if this function
+ small_madvise_free_range_no_lock(rack, small_mag_ptr, region, freee, msize, original_ptr, original_size);
+#endif
+
+ // Caller must do SZONE_MAGAZINE_PTR_UNLOCK(tiny_mag_ptr) if this function
// returns TRUE.
boolean_t needs_unlock = TRUE;
#if CONFIG_RECIRC_DEPOT
- needs_unlock = small_free_try_recirc_to_depot(szone, small_mag_ptr, mag_index, region, freee, msize, original_ptr, original_size);
-#endif
+ needs_unlock = small_free_try_recirc_to_depot(rack, small_mag_ptr, mag_index, region, freee, msize, original_ptr, original_size);
+#endif // CONFIG_RECIRC_DEPOT
return needs_unlock;
}
// Allocates from the last region or a freshly allocated region
static void *
-small_malloc_from_region_no_lock(szone_t *szone,
+small_malloc_from_region_no_lock(rack_t *rack,
magazine_t *small_mag_ptr,
mag_index_t mag_index,
msize_t msize,
@@ -1157,55 +1408,20 @@
// Before anything we transform the mag_bytes_free_at_end or mag_bytes_free_at_start - if any - to a regular free block
/* FIXME: last_block needs to be coalesced with previous entry if free, <rdar://5462322> */
if (small_mag_ptr->mag_bytes_free_at_end || small_mag_ptr->mag_bytes_free_at_start) {
- small_finalize_region(szone, small_mag_ptr);
- }
-
- // Here find the only place in smallville that (infrequently) takes the small_regions_lock.
- // Only one thread at a time should be permitted to assess the density of the hash
- // ring and adjust if needed.
- // Only one thread at a time should be permitted to insert its new region on
- // the hash ring.
- // It is safe for all other threads to read the hash ring (hashed_regions) and
- // the associated sizes (num_regions_allocated and num_small_regions).
-
- _malloc_lock_lock(&szone->small_regions_lock);
- // Check to see if the hash ring of small regions needs to grow. Try to
- // avoid the hash ring becoming too dense.
- if (szone->small_region_generation->num_regions_allocated < (2 * szone->num_small_regions)) {
- region_t *new_regions;
- size_t new_size;
- size_t new_shift = szone->small_region_generation->num_regions_allocated_shift; // In/Out parameter
- new_regions = hash_regions_grow_no_lock(szone, szone->small_region_generation->hashed_regions,
- szone->small_region_generation->num_regions_allocated, &new_shift, &new_size);
- // Do not deallocate the current hashed_regions allocation since someone
- // may be iterating it. Instead, just leak it.
-
- // Prepare to advance to the "next generation" of the hash ring.
- szone->small_region_generation->nextgen->hashed_regions = new_regions;
- szone->small_region_generation->nextgen->num_regions_allocated = new_size;
- szone->small_region_generation->nextgen->num_regions_allocated_shift = new_shift;
-
- // Throw the switch to atomically advance to the next generation.
- szone->small_region_generation = szone->small_region_generation->nextgen;
- // Ensure everyone sees the advance.
- OSMemoryBarrier();
- }
+ small_finalize_region(rack, small_mag_ptr);
+ }
+
// 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_SMALL_REGION(aligned_address) = mag_index;
- // Insert the new region into the hash ring, and update malloc statistics
- hash_region_insert_no_lock(szone->small_region_generation->hashed_regions,
- szone->small_region_generation->num_regions_allocated, szone->small_region_generation->num_regions_allocated_shift,
- aligned_address);
-
- szone->num_small_regions++;
-
- _malloc_lock_unlock(&szone->small_regions_lock);
+ // Insert the new region into the hash ring
+ rack_region_insert(rack, (region_t)aligned_address);
small_mag_ptr->mag_last_region = aligned_address;
BYTES_USED_FOR_SMALL_REGION(aligned_address) = SMALL_BYTES_FOR_MSIZE(msize);
+
#if CONFIG_ASLR_INTERNAL
int offset_msize = malloc_entropy[1] & SMALL_ENTROPY_MASK;
#if DEBUG_MALLOC
@@ -1213,7 +1429,7 @@
offset_msize = strtol(getenv("MallocASLRForce"), NULL, 0) & SMALL_ENTROPY_MASK;
}
if (getenv("MallocASLRPrint")) {
- malloc_printf("Region: %p offset: %d\n", aligned_address, offset_msize);
+ malloc_report(ASL_LEVEL_INFO, "Region: %p offset: %d\n", aligned_address, offset_msize);
}
#endif
#else
@@ -1240,7 +1456,7 @@
#endif
// connect to magazine as last node
- recirc_list_splice_last(szone, small_mag_ptr, REGION_TRAILER_FOR_SMALL_REGION(aligned_address));
+ recirc_list_splice_last(rack, small_mag_ptr, REGION_TRAILER_FOR_SMALL_REGION(aligned_address));
return ptr;
}
@@ -1248,14 +1464,8 @@
void *
small_memalign(szone_t *szone, size_t alignment, size_t size, size_t span)
{
- if (size <= SMALL_THRESHOLD) {
- // ensure block allocated by small does not have a tiny-possible size
- size = SMALL_THRESHOLD + TINY_QUANTUM;
- span = size + alignment - 1;
- }
-
msize_t mspan = SMALL_MSIZE_FOR_BYTES(span + SMALL_QUANTUM - 1);
- void *p = szone_malloc(szone, span); // avoid inlining small_malloc_should_clear(szone, mspan, 0);
+ void *p = small_malloc_should_clear(&szone->small_rack, mspan, 0);
if (NULL == p) {
return NULL;
@@ -1272,65 +1482,70 @@
void *q = (void *)(((uintptr_t)p) + pad);
// Mark q as block header and in-use, thus creating two blocks.
- magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(szone, szone->small_magazines,
- REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)),
- MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)));
+ magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(szone->small_rack.magazines,
+ REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)),
+ MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)));
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(p), SMALL_META_INDEX_FOR_PTR(p), mpad);
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(q), SMALL_META_INDEX_FOR_PTR(q), msize + mwaste);
small_mag_ptr->mag_num_objects++;
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
// Give up mpad blocks beginning at p to the small free list
- // region_t r = SMALL_REGION_FOR_PTR(p);
- szone_free(szone, p); // avoid inlining free_small(szone, p, &r);
+ free_small(&szone->small_rack, p, SMALL_REGION_FOR_PTR(p), SMALL_BYTES_FOR_MSIZE(mpad));
p = q; // advance p to the desired alignment
}
if (mwaste > 0) {
void *q = (void *)(((uintptr_t)p) + SMALL_BYTES_FOR_MSIZE(msize));
// Mark q as block header and in-use, thus creating two blocks.
- magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(szone, szone->small_magazines,
- REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)),
- MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)));
+ magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(szone->small_rack.magazines,
+ REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)),
+ MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(p)));
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(p), SMALL_META_INDEX_FOR_PTR(p), msize);
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(q), SMALL_META_INDEX_FOR_PTR(q), mwaste);
small_mag_ptr->mag_num_objects++;
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
// Give up mwaste blocks beginning at q to the small free list
- // region_t r = SMALL_REGION_FOR_PTR(q);
- szone_free(szone, q); // avoid inlining free_small(szone, q, &r);
+ free_small(&szone->small_rack, q, SMALL_REGION_FOR_PTR(q), SMALL_BYTES_FOR_MSIZE(mwaste));
}
return p; // p has the desired size and alignment, and can later be free()'d
}
+boolean_t
+small_claimed_address(rack_t *rack, void *ptr)
+{
+ region_t r = small_region_for_ptr_no_lock(rack, ptr);
+ return r && ptr < (void *)SMALL_REGION_END(r);
+}
+
void *
-small_try_shrink_in_place(szone_t *szone, void *ptr, size_t old_size, size_t new_good_size)
+small_try_shrink_in_place(rack_t *rack, void *ptr, size_t old_size, size_t new_good_size)
{
msize_t new_msize = SMALL_MSIZE_FOR_BYTES(new_good_size);
msize_t mshrinkage = SMALL_MSIZE_FOR_BYTES(old_size) - new_msize;
if (mshrinkage) {
void *q = (void *)((uintptr_t)ptr + SMALL_BYTES_FOR_MSIZE(new_msize));
- magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(szone, szone->small_magazines,
- REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)),
- MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)));
+ magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
+ REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)),
+ MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)));
// Mark q as block header and in-use, thus creating two blocks.
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(ptr), SMALL_META_INDEX_FOR_PTR(ptr), new_msize);
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(q), SMALL_META_INDEX_FOR_PTR(q), mshrinkage);
small_mag_ptr->mag_num_objects++;
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
- szone_free(szone, q); // avoid inlining free_small(szone, q, ...);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+ free_small(rack, q, SMALL_REGION_FOR_PTR(q), 0);
}
return ptr;
}
boolean_t
-small_try_realloc_in_place(szone_t *szone, void *ptr, size_t old_size, size_t new_size)
+small_try_realloc_in_place(rack_t *rack, void *ptr, size_t old_size, size_t new_size)
{
// returns 1 on success
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
@@ -1355,58 +1570,105 @@
#if DEBUG_MALLOC
if ((uintptr_t)next_block & (SMALL_QUANTUM - 1)) {
- szone_error(szone, 1, "internal invariant broken in realloc(next_block)", next_block, NULL);
+ malloc_zone_error(rack->debug_flags, true, "internal invariant broken in realloc(next_block) for %p\n", next_block);
}
if (meta_headers[index] != old_msize) {
- malloc_printf("*** small_try_realloc_in_place incorrect old %d %d\n", meta_headers[index], old_msize);
- }
-#endif
-
- magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(szone, szone->small_magazines,
- REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)), MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)));
+ malloc_report(ASL_LEVEL_ERR, "*** small_try_realloc_in_place incorrect old %d %d\n", meta_headers[index], old_msize);
+ }
+#endif
+
+ magazine_t *small_mag_ptr = mag_lock_zine_for_region_trailer(rack->magazines,
+ REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)),
+ MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr)));
if (DEPOT_MAGAZINE_INDEX == MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr))) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
return 0;
}
- /*
- * Look for a free block immediately afterwards. If it's large enough, we can consume (part of)
- * it.
- */
- next_msize_and_free = meta_headers[next_index];
- is_free = next_msize_and_free & SMALL_IS_FREE;
- if (!is_free) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
- return 0; // next_block is in use;
- }
-
- next_msize = next_msize_and_free & ~SMALL_IS_FREE;
- if (old_msize + next_msize < new_msize) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_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.
- free_list_t freee = small_free_list_find_by_ptr(szone, small_mag_ptr, next_block, next_msize);
- small_free_list_remove_ptr(szone, small_mag_ptr, freee, next_msize);
- small_meta_header_set_middle(meta_headers, next_index);
- leftover_msize = old_msize + next_msize - new_msize;
- if (leftover_msize) {
- /* there's some left, so put the remainder back */
- leftover = (unsigned char *)ptr + SMALL_BYTES_FOR_MSIZE(new_msize);
-
- small_free_list_add_ptr(szone, small_mag_ptr, leftover, leftover_msize);
- }
+ msize_t coalesced_msize = new_msize - old_msize;
+#if CONFIG_SMALL_CACHE
+ void *last_free_ptr = small_mag_ptr->mag_last_free;
+ msize_t last_free_msize = small_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) {
+ small_mag_ptr->mag_last_free_msize -= coalesced_msize;
+ small_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 small_meta_header_set_middle() call below.
+ small_meta_header_set_in_use(meta_headers, index + new_msize, leftover_msize);
+ } else {
+ // Using the whole block.
+ small_mag_ptr->mag_last_free = NULL;
+ small_mag_ptr->mag_last_free_msize = 0;
+ small_mag_ptr->mag_last_free_rgn = NULL;
+ }
+ small_meta_header_set_in_use(meta_headers, index, new_msize);
+ small_meta_header_set_middle(meta_headers, next_index);
+ } else {
+#endif // CONFIG_SMALL_CACHE
+ /*
+ * Try to expand into unused space immediately after this block.
+ */
+ msize_t unused_msize = SMALL_MSIZE_FOR_BYTES(small_mag_ptr->mag_bytes_free_at_end);
+ void *unused_start = SMALL_REGION_END(SMALL_REGION_FOR_PTR(ptr)) - small_mag_ptr->mag_bytes_free_at_end;
+ if (small_mag_ptr->mag_last_region == SMALL_REGION_FOR_PTR(ptr)
+ && coalesced_msize < unused_msize && unused_start == ptr + old_size) {
+ // Extend the in-use for this block to the new size
+ small_meta_header_set_in_use(meta_headers, index, new_msize);
+
+ // Clear the in-use size for the start of the area we extended into
+ small_meta_header_set_middle(meta_headers, next_index);
+
+ // Reduce mag_bytes_free_at_end and update its in-use size.
+ small_mag_ptr->mag_bytes_free_at_end -= SMALL_BYTES_FOR_MSIZE(coalesced_msize);
+ small_meta_header_set_in_use(meta_headers, index + new_msize, SMALL_MSIZE_FOR_BYTES(small_mag_ptr->mag_bytes_free_at_end));
+ } else {
+ /*
+ * Look for a free block immediately afterwards. If it's large enough, we can consume (part of)
+ * it.
+ */
+ next_msize_and_free = meta_headers[next_index];
+ is_free = next_msize_and_free & SMALL_IS_FREE;
+ if (!is_free) {
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+ return 0; // next_block is in use;
+ }
+
+ next_msize = next_msize_and_free & ~SMALL_IS_FREE;
+ if (old_msize + next_msize < new_msize) {
+ SZONE_MAGAZINE_PTR_UNLOCK(small_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.
+ free_list_t freee = small_free_list_find_by_ptr(rack, small_mag_ptr, next_block, next_msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, freee, next_msize);
+ small_meta_header_set_middle(meta_headers, next_index);
+ leftover_msize = old_msize + next_msize - new_msize;
+ if (leftover_msize) {
+ /* there's some left, so put the remainder back */
+ leftover = (unsigned char *)ptr + SMALL_BYTES_FOR_MSIZE(new_msize);
+ small_free_list_add_ptr(rack, small_mag_ptr, leftover, leftover_msize);
+ }
+ small_meta_header_set_in_use(meta_headers, index, new_msize);
+ }
+#if CONFIG_SMALL_CACHE
+ }
+#endif // CONFIG_SMALL_CACHE
#if DEBUG_MALLOC
if (SMALL_BYTES_FOR_MSIZE(new_msize) > szone->large_threshold) {
- malloc_printf("*** realloc in place for %p exceeded msize=%d\n", new_msize);
- }
-#endif
- small_meta_header_set_in_use(meta_headers, index, new_msize);
-#if DEBUG_MALLOC
+ malloc_report(ASL_LEVEL_ERR, "*** realloc in place for %p exceeded msize=%d\n", new_msize);
+ }
+
if (LOG(szone, ptr)) {
- malloc_printf("in small_try_realloc_in_place(), ptr=%p, msize=%d\n", ptr, *SMALL_METADATA_FOR_PTR(ptr));
+ malloc_report(ASL_LEVEL_INFO, "in small_try_realloc_in_place(), ptr=%p, msize=%d\n", ptr, *SMALL_METADATA_FOR_PTR(ptr));
}
#endif
small_mag_ptr->mag_num_bytes_in_objects += SMALL_BYTES_FOR_MSIZE(new_msize - old_msize);
@@ -1426,13 +1688,20 @@
node->recirc_suitable = FALSE;
}
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
return 1;
}
+static char *small_check_fail_msg = "check: incorrect small region ";
+
+#define SMALL_CHECK_FAIL(fmt, ...) \
+ malloc_zone_check_fail(small_check_fail_msg, \
+ "%ld, counter=%d\n" fmt, region_index, counter, __VA_ARGS__);
+
boolean_t
-small_check_region(szone_t *szone, region_t region)
+small_check_region(rack_t *rack, region_t region, size_t region_index,
+ unsigned counter)
{
unsigned char *ptr = SMALL_REGION_ADDRESS(region);
msize_t *meta_headers = SMALL_META_HEADER_FOR_PTR(ptr);
@@ -1444,7 +1713,7 @@
free_list_t free_head, previous, next;
msize_t *follower;
mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr));
- magazine_t *small_mag_ptr = &(szone->small_magazines[mag_index]);
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
// Assumes locked
CHECK_MAGAZINE_PTR_LOCKED(szone, small_mag_ptr, __PRETTY_FUNCTION__);
@@ -1461,13 +1730,13 @@
// block is in use
msize = msize_and_free;
if (!msize) {
- malloc_printf("*** invariant broken: null msize ptr=%p num_small_regions=%d end=%p\n", ptr,
- szone->num_small_regions, region_end);
+ SMALL_CHECK_FAIL("*** invariant broken: null msize ptr=%p num_small_regions=%d end=%p\n", ptr,
+ (int)rack->num_regions, region_end);
return 0;
}
#if !CONFIG_RELAXED_INVARIANT_CHECKS
if (SMALL_BYTES_FOR_MSIZE(msize) > szone->large_threshold) {
- malloc_printf("*** invariant broken for %p this small msize=%d - size is too large\n", ptr, msize_and_free);
+ SMALL_CHECK_FAIL("*** invariant broken for %p this small msize=%d - size is too large\n", ptr, msize_and_free);
return 0;
}
#endif // CONFIG_RELAXED_INVARIANT_CHECKS
@@ -1479,12 +1748,12 @@
free_head = (free_list_t){ .p = ptr };
follower = (msize_t *)FOLLOWING_SMALL_PTR(ptr, msize);
if (!msize) {
- malloc_printf("*** invariant broken for free block %p this msize=%d\n", ptr, msize);
+ SMALL_CHECK_FAIL("*** invariant broken for free block %p this msize=%d\n", ptr, msize);
return 0;
}
#if !CONFIG_RELAXED_INVARIANT_CHECKS
if (prev_free) {
- malloc_printf("*** invariant broken for %p (2 free in a row)\n", ptr);
+ SMALL_CHECK_FAIL("*** invariant broken for %p (2 free in a row)\n", ptr);
return 0;
}
#endif
@@ -1497,21 +1766,20 @@
}
}
- previous = small_free_list_get_previous(szone, free_head);
- next = small_free_list_get_next(szone, free_head);
- if (previous.p && !SMALL_PTR_IS_FREE(small_free_list_get_ptr(szone, previous))) {
- malloc_printf("*** invariant broken for %p (previous %p is not a free pointer)\n", ptr, small_free_list_get_ptr(szone, previous));
+ previous = small_free_list_get_previous(rack, free_head);
+ next = small_free_list_get_next(rack, free_head);
+ if (previous.p && !SMALL_PTR_IS_FREE(small_free_list_get_ptr(previous))) {
+ SMALL_CHECK_FAIL("*** invariant broken for %p (previous %p is not a free pointer)\n", ptr, small_free_list_get_ptr(previous));
return 0;
}
- if (next.p && !SMALL_PTR_IS_FREE(small_free_list_get_ptr(szone, next))) {
- malloc_printf("*** invariant broken for %p (next %p is not a free pointer)\n", ptr, small_free_list_get_ptr(szone, next));
+ if (next.p && !SMALL_PTR_IS_FREE(small_free_list_get_ptr(next))) {
+ SMALL_CHECK_FAIL("*** invariant broken for %p (next %p is not a free pointer)\n", ptr, small_free_list_get_ptr(next));
return 0;
}
if (SMALL_PREVIOUS_MSIZE(follower) != msize) {
- malloc_printf(
- "*** invariant broken for small free %p followed by %p in region [%p-%p] "
- "(end marker incorrect) should be %d; in fact %d\n",
- ptr, follower, SMALL_REGION_ADDRESS(region), region_end, msize, SMALL_PREVIOUS_MSIZE(follower));
+ SMALL_CHECK_FAIL("*** invariant broken for small free %p followed by %p in region [%p-%p] "
+ "(end marker incorrect) should be %d; in fact %d\n",
+ ptr, follower, SMALL_REGION_ADDRESS(region), region_end, msize, SMALL_PREVIOUS_MSIZE(follower));
return 0;
}
ptr = (unsigned char *)follower;
@@ -1548,7 +1816,7 @@
magazine_t *small_mag_base = NULL;
region_hash_generation_t *srg_ptr;
- err = reader(task, (vm_address_t)szone->small_region_generation, sizeof(region_hash_generation_t), (void **)&srg_ptr);
+ err = reader(task, (vm_address_t)szone->small_rack.region_generation, sizeof(region_hash_generation_t), (void **)&srg_ptr);
if (err) {
return err;
}
@@ -1561,7 +1829,7 @@
if (type_mask & MALLOC_PTR_IN_USE_RANGE_TYPE) {
// Map in all active magazines. Do this outside the iteration over regions.
- err = reader(task, (vm_address_t)(szone->small_magazines), szone->num_small_magazines * sizeof(magazine_t),
+ err = reader(task, (vm_address_t)(szone->small_rack.magazines), szone->small_rack.num_magazines * sizeof(magazine_t),
(void **)&small_mag_base);
if (err) {
return err;
@@ -1584,10 +1852,6 @@
recorder(task, context, MALLOC_PTR_REGION_RANGE_TYPE, &ptr_range, 1);
}
if (type_mask & MALLOC_PTR_IN_USE_RANGE_TYPE) {
- 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);
if (err) {
return err;
@@ -1596,23 +1860,16 @@
mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(mapped_region);
magazine_t *small_mag_ptr = small_mag_base + mag_index;
- if (DEPOT_MAGAZINE_INDEX != mag_index) {
- mag_last_free = small_mag_ptr->mag_last_free;
- if (mag_last_free) {
- mag_last_free_ptr = (uintptr_t)mag_last_free & ~(SMALL_QUANTUM - 1);
- mag_last_free_msize = (uintptr_t)mag_last_free & (SMALL_QUANTUM - 1);
- }
- } else {
- for (mag_index = 0; mag_index < szone->num_small_magazines; mag_index++) {
- if ((void *)range.address == (small_mag_base + mag_index)->mag_last_free_rgn) {
- mag_last_free = (small_mag_base + mag_index)->mag_last_free;
- if (mag_last_free) {
- mag_last_free_ptr = (uintptr_t)mag_last_free & ~(SMALL_QUANTUM - 1);
- mag_last_free_msize = (uintptr_t)mag_last_free & (SMALL_QUANTUM - 1);
- }
- }
+ int cached_free_blocks = 0;
+#if CONFIG_SMALL_CACHE
+ // 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->small_rack.num_magazines; mag_index++) {
+ if ((void *)range.address == (small_mag_base + mag_index)->mag_last_free_rgn) {
+ cached_free_blocks++;
}
}
+#endif // CONFIG_SMALL_CACHE
block_header = (msize_t *)(mapped_region + SMALL_METADATA_START + sizeof(region_trailer_t));
block_index = 0;
@@ -1621,13 +1878,35 @@
block_index += SMALL_MSIZE_FOR_BYTES(small_mag_ptr->mag_bytes_free_at_start);
block_limit -= SMALL_MSIZE_FOR_BYTES(small_mag_ptr->mag_bytes_free_at_end);
}
- while (block_index < block_limit) {
+
+ for (;block_index < block_limit; block_index += msize) {
msize_and_free = block_header[block_index];
msize = msize_and_free & ~SMALL_IS_FREE;
- if (!(msize_and_free & SMALL_IS_FREE) &&
- range.address + SMALL_BYTES_FOR_MSIZE(block_index) != mag_last_free_ptr) {
+ if (!msize) {
+ return KERN_FAILURE; // Somethings amiss. Avoid looping at this block_index.
+ }
+ if (!(msize_and_free & SMALL_IS_FREE)) {
+ vm_address_t ptr = range.address + SMALL_BYTES_FOR_MSIZE(block_index);
+#if CONFIG_SMALL_CACHE
+ // 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.
+ boolean_t block_cached = false;
+ if (cached_free_blocks) {
+ for (mag_index = 0; mag_index < szone->small_rack.num_magazines; mag_index++) {
+ if ((void *)ptr == (small_mag_base + mag_index)->mag_last_free) {
+ block_cached = true;
+ cached_free_blocks--;
+ break;
+ }
+ }
+ }
+ if (block_cached) {
+ continue;
+ }
+#endif // CONFIG_SMALL_CACHE
// Block in use
- buffer[count].address = range.address + SMALL_BYTES_FOR_MSIZE(block_index);
+ buffer[count].address = ptr;
buffer[count].size = SMALL_BYTES_FOR_MSIZE(msize);
count++;
if (count >= MAX_RECORDER_BUFFER) {
@@ -1635,11 +1914,6 @@
count = 0;
}
}
-
- if (!msize) {
- return KERN_FAILURE; // Somethings amiss. Avoid looping at this block_index.
- }
- block_index += msize;
}
if (count) {
recorder(task, context, MALLOC_PTR_IN_USE_RANGE_TYPE, buffer, count);
@@ -1652,10 +1926,10 @@
}
static void *
-small_malloc_from_free_list(szone_t *szone, magazine_t *small_mag_ptr, mag_index_t mag_index, msize_t msize)
+small_malloc_from_free_list(rack_t *rack, magazine_t *small_mag_ptr, mag_index_t mag_index, msize_t msize)
{
msize_t this_msize;
- grain_t slot = (msize <= szone->num_small_slots) ? msize - 1 : szone->num_small_slots - 1;
+ grain_t slot = SMALL_FREE_SLOT_FOR_MSIZE(rack, msize);
free_list_t *free_list = small_mag_ptr->mag_free_list;
free_list_t *the_slot = free_list + slot;
free_list_t *limit;
@@ -1668,58 +1942,50 @@
CHECK_MAGAZINE_PTR_LOCKED(szone, small_mag_ptr, __PRETTY_FUNCTION__);
// Look for an exact match by checking the freelist for this msize.
- //
- if (small_free_list_get_ptr(szone, *the_slot)) {
- ptr = small_free_list_get_ptr(szone, *the_slot);
+ if (small_free_list_get_ptr(*the_slot)) {
+ ptr = small_free_list_get_ptr(*the_slot);
this_msize = msize;
- small_free_list_remove_ptr(szone, small_mag_ptr, *the_slot, msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, *the_slot, msize);
goto return_small_alloc;
}
// Mask off the bits representing slots holding free blocks smaller than
// the size we need. If there are no larger free blocks, try allocating
// from the free space at the end of the small region.
- if (szone->is_largemem) {
- // BITMAPN_CTZ implementation
- unsigned idx = slot >> 5;
- bitmap = 0;
- unsigned mask = ~((1 << (slot & 31)) - 1);
- for (; idx < SMALL_BITMAP_WORDS; ++idx) {
- bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
- if (bitmap != 0) {
- break;
- }
- mask = ~0U;
- }
- // Check for fallthrough: No bits set in bitmap
- if ((bitmap == 0) && (idx == SMALL_BITMAP_WORDS)) {
- goto try_small_from_end;
- }
-
- // Start looking at the first set bit, plus 32 bits for every word of
- // zeroes or entries that were too small.
- slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
- } else {
- bitmap = small_mag_ptr->mag_bitmap[0] & ~((1 << slot) - 1);
- if (!bitmap) {
- goto try_small_from_end;
- }
-
- slot = BITMAP32_CTZ((&bitmap));
- }
+ //
+ // BITMAPN_CTZ implementation
+ unsigned idx = slot >> 5;
+ bitmap = 0;
+ unsigned mask = ~((1 << (slot & 31)) - 1);
+ for (; idx < SMALL_FREELIST_BITMAP_WORDS(rack); ++idx) {
+ bitmap = small_mag_ptr->mag_bitmap[idx] & mask;
+ if (bitmap != 0) {
+ break;
+ }
+ mask = ~0U;
+ }
+ // Check for fallthrough: No bits set in bitmap
+ if ((bitmap == 0) && (idx == SMALL_FREELIST_BITMAP_WORDS(rack))) {
+ goto try_small_from_end;
+ }
+
+ // Start looking at the first set bit, plus 32 bits for every word of
+ // zeroes or entries that were too small.
+ slot = BITMAP32_CTZ((&bitmap)) + (idx * 32);
+
// FIXME: Explain use of - 1 here, last slot has special meaning
- limit = free_list + szone->num_small_slots - 1;
+ limit = free_list + SMALL_FREE_SLOT_COUNT(rack) - 1;
free_list += slot;
// Attempt to pull off the free_list slot that we now think is full.
- if ((ptr = small_free_list_get_ptr(szone, *free_list))) {
+ if ((ptr = small_free_list_get_ptr(*free_list))) {
this_msize = SMALL_PTR_SIZE(ptr);
- small_free_list_remove_ptr(szone, small_mag_ptr, *free_list, this_msize);
+ small_free_list_remove_ptr(rack, small_mag_ptr, *free_list, this_msize);
goto add_leftover_and_proceed;
}
#if DEBUG_MALLOC
- malloc_printf("in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
+ malloc_report(ASL_LEVEL_ERR, "in small_malloc_from_free_list(), mag_bitmap out of sync, slot=%d\n", slot);
#endif
try_small_from_end:
@@ -1759,10 +2025,10 @@
leftover_ptr = (unsigned char *)ptr + SMALL_BYTES_FOR_MSIZE(msize);
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_printf("in small_malloc_from_free_list(), adding leftover ptr=%p, this_msize=%d\n", ptr, this_msize);
- }
-#endif
- small_free_list_add_ptr(szone, small_mag_ptr, leftover_ptr, leftover_msize);
+ malloc_report(ASL_LEVEL_INFO, "in small_malloc_from_free_list(), adding leftover ptr=%p, this_msize=%d\n", ptr, this_msize);
+ }
+#endif
+ small_free_list_add_ptr(rack, small_mag_ptr, leftover_ptr, leftover_msize);
this_msize = msize;
}
@@ -1770,10 +2036,12 @@
small_mag_ptr->mag_num_objects++;
small_mag_ptr->mag_num_bytes_in_objects += SMALL_BYTES_FOR_MSIZE(this_msize);
- // Update this region's bytes in use count
- region_trailer_t *node = REGION_TRAILER_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr));
- size_t bytes_used = node->bytes_used + SMALL_BYTES_FOR_MSIZE(this_msize);
- node->bytes_used = (unsigned int)bytes_used;
+ // Check that the region cookie is intact and update the region's bytes in use count
+ region_t *region = SMALL_REGION_FOR_PTR(ptr);
+ region_trailer_t *trailer = REGION_TRAILER_FOR_SMALL_REGION(region);
+ region_check_cookie(region, trailer);
+ size_t bytes_used = trailer->bytes_used + SMALL_BYTES_FOR_MSIZE(this_msize);
+ trailer->bytes_used = (unsigned int)bytes_used;
// Emptiness discriminant
if (bytes_used < DENSITY_THRESHOLD(SMALL_REGION_PAYLOAD_BYTES)) {
@@ -1782,11 +2050,11 @@
} else {
/* Region has crossed threshold from sparsity to density. Mark in not "suitable" on the
* recirculation candidates list. */
- node->recirc_suitable = FALSE;
+ trailer->recirc_suitable = FALSE;
}
#if DEBUG_MALLOC
if (LOG(szone, ptr)) {
- malloc_printf("in small_malloc_from_free_list(), ptr=%p, this_msize=%d, msize=%d\n", ptr, this_msize, msize);
+ malloc_report(ASL_LEVEL_INFO, "in small_malloc_from_free_list(), ptr=%p, this_msize=%d, msize=%d\n", ptr, this_msize, msize);
}
#endif
small_meta_header_set_in_use(SMALL_META_HEADER_FOR_PTR(ptr), SMALL_META_INDEX_FOR_PTR(ptr), this_msize);
@@ -1794,26 +2062,26 @@
}
void *
-small_malloc_should_clear(szone_t *szone, msize_t msize, boolean_t cleared_requested)
+small_malloc_should_clear(rack_t *rack, msize_t msize, boolean_t cleared_requested)
{
void *ptr;
- mag_index_t mag_index = mag_get_thread_index(szone);
- magazine_t *small_mag_ptr = &(szone->small_magazines[mag_index]);
-
- MALLOC_TRACE(TRACE_small_malloc, (uintptr_t)szone, SMALL_BYTES_FOR_MSIZE(msize), (uintptr_t)small_mag_ptr, cleared_requested);
-
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
+ mag_index_t mag_index = small_mag_get_thread_index() % rack->num_magazines;
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
+
+ MALLOC_TRACE(TRACE_small_malloc, (uintptr_t)rack, SMALL_BYTES_FOR_MSIZE(msize), (uintptr_t)small_mag_ptr, cleared_requested);
+
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
#if CONFIG_SMALL_CACHE
- ptr = (void *)small_mag_ptr->mag_last_free;
-
- if ((((uintptr_t)ptr) & (SMALL_QUANTUM - 1)) == msize) {
+ ptr = small_mag_ptr->mag_last_free;
+
+ if (small_mag_ptr->mag_last_free_msize == msize) {
// we have a winner
small_mag_ptr->mag_last_free = NULL;
+ small_mag_ptr->mag_last_free_msize = 0;
small_mag_ptr->mag_last_free_rgn = NULL;
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
- ptr = (void *)((uintptr_t)ptr & ~(SMALL_QUANTUM - 1));
if (cleared_requested) {
memset(ptr, 0, SMALL_BYTES_FOR_MSIZE(msize));
}
@@ -1822,9 +2090,9 @@
#endif /* CONFIG_SMALL_CACHE */
while (1) {
- ptr = small_malloc_from_free_list(szone, small_mag_ptr, mag_index, msize);
+ ptr = small_malloc_from_free_list(rack, small_mag_ptr, mag_index, msize);
if (ptr) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
if (cleared_requested) {
memset(ptr, 0, SMALL_BYTES_FOR_MSIZE(msize));
@@ -1832,10 +2100,11 @@
return ptr;
}
- if (small_get_region_from_depot(szone, small_mag_ptr, mag_index, msize)) {
- ptr = small_malloc_from_free_list(szone, small_mag_ptr, mag_index, msize);
+#if CONFIG_RECIRC_DEPOT
+ if (small_get_region_from_depot(rack, small_mag_ptr, mag_index, msize)) {
+ ptr = small_malloc_from_free_list(rack, small_mag_ptr, mag_index, msize);
if (ptr) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
if (cleared_requested) {
memset(ptr, 0, SMALL_BYTES_FOR_MSIZE(msize));
@@ -1843,6 +2112,7 @@
return ptr;
}
}
+#endif // CONFIG_RECIRC_DEPOT
// The magazine is exhausted. A new region (heap) must be allocated to satisfy this call to malloc().
// The allocation, an mmap() system call, will be performed outside the magazine spin locks by the first
@@ -1856,51 +2126,90 @@
// time to create a new region (do this outside the magazine lock)
small_mag_ptr->alloc_underway = TRUE;
OSMemoryBarrier();
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
- fresh_region = allocate_pages_securely(szone, SMALL_REGION_SIZE, SMALL_BLOCKS_ALIGN, VM_MEMORY_MALLOC_SMALL);
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
-
- MAGMALLOC_ALLOCREGION((void *)szone, (int)mag_index, fresh_region, SMALL_REGION_SIZE); // DTrace USDT Probe
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+ fresh_region = mvm_allocate_pages_securely(SMALL_REGION_SIZE, SMALL_BLOCKS_ALIGN, VM_MEMORY_MALLOC_SMALL, rack->debug_flags);
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
+
+ // DTrace USDT Probe
+ MAGMALLOC_ALLOCREGION(SMALL_SZONE_FROM_RACK(rack), (int)mag_index, fresh_region, SMALL_REGION_SIZE);
if (!fresh_region) { // out of memory!
small_mag_ptr->alloc_underway = FALSE;
OSMemoryBarrier();
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
return NULL;
}
- ptr = small_malloc_from_region_no_lock(szone, small_mag_ptr, mag_index, msize, fresh_region);
+ region_set_cookie(REGION_TRAILER_FOR_SMALL_REGION(fresh_region));
+ ptr = small_malloc_from_region_no_lock(rack, small_mag_ptr, mag_index, msize, fresh_region);
// we don't clear because this freshly allocated space is pristine
small_mag_ptr->alloc_underway = FALSE;
OSMemoryBarrier();
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
return ptr;
} else {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
yield();
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
}
}
/* NOTREACHED */
}
+size_t
+small_size(rack_t *rack, const void *ptr)
+{
+ if (small_region_for_ptr_no_lock(rack, ptr)) {
+ if (SMALL_META_INDEX_FOR_PTR(ptr) >= NUM_SMALL_BLOCKS) {
+ return 0;
+ }
+ msize_t msize_and_free = *SMALL_METADATA_FOR_PTR(ptr);
+ if (msize_and_free & SMALL_IS_FREE) {
+ return 0;
+ }
+#if CONFIG_SMALL_CACHE
+ {
+ mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr));
+ if (DEPOT_MAGAZINE_INDEX != mag_index) {
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
+
+ if (ptr == small_mag_ptr->mag_last_free) {
+ return 0;
+ }
+ } else {
+ for (mag_index = 0; mag_index < rack->num_magazines; mag_index++) {
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
+
+ if (ptr == small_mag_ptr->mag_last_free) {
+ return 0;
+ }
+ }
+ }
+ }
+#endif
+ return SMALL_BYTES_FOR_MSIZE(msize_and_free);
+ }
+
+ return 0;
+}
+
static MALLOC_NOINLINE void
-free_small_botch(szone_t *szone, void *ptr)
+free_small_botch(rack_t *rack, void *ptr)
{
mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr));
- magazine_t *small_mag_ptr = &(szone->small_magazines[mag_index]);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
- szone_error(szone, 1, "double free", ptr, NULL);
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
+ malloc_zone_error(rack->debug_flags, true, "double free for ptr %p\n", ptr);
}
void
-free_small(szone_t *szone, void *ptr, region_t small_region, size_t known_size)
+free_small(rack_t *rack, void *ptr, region_t small_region, size_t known_size)
{
msize_t msize;
mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(SMALL_REGION_FOR_PTR(ptr));
- magazine_t *small_mag_ptr = &(szone->small_magazines[mag_index]);
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
// ptr is known to be in small_region
if (known_size) {
@@ -1908,40 +2217,42 @@
} else {
msize = SMALL_PTR_SIZE(ptr);
if (SMALL_PTR_IS_FREE(ptr)) {
- free_small_botch(szone, ptr);
+ free_small_botch(rack, ptr);
return;
}
}
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
#if CONFIG_SMALL_CACHE
// Depot does not participate in CONFIG_SMALL_CACHE since it can't be directly malloc()'d
if (DEPOT_MAGAZINE_INDEX != mag_index) {
void *ptr2 = small_mag_ptr->mag_last_free; // Might be NULL
+ msize_t msize2 = small_mag_ptr->mag_last_free_msize;
region_t rgn2 = small_mag_ptr->mag_last_free_rgn;
/* check that we don't already have this pointer in the cache */
- if (ptr == (void *)((uintptr_t)ptr2 & ~(SMALL_QUANTUM - 1))) {
- free_small_botch(szone, ptr);
+ if (ptr == ptr2) {
+ free_small_botch(rack, ptr);
return;
}
- if ((szone->debug_flags & MALLOC_DO_SCRIBBLE) && msize) {
+ if ((rack->debug_flags & MALLOC_DO_SCRIBBLE) && msize) {
memset(ptr, SCRABBLE_BYTE, SMALL_BYTES_FOR_MSIZE(msize));
}
- small_mag_ptr->mag_last_free = (void *)(((uintptr_t)ptr) | msize);
+ small_mag_ptr->mag_last_free = ptr;
+ small_mag_ptr->mag_last_free_msize = msize;
small_mag_ptr->mag_last_free_rgn = small_region;
if (!ptr2) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
CHECK(szone, __PRETTY_FUNCTION__);
return;
}
- msize = (uintptr_t)ptr2 & (SMALL_QUANTUM - 1);
- ptr = (void *)(((uintptr_t)ptr2) & ~(SMALL_QUANTUM - 1));
+ msize = msize2;
+ ptr = ptr2;
small_region = rgn2;
}
#endif /* CONFIG_SMALL_CACHE */
@@ -1955,65 +2266,99 @@
while (mag_index != (refreshed_index = trailer->mag_index)) { // Note assignment
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
mag_index = refreshed_index;
- small_mag_ptr = &(szone->small_magazines[mag_index]);
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
- }
-
- if (small_free_no_lock(szone, small_mag_ptr, mag_index, small_region, ptr, msize)) {
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ small_mag_ptr = &(rack->magazines[mag_index]);
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
+ }
+
+ if (small_free_no_lock(rack, small_mag_ptr, mag_index, small_region, ptr, msize)) {
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
}
CHECK(szone, __PRETTY_FUNCTION__);
}
void
-print_small_free_list(szone_t *szone)
+print_small_free_list(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, rack_t *rack)
{
free_list_t ptr;
_SIMPLE_STRING b = _simple_salloc();
mag_index_t mag_index;
if (b) {
- _simple_sappend(b, "small free sizes:\n");
- for (mag_index = -1; mag_index < szone->num_small_magazines; mag_index++) {
+ rack_t *mapped_rack;
+ magazine_t *mapped_magazines;
+ if (reader(task, (vm_address_t)rack, sizeof(struct rack_s),
+ (void **)&mapped_rack)) {
+ printer("Failed to map small rack\n");
+ return;
+ }
+ if (reader(task, (vm_address_t)mapped_rack->magazines,
+ mapped_rack->num_magazines * sizeof(magazine_t),
+ (void **)&mapped_magazines)) {
+ printer("Failed to map small rack magazines\n");
+ return;
+ }
+
+ _simple_sappend(b, "small free sizes:\n");
+ grain_t free_slots = SMALL_FREE_SLOT_COUNT(mapped_rack);
+ for (mag_index = -1; mag_index < mapped_rack->num_magazines;
+ mag_index++) {
grain_t slot = 0;
- _simple_sprintf(b, "\tMagazine %d: ", mag_index);
- while (slot < szone->num_small_slots) {
- ptr = szone->small_magazines[mag_index].mag_free_list[slot];
- if (small_free_list_get_ptr(szone, ptr)) {
- _simple_sprintf(b, "%s%y[%d]; ", (slot == szone->num_small_slots - 1) ? ">=" : "", (slot + 1) * SMALL_QUANTUM,
- small_free_list_count(szone, ptr));
+ if (mag_index == -1) {
+ _simple_sprintf(b, "\tRecirc depot: ");
+ } else {
+ _simple_sprintf(b, "\tMagazine %d: ", mag_index);
+ }
+ while (slot < free_slots) {
+ ptr = mapped_magazines[mag_index].mag_free_list[slot];
+ if (small_free_list_get_ptr_task(task, reader, printer, ptr)) {
+ _simple_sprintf(b, "%s%y[%d]; ", (slot == free_slots - 1) ?
+ ">=" : "", (slot + 1) * SMALL_QUANTUM,
+ small_free_list_count(task, reader, printer,
+ rack, ptr));
}
slot++;
}
_simple_sappend(b, "\n");
}
- _malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
+ printer("%s\n", _simple_string(b));
_simple_sfree(b);
}
}
void
-print_small_region(szone_t *szone, boolean_t verbose, region_t region, size_t bytes_at_start, size_t bytes_at_end)
+print_small_region(task_t task, memory_reader_t reader,
+ print_task_printer_t printer, szone_t *szone, int level,
+ region_t region, size_t bytes_at_start, size_t bytes_at_end)
{
unsigned counts[1024];
unsigned in_use = 0;
uintptr_t start = (uintptr_t)SMALL_REGION_ADDRESS(region);
uintptr_t current = start + bytes_at_start;
uintptr_t limit = (uintptr_t)SMALL_REGION_END(region) - bytes_at_end;
+ uintptr_t mapped_start;
msize_t msize_and_free;
msize_t msize;
unsigned ci;
_SIMPLE_STRING b;
uintptr_t pgTot = 0;
+ if (reader(task, (vm_address_t)start, SMALL_REGION_SIZE,
+ (void **)&mapped_start)) {
+ printer("Failed to map small region at %p\n", start);
+ return;
+ }
+ off_t start_offset = mapped_start - start;
+ region_t mapped_region = (region_t)mapped_start;
+
if (region == HASHRING_REGION_DEALLOCATED) {
if ((b = _simple_salloc()) != NULL) {
_simple_sprintf(b, "Small region [unknown address] was returned to the OS\n");
- _malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
+ printer("%s\n", _simple_string(b));
_simple_sfree(b);
}
return;
@@ -2021,10 +2366,11 @@
memset(counts, 0, sizeof(counts));
while (current < limit) {
- msize_and_free = *SMALL_METADATA_FOR_PTR(current);
+ msize_and_free = *(uintptr_t *)((char *)SMALL_METADATA_FOR_PTR(current) + start_offset);
msize = msize_and_free & ~SMALL_IS_FREE;
if (!msize) {
- malloc_printf("*** error with %p: msize=%d\n", (void *)current, (unsigned)msize);
+ printer("*** error with %p: msize=%d, free: %x\n", (void *)current,
+ (unsigned)msize, msize_and_free & SMALL_IS_FREE);
break;
}
if (!(msize_and_free & SMALL_IS_FREE)) {
@@ -2034,8 +2380,10 @@
}
in_use++;
} else {
- uintptr_t pgLo = round_page_quanta(current + sizeof(free_list_t) + sizeof(msize_t));
- uintptr_t pgHi = trunc_page_quanta(current + SMALL_BYTES_FOR_MSIZE(msize) - sizeof(msize_t));
+ uintptr_t pgLo = round_page_quanta(current +
+ sizeof(free_list_t) + sizeof(msize_t));
+ uintptr_t pgHi = trunc_page_quanta(current +
+ SMALL_BYTES_FOR_MSIZE(msize) - sizeof(msize_t));
if (pgLo < pgHi) {
pgTot += (pgHi - pgLo);
@@ -2043,74 +2391,95 @@
}
current += SMALL_BYTES_FOR_MSIZE(msize);
}
+
if ((b = _simple_salloc()) != NULL) {
- _simple_sprintf(b, "Small region [%p-%p, %y] \t", (void *)start, SMALL_REGION_END(region), (int)SMALL_REGION_SIZE);
- _simple_sprintf(b, "Magazine=%d \t", MAGAZINE_INDEX_FOR_SMALL_REGION(region));
- _simple_sprintf(b, "Allocations in use=%d \t Bytes in use=%ly \t", in_use, BYTES_USED_FOR_SMALL_REGION(region));
+ mag_index_t mag_index = MAGAZINE_INDEX_FOR_SMALL_REGION(mapped_region);
+ _simple_sprintf(b, "Small region [%p-%p, %y] \t", (void *)start,
+ SMALL_REGION_END(region), (int)SMALL_REGION_SIZE);
+ if (mag_index == DEPOT_MAGAZINE_INDEX) {
+ _simple_sprintf(b, "Recirc depot \t");
+ } else {
+ _simple_sprintf(b, "Magazine=%d \t", mag_index);
+ }
+ _simple_sprintf(b, "Allocations in use=%d \t Bytes in use=%ly (%d%%) \t",
+ in_use, BYTES_USED_FOR_SMALL_REGION(mapped_region),
+ (int)(100.0F * BYTES_USED_FOR_SMALL_REGION(mapped_region))/SMALL_REGION_SIZE);
if (bytes_at_end || bytes_at_start) {
_simple_sprintf(b, "Untouched=%ly ", bytes_at_end + bytes_at_start);
}
- if (DEPOT_MAGAZINE_INDEX == MAGAZINE_INDEX_FOR_SMALL_REGION(region)) {
+ if (mag_index == DEPOT_MAGAZINE_INDEX) {
_simple_sprintf(b, "Advised MADV_FREE=%ly", pgTot);
} else {
_simple_sprintf(b, "Fragments subject to reclamation=%ly", pgTot);
- }
- if (verbose && in_use) {
+#if CONFIG_RECIRC_DEPOT
+ _simple_sprintf(b, small_region_below_recirc_threshold(mapped_region) ?
+ "\tEmpty enough to be moved to recirc depot" :
+ "\tNot empty enough to be moved to recirc depot");
+#endif // CONFIG_RECIRC_DEPOT
+ }
+ if (level >= MALLOC_VERBOSE_PRINT_LEVEL && in_use) {
_simple_sappend(b, "\n\tSizes in use: ");
for (ci = 0; ci < 1024; ci++) {
if (counts[ci]) {
- _simple_sprintf(b, "%d[%d] ", SMALL_BYTES_FOR_MSIZE(ci), counts[ci]);
+ _simple_sprintf(b, "%y[%d] ", SMALL_BYTES_FOR_MSIZE(ci),
+ counts[ci]);
}
}
}
- _malloc_printf(MALLOC_PRINTF_NOLOG | MALLOC_PRINTF_NOPREFIX, "%s\n", _simple_string(b));
+ printer("%s\n", _simple_string(b));
_simple_sfree(b);
}
}
+static char *small_freelist_fail_msg = "check: small free list incorrect";
+
+#define SMALL_FREELIST_FAIL(fmt, ...) \
+ malloc_zone_check_fail(small_freelist_fail_msg, \
+ " (slot=%u), counter=%d\n" fmt, slot, counter, __VA_ARGS__);
+
boolean_t
-small_free_list_check(szone_t *szone, grain_t slot)
+small_free_list_check(rack_t *rack, grain_t slot, unsigned counter)
{
mag_index_t mag_index;
- for (mag_index = -1; mag_index < szone->num_small_magazines; mag_index++) {
- magazine_t *small_mag_ptr = &(szone->small_magazines[mag_index]);
- SZONE_MAGAZINE_PTR_LOCK(szone, small_mag_ptr);
+ for (mag_index = -1; mag_index < rack->num_magazines; mag_index++) {
+ magazine_t *small_mag_ptr = &(rack->magazines[mag_index]);
+ SZONE_MAGAZINE_PTR_LOCK(small_mag_ptr);
unsigned count = 0;
- free_list_t current = szone->small_magazines[mag_index].mag_free_list[slot];
+ free_list_t current = rack->magazines[mag_index].mag_free_list[slot];
free_list_t previous = (free_list_t){ .p = NULL };
msize_t msize_and_free;
void *ptr = NULL;
- while ((ptr = small_free_list_get_ptr(szone, current))) {
+ while ((ptr = small_free_list_get_ptr(current))) {
msize_and_free = *SMALL_METADATA_FOR_PTR(ptr);
if (!(msize_and_free & SMALL_IS_FREE)) {
- malloc_printf("*** in-use ptr in free list slot=%d count=%d ptr=%p\n", slot, count, ptr);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SMALL_FREELIST_FAIL("*** in-use ptr in free list slot=%u count=%d ptr=%p\n", slot, count, ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
return 0;
}
if (((uintptr_t)ptr) & (SMALL_QUANTUM - 1)) {
- malloc_printf("*** unaligned ptr in free list slot=%d count=%d ptr=%p\n", slot, count, ptr);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ SMALL_FREELIST_FAIL("*** unaligned ptr in free list slot=%u count=%d ptr=%p\n", slot, count, ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
return 0;
}
- if (!small_region_for_ptr_no_lock(szone, ptr)) {
- malloc_printf("*** ptr not in szone slot=%d count=%d ptr=%p\n", slot, count, ptr);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ if (!small_region_for_ptr_no_lock(rack, ptr)) {
+ SMALL_FREELIST_FAIL("*** ptr not in szone slot=%d count=%d ptr=%p\n", slot, count, ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
return 0;
}
- if (small_free_list_get_previous(szone, current).p != previous.p) {
- malloc_printf("*** previous incorrectly set slot=%d count=%d ptr=%p\n", slot, count, ptr);
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+ if (small_free_list_get_previous(rack, current).p != previous.p) {
+ SMALL_FREELIST_FAIL("*** previous incorrectly set slot=%u count=%d ptr=%p\n", slot, count, ptr);
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
return 0;
}
previous = current;
- current = small_free_list_get_next(szone, current);
+ current = small_free_list_get_next(rack, current);
count++;
}
-
- SZONE_MAGAZINE_PTR_UNLOCK(szone, small_mag_ptr);
+
+ SZONE_MAGAZINE_PTR_UNLOCK(small_mag_ptr);
}
return 1;
}