Loading...
--- libmalloc/libmalloc-166.251.2/src/magazine_lite.c
+++ libmalloc/libmalloc-116/src/magazine_lite.c
@@ -25,8 +25,6 @@
typedef uint64_t malloc_stack_id;
-uint64_t max_lite_mallocs = 0;
-
static malloc_stack_id
get_stack_id_from_ptr(void *ptr, size_t ptr_size)
{
@@ -65,7 +63,7 @@
__malloc_unlock_stack_logging();
if (stack_id == __invalid_stack_id) {
- malloc_report(ASL_LEVEL_ERR, "bad stack id. turning off stack logging\n");
+ malloc_printf("bad stack id. turning off stack logging\n");
turn_off_stack_logging();
} else {
set_stack_id_in_ptr(ptr, requested_size, ptr_size, stack_id);
@@ -95,7 +93,6 @@
{
szone_t *szone = (szone_t *) zone;
void* p = NULL;
- static uint64_t num_mallocs = 0;
if (stack_logging_lite_enabled) {
__prepare_to_log_stacks(true); // do this again in case stack logging was postponed
@@ -105,14 +102,6 @@
if (p) {
add_stack_to_ptr(szone, size, p);
}
-
- // this value doesn't need to be exact, so no need for atomic operations
- num_mallocs++;
-
- if (max_lite_mallocs > 0 && num_mallocs > max_lite_mallocs) {
- malloc_report(ASL_LEVEL_ERR, "lite allocations exceeded limit. disabling lite mode\n");
- disable_stack_logging_lite();
- }
} else {
p = szone->helper_zone->basic_zone.malloc((malloc_zone_t *) szone->helper_zone, size);
}
@@ -127,16 +116,34 @@
void *p = NULL;
if (stack_logging_lite_enabled) {
- size_t total_bytes;
-
- if (calloc_get_size(num_items, size, sizeof(malloc_stack_id), &total_bytes)) {
- return NULL;
+ size_t total_bytes = (num_items * size) + sizeof(malloc_stack_id);
+
+ if (num_items > 1) {
+
+#if __LP64__ /* size_t is uint64_t */
+ if ((num_items | size) & 0xffffffff00000000ul) {
+ // num_items or size equals or exceeds sqrt(2^64) == 2^32, appeal to wider arithmetic
+ __uint128_t product = (((__uint128_t)num_items) * ((__uint128_t)size)) + sizeof(malloc_stack_id);
+ if ((uint64_t)(product >> 64)) { // compiles to test on upper register of register pair
+ return NULL;
+ }
+ }
+#else /* size_t is uint32_t */
+ if ((num_items | size) & 0xffff0000ul) {
+ // num_items or size equals or exceeds sqrt(2^32) == 2^16, appeal to wider arithmetic
+ uint64_t product = ((uint64_t)num_items) * ((uint64_t)size) + sizeof(malloc_stack_id);;
+ if ((uint32_t)(product >> 32)) { // compiles to test on upper register of register pair
+ return NULL;
+ }
+ }
+#endif
+
}
p = szone_malloc_should_clear(szone, total_bytes, 1);
if (p) {
- add_stack_to_ptr(szone, total_bytes - sizeof(malloc_stack_id), p);
+ add_stack_to_ptr(szone, num_items * size, p);
}
} else {
p = szone->helper_zone->basic_zone.calloc((malloc_zone_t *) szone->helper_zone, num_items, size);
@@ -187,11 +194,6 @@
stack_logging_lite_free(zone, ptr);
}
-// Three paths:
-// 1. do a szone_realloc with padding and add stack id
-// 2. do a szone_realloc on the helper zone
-// 3. do a manual free / malloc
-
static void *
stack_logging_lite_realloc(malloc_zone_t *zone, void *ptr, size_t new_size)
{
@@ -210,9 +212,6 @@
__decrement_table_slot_refcount(stack_id, old_size);
add_stack_to_ptr(szone, new_size, new_ptr);
}
- } else if (!old_size && !stack_logging_lite_enabled) {
- // we don't own the pointer and lite mode is disabled, so just pass the realloc on to the helper zone
- return szone->helper_zone->basic_zone.realloc((malloc_zone_t *) szone->helper_zone, ptr, new_size);
} else {
// otherwise perform the realloc by hand:
// 1. malloc new ptr