/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// If mSegmentArray is null, there's no need to actually free anything if (!mSegmentArray) { return;
}
// Dispatch a task that frees up the array. This may run immediately or on // a background thread.
FreeOMT([segmentArray = mSegmentArray, arrayCount = mSegmentArrayCount]() { for (uint32_t i = 0; i < arrayCount; i++) { if (segmentArray[i]) {
free(segmentArray[i]);
}
}
free(segmentArray);
});
}
void nsSegmentedBuffer::FreeOMT(std::function<void()>&& aTask) { if (!NS_IsMainThread()) {
aTask(); return;
}
if (mFreeOMT) { // There is a runnable pending which will handle this object if (mFreeOMT->AddTask(std::move(aTask)) > 1) { return;
}
} else {
mFreeOMT = mozilla::MakeRefPtr<FreeOMTPointers>();
mFreeOMT->AddTask(std::move(aTask));
}
if (!mIOThread) {
mIOThread = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
}
// During the shutdown we are not able to obtain the IOThread and/or the // dispatching of runnable fails. if (!mIOThread || NS_FAILED(mIOThread->Dispatch(NS_NewRunnableFunction( "nsSegmentedBuffer::FreeOMT",
[obj = mFreeOMT]() { obj->FreeAll(); })))) {
mFreeOMT->FreeAll();
}
}
void nsSegmentedBuffer::FreeOMTPointers::FreeAll() { // Take all the tasks from the object. If AddTask is called after we release // the lock, then another runnable will be dispatched for that task. This is // necessary to avoid blocking the main thread while memory is being freed.
nsTArray<std::function<void()>> tasks = [this]() { auto t = mTasks.Lock(); return std::move(*t);
}();
// Finally run all the tasks to free memory. for (auto& task : tasks) {
task();
}
}
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 ist noch experimentell.