void test_introspection(malloc_zone_t *zone, boolean_t exact) { static void* pointers[1000]; BlockRecorderContext context = { 0, 0, zone }; malloc_statistics_t stats; size_t i; // allocate 1000 randomly sized blocks. for (i = 0; i < 1000; ++i) { size_t random_size = (random() & 0x1FFFF); pointers[i] = malloc_zone_malloc(zone, random_size); } // deallocate a random number of the blocks. for (i = 0; i < 1000; ++i) { if (random() & 0x1) { malloc_zone_free(zone, pointers[i]); pointers[i] = NULL; } } // validate that these agree with the values returned by the enumeration APIs. zone->introspect->enumerator(mach_task_self(), &context, MALLOC_PTR_IN_USE_RANGE_TYPE, (vm_address_t)zone, NULL, blockRecorder); malloc_zone_statistics(zone, &stats); if (exact) { assert(stats.blocks_in_use == context.blocks_in_use); assert(stats.size_in_use == context.size_in_use); } else { // the malloc default zone enumerators don't currently match the statistics. assert(stats.blocks_in_use >= context.blocks_in_use); assert(stats.size_in_use >= context.size_in_use); } }
double memUsed(void) { malloc_statistics_t t; malloc_zone_statistics(NULL, &t); return (double)t.max_size_in_use / (1024*1024); }
static PRInt64 GetHeapZone0Used(void *) { malloc_statistics_t stats; malloc_zone_statistics(malloc_default_zone(), &stats); return stats.size_allocated; }
static PRInt64 GetHeapZone0Committed(void *) { malloc_statistics_t stats; malloc_zone_statistics(malloc_default_zone(), &stats); return stats.size_in_use; }