#include "AutoDefs.h"
#include "AutoEnvironment.h"
#include "AutoZone.h"
namespace Auto {
uint64_t micro_time() {
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return ((uint64_t)usage.ru_utime.tv_sec * (uint64_t)1000000) + (uint64_t)usage.ru_utime.tv_usec +
((uint64_t)usage.ru_stime.tv_sec * (uint64_t)1000000) + (uint64_t)usage.ru_stime.tv_usec;
}
double nano_time() {
static mach_timebase_info_data_t timebase_info;
static double scale = 1.0;
static uint64_t delta;
if (!timebase_info.denom) {
mach_timebase_info(&timebase_info);
scale = ((double)timebase_info.numer / (double)timebase_info.denom) * 1.0E-9;
delta = mach_absolute_time();
}
return (double)(mach_absolute_time() - delta) * scale;
}
mach_timebase_info_data_t &StopWatch::cached_timebase_info() {
static mach_timebase_info_data_t timebase_info;
if (!timebase_info.denom) mach_timebase_info(&timebase_info);
return timebase_info;
}
void *aux_malloc(size_t size) {
ASSERTION(aux_zone);
void *new_ptr = malloc_zone_malloc(aux_zone, size);
#if defined(DEBUG)
if (Environment::print_allocs) malloc_printf("malloc_zone_malloc @%p %d\n", new_ptr, size);
#endif
return new_ptr;
}
void *aux_calloc(size_t count, size_t size) {
ASSERTION(aux_zone);
void *new_ptr = malloc_zone_calloc(aux_zone, count, size);
#if defined(DEBUG)
if (Environment::print_allocs) malloc_printf("malloc_zone_calloc @%p %d\n", new_ptr, count * size);
#endif
return new_ptr;
}
void *aux_valloc(size_t size) {
ASSERTION(aux_zone);
void *new_ptr = malloc_zone_valloc(aux_zone, size);
#if defined(DEBUG)
if (Environment::print_allocs) malloc_printf("malloc_zone_valloc @%p %d\n", new_ptr, size);
#endif
return new_ptr;
}
void *aux_realloc(void *ptr, size_t size) {
ASSERTION(aux_zone);
void *new_ptr = malloc_zone_realloc(aux_zone, ptr, size);
#if defined(DEBUG)
if (Environment::print_allocs) malloc_printf("malloc_zone_realloc @%p %d\n", new_ptr, size);
#endif
return new_ptr;
}
void aux_free(void *ptr) {
ASSERTION(aux_zone);
#if defined(DEBUG)
if (Environment::print_allocs) malloc_printf("malloc_zone_free @%p\n", ptr);
#endif
malloc_zone_free(aux_zone, ptr);
}
};