Bug 1747299 - [UniqueBuffer] Use UniqueBuffer::Take instead of copy constructors for void*. r=gfx-reviewers,sotaro

Differential Revision: https://phabricator.services.mozilla.com/D134589
This commit is contained in:
Kelsey Gilbert 2021-12-25 00:31:48 +00:00
Родитель f103fe0859
Коммит ddb05ea412
7 изменённых файлов: 31 добавлений и 47 удалений

Просмотреть файл

@ -450,7 +450,7 @@ bool TexUnpackBlob::ConvertIfNeeded(
return false;
}
UniqueBuffer dstBuffer = calloc(1u, (size_t)dstTotalBytes.value());
auto dstBuffer = UniqueBuffer::Take(calloc(1u, dstTotalBytes.value()));
if (!dstBuffer.get()) {
webgl->ErrorOutOfMemory("Failed to allocate dest buffer.");
return false;

Просмотреть файл

@ -18,7 +18,7 @@ WebGLBuffer::~WebGLBuffer() {
mByteLength = 0;
mFetchInvalidator.InvalidateCaches();
mIndexCache = nullptr;
mIndexCache.reset();
mIndexRanges.clear();
if (!mContext) return;
@ -105,7 +105,7 @@ void WebGLBuffer::BufferData(const GLenum target, const uint64_t size,
const void* uploadData = maybeData;
UniqueBuffer maybeCalloc;
if (!uploadData) {
maybeCalloc = calloc(1, AssertedCast<size_t>(size));
maybeCalloc = UniqueBuffer::Take(calloc(1, AssertedCast<size_t>(size)));
if (!maybeCalloc) {
mContext->ErrorOutOfMemory("Failed to alloc zeros.");
return;
@ -117,7 +117,7 @@ void WebGLBuffer::BufferData(const GLenum target, const uint64_t size,
UniqueBuffer newIndexCache;
if (target == LOCAL_GL_ELEMENT_ARRAY_BUFFER &&
mContext->mNeedsIndexValidation) {
newIndexCache = malloc(AssertedCast<size_t>(size));
newIndexCache = UniqueBuffer::Take(malloc(AssertedCast<size_t>(size)));
if (!newIndexCache) {
mContext->ErrorOutOfMemory("Failed to alloc index cache.");
return;
@ -148,7 +148,7 @@ void WebGLBuffer::BufferData(const GLenum target, const uint64_t size,
// Truncate
mByteLength = 0;
mFetchInvalidator.InvalidateCaches();
mIndexCache = nullptr;
mIndexCache.reset();
return;
}
} else {

Просмотреть файл

@ -1086,7 +1086,7 @@ bool WebGLContext::DoFakeVertexAttrib0(const uint64_t vertexCount) {
////
const UniqueBuffer data(malloc(dataSize));
const auto data = UniqueBuffer::Take(malloc(dataSize));
if (!data) {
ErrorOutOfMemory("Failed to allocate fake vertex attrib 0 array.");
return false;

Просмотреть файл

@ -24,9 +24,8 @@ static void PrintLongString(const char* const begin, const size_t len) {
// internal size, so long strings are truncated.
const size_t chunkSize = 1000;
const UniqueBuffer buf(moz_xmalloc(chunkSize + 1)); // +1 for null-term
const auto bufBegin = (char*)buf.get();
bufBegin[chunkSize] = '\0';
auto buf = std::vector<char>(chunkSize + 1); // +1 for null-term
const auto bufBegin = buf.data();
auto chunkBegin = begin;
const auto end = begin + len;

Просмотреть файл

@ -590,7 +590,7 @@ static bool ZeroTextureData(const WebGLContext* webgl, GLuint tex,
const size_t sliceByteCount = checkedByteCount.value();
UniqueBuffer zeros = calloc(1u, sliceByteCount);
const auto zeros = UniqueBuffer::Take(calloc(1u, sliceByteCount));
if (!zeros) return false;
// Don't bother with striding it well.
@ -635,7 +635,7 @@ static bool ZeroTextureData(const WebGLContext* webgl, GLuint tex,
const size_t sliceByteCount = checkedByteCount.value();
UniqueBuffer zeros = calloc(1u, sliceByteCount);
const auto zeros = UniqueBuffer::Take(calloc(1u, sliceByteCount));
if (!zeros) return false;
// Don't bother with striding it well.

Просмотреть файл

@ -1709,7 +1709,7 @@ static bool DoCopyTexOrSubImage(WebGLContext* webgl, bool isSubImage,
byteCount *= dstHeight;
if (byteCount.isValid()) {
zeros = calloc(1u, byteCount.value());
zeros = UniqueBuffer::Take(calloc(1u, byteCount.value()));
}
if (!zeros.get()) {

Просмотреть файл

@ -253,49 +253,29 @@ enum class WebGLExtensionID : uint8_t {
Max
};
class UniqueBuffer {
class UniqueBuffer final {
// Like UniquePtr<>, but for void* and malloc/calloc/free.
void* mBuffer;
void reset() {
// Believe it or not, when `free` unconditional, it was showing up
// in profiles, nearly 20% of time spent in MethodDispatcther<UniformData>
// on Aquarium.
if (mBuffer) {
free(mBuffer);
mBuffer = nullptr;
}
}
void* mBuffer = nullptr;
public:
static inline UniqueBuffer Alloc(const size_t byteSize) {
return {malloc(byteSize)};
static inline UniqueBuffer Take(void* buffer) {
UniqueBuffer ret;
ret.mBuffer = buffer;
return ret;
}
UniqueBuffer() : mBuffer(nullptr) {}
MOZ_IMPLICIT UniqueBuffer(void* buffer) : mBuffer(buffer) {}
UniqueBuffer() = default;
~UniqueBuffer() {
reset();
}
UniqueBuffer(UniqueBuffer&& other) {
this->mBuffer = other.mBuffer;
other.mBuffer = nullptr;
}
UniqueBuffer(UniqueBuffer&& rhs) { *this = std::move(rhs); }
UniqueBuffer& operator=(UniqueBuffer&& other) {
UniqueBuffer& operator=(UniqueBuffer&& rhs) {
reset();
this->mBuffer = other.mBuffer;
other.mBuffer = nullptr;
return *this;
}
UniqueBuffer& operator=(void* newBuffer) {
reset();
this->mBuffer = newBuffer;
this->mBuffer = rhs.mBuffer;
rhs.mBuffer = nullptr;
return *this;
}
@ -303,10 +283,15 @@ class UniqueBuffer {
void* get() const { return mBuffer; }
explicit UniqueBuffer(const UniqueBuffer& other) =
delete; // construct using std::move()!
UniqueBuffer& operator=(const UniqueBuffer& other) =
delete; // assign using std::move()!
void reset() {
// Believe it or not, when `free` unconditional, it was showing up
// in profiles, nearly 20% of time spent in MethodDispatcther<UniformData>
// on Aquarium.
if (mBuffer) {
free(mBuffer);
mBuffer = nullptr;
}
}
};
namespace webgl {