// This loop will convert the ptr field into an index into the ptrs array. // Creating this index allows the trace run to quickly store or retrieve the // allocation. // For free, the ptr field will be index + one, where a zero represents // a free(nullptr) call. // For realloc, the old_pointer field will be index + one, where a zero // represents a realloc(nullptr, XX).
num_ptrs_ = 0;
std::stack<size_t> free_indices;
std::unordered_map<uint64_t, size_t> ptr_to_index; for (size_t i = 0; i < num_entries_; i++) {
memory_trace::Entry* entry = &entries_[i]; switch (entry->type) { case memory_trace::MALLOC: case memory_trace::CALLOC: case memory_trace::MEMALIGN: {
size_t idx = GetIndex(free_indices);
ptr_to_index[entry->ptr] = idx;
entry->ptr = idx; break;
} case memory_trace::REALLOC: { if (entry->u.old_ptr != 0) { auto idx_entry = ptr_to_index.find(entry->u.old_ptr); if (idx_entry == ptr_to_index.end()) {
errx(1, "File Error: Failed to find realloc pointer %" PRIx64, entry->u.old_ptr);
}
size_t old_pointer_idx = idx_entry->second;
free_indices.push(old_pointer_idx);
ptr_to_index.erase(idx_entry);
entry->u.old_ptr = old_pointer_idx + 1;
}
size_t idx = GetIndex(free_indices);
ptr_to_index[entry->ptr] = idx;
entry->ptr = idx; break;
} case memory_trace::FREE: if (entry->ptr != 0) { auto idx_entry = ptr_to_index.find(entry->ptr); if (idx_entry == ptr_to_index.end()) {
errx(1, "File Error: Unable to find free pointer %" PRIx64, entry->ptr);
}
free_indices.push(idx_entry->second);
entry->ptr = idx_entry->second + 1;
ptr_to_index.erase(idx_entry);
} break; case memory_trace::THREAD_DONE: case memory_trace::UNKNOWN: break;
}
} void* map = mmap(nullptr, sizeof(void*) * num_ptrs_, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0); if (map == MAP_FAILED) {
err(1, "mmap failed");
}
ptrs_ = reinterpret_cast<void**>(map);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.