#include "utils/commons.h"
#include "system/mm_allocator.h"
#include "wavefront.h"
void wavefront_allocate(
wavefront_t* const wavefront,
const int wf_elements_allocated,
const bool allocate_backtrace,
mm_allocator_t* const mm_allocator) {
wavefront->wf_elements_allocated = wf_elements_allocated;
wavefront->offsets_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,wf_offset_t,false);
if (allocate_backtrace) {
wavefront->bt_pcigar_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,pcigar_t,false);
wavefront->bt_prev_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,bt_block_idx_t,false);
} else {
wavefront->bt_pcigar_mem = NULL;
}
}
void wavefront_resize(
wavefront_t* const wavefront,
const int wf_elements_allocated,
mm_allocator_t* const mm_allocator) {
wavefront->wf_elements_allocated = wf_elements_allocated;
mm_allocator_free(mm_allocator,wavefront->offsets_mem);
wavefront->offsets_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,wf_offset_t,false);
if (wavefront->bt_pcigar_mem) {
mm_allocator_free(mm_allocator,wavefront->bt_pcigar_mem);
mm_allocator_free(mm_allocator,wavefront->bt_prev_mem);
wavefront->bt_pcigar_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,pcigar_t,false);
wavefront->bt_prev_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,bt_block_idx_t,false);
}
}
void wavefront_free(
wavefront_t* const wavefront,
mm_allocator_t* const mm_allocator) {
mm_allocator_free(mm_allocator,wavefront->offsets_mem);
if (wavefront->bt_pcigar_mem) {
mm_allocator_free(mm_allocator,wavefront->bt_pcigar_mem);
mm_allocator_free(mm_allocator,wavefront->bt_prev_mem);
}
}
void wavefront_init(
wavefront_t* const wavefront,
const int min_lo,
const int max_hi) {
wavefront->null = false;
wavefront->lo = 1;
wavefront->hi = -1;
wavefront->offsets = wavefront->offsets_mem - min_lo; if (wavefront->bt_pcigar_mem) {
wavefront->bt_occupancy_max = 0;
wavefront->bt_pcigar = wavefront->bt_pcigar_mem - min_lo; wavefront->bt_prev = wavefront->bt_prev_mem - min_lo; }
wavefront->wf_elements_allocated_min = min_lo;
wavefront->wf_elements_allocated_max = max_hi;
wavefront->wf_elements_init_min = 0;
wavefront->wf_elements_init_max = 0;
}
void wavefront_init_null(
wavefront_t* const wavefront,
const int min_lo,
const int max_hi) {
wavefront->null = true;
wavefront->lo = 1;
wavefront->hi = -1;
wavefront->offsets = wavefront->offsets_mem - min_lo; if (wavefront->bt_pcigar_mem) {
wavefront->bt_occupancy_max = 0;
wavefront->bt_pcigar = wavefront->bt_pcigar_mem - min_lo; wavefront->bt_prev = wavefront->bt_prev_mem - min_lo; }
const int wf_elements = WAVEFRONT_LENGTH(min_lo,max_hi);
int i;
for (i=0;i<wf_elements;++i) {
wavefront->offsets_mem[i] = WAVEFRONT_OFFSET_NULL;
}
if (wavefront->bt_pcigar_mem) { memset(wavefront->bt_pcigar_mem,0,wf_elements*sizeof(pcigar_t));
memset(wavefront->bt_prev_mem,0,wf_elements*sizeof(bt_block_idx_t));
}
wavefront->wf_elements_allocated_min = min_lo;
wavefront->wf_elements_allocated_max = max_hi;
wavefront->wf_elements_init_min = min_lo;
wavefront->wf_elements_init_max = max_hi;
}
void wavefront_init_victim(
wavefront_t* const wavefront,
const int min_lo,
const int max_hi) {
wavefront_init(wavefront,min_lo,max_hi);
wavefront->null = true;
}
void wavefront_set_limits(
wavefront_t* const wavefront,
const int lo,
const int hi) {
wavefront->lo = lo;
wavefront->hi = hi;
wavefront->wf_elements_init_min = lo;
wavefront->wf_elements_init_max = hi;
}
uint64_t wavefront_get_size(
wavefront_t* const wavefront) {
uint64_t total_size = wavefront->wf_elements_allocated*sizeof(wf_offset_t);
if (wavefront->bt_pcigar_mem) {
total_size += wavefront->wf_elements_allocated*(sizeof(pcigar_t)+sizeof(bt_block_idx_t));
}
return total_size;
}