// An entry can be appended by a __cxa_finalize callback. Track the number of appends so we // restart concurrent __cxa_finalize passes.
uint64_t total_appends_;
// Recompact the array if it will save at least one page of memory at the end. bool needs_recompaction() const { return page_end_of_index(size_ - extracted_count_) < page_end_of_index(size_);
}
void AtexitArray::recompact() { if (!needs_recompaction()) return;
set_writable(true, 0, size_);
// Optimization: quickly skip over the initial non-null entries.
size_t src = 0, dst = 0; while (src < size_ && array_[src].fn != nullptr) {
++src;
++dst;
}
// Shift the non-null entries forward, and zero out the removed entries at the end of the array. for (; src < size_; ++src) { const AtexitEntry entry = array_[src];
array_[src] = {}; if (entry.fn != nullptr) {
array_[dst++] = entry;
}
}
// If the table uses fewer pages, clean the pages at the end.
size_t old_bytes = page_end_of_index(size_);
size_t new_bytes = page_end_of_index(dst); if (new_bytes < old_bytes) {
madvise(reinterpret_cast<char*>(array_) + new_bytes, old_bytes - new_bytes, MADV_DONTNEED);
}
set_writable(false, 0, size_);
size_ = dst;
extracted_count_ = 0;
}
// Use mprotect to make the array writable or read-only. Returns true on success. Making the array // read-only could protect against either unintentional or malicious corruption of the array. void AtexitArray::set_writable(bool writable, size_t start_idx, size_t num_entries) { if (array_ == nullptr) return;
// Approximately double the capacity. Returns true if successful (no overflow). AtexitEntry is // smaller than a page, but this function should still be correct even if AtexitEntry were larger // than one. bool AtexitArray::next_capacity(size_t capacity, size_t* result) { if (capacity == 0) {
*result = page_end(sizeof(AtexitEntry)) / sizeof(AtexitEntry); returntrue;
}
size_t num_bytes; if (__builtin_mul_overflow(page_end_of_index(capacity), 2, &num_bytes)) {
async_safe_format_log(ANDROID_LOG_WARN, "libc", "__cxa_atexit: capacity calculation overflow"); returnfalse;
}
*result = num_bytes / sizeof(AtexitEntry); returntrue;
}
// Register a function to be called either when a library is unloaded (dso != nullptr), or when the // program exits (dso == nullptr). The `dso` argument is typically the address of a hidden // __dso_handle variable. This function is also used as the backend for the atexit function. // // See https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor. // int __cxa_atexit(void (*func)(void*), void* arg, void* dso) { int result = -1;
if (func != nullptr) {
atexit_lock(); if (g_array.append_entry({.fn = func, .arg = arg, .dso = dso})) {
result = 0;
}
atexit_unlock();
}
for (ssize_t i = g_array.size() - 1; i >= 0; --i) { if (g_array[i].fn == nullptr || (dso != nullptr && g_array[i].dso != dso)) continue;
// Clear the entry in the array because its DSO handle will become invalid, and to avoid calling // an entry again if __cxa_finalize is called recursively. const AtexitEntry entry = g_array.extract_entry(i);
if (g_array.total_appends() != total_appends) goto restart;
}
// Avoid recompaction on recursive calls because it's unnecessary and would require earlier, // concurrent __cxa_finalize calls to restart. Skip recompaction on program exit too // (dso == nullptr), because the memory will be reclaimed soon anyway.
--call_depth; if (call_depth == 0 && dso != nullptr) {
g_array.recompact();
}
atexit_unlock();
if (dso != nullptr) {
__unregister_atfork(dso);
} else { // If called via exit(), flush output of all open files.
__libc_stdio_cleanup();
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-28)
¤
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.