From e45428fae6af65a27d80c3fa1227a0525d084942 Mon Sep 17 00:00:00 2001 From: Jon Coppeard Date: Thu, 22 Jan 2015 14:11:12 +0000 Subject: [PATCH] Bug 1124195 - Replace use of AutoPtr with mozilla::UniquePtr r=sfink --- js/src/ctypes/CTypes.cpp | 50 ++++++++++++++++++---------------------- js/src/ctypes/CTypes.h | 41 +++++--------------------------- 2 files changed, 28 insertions(+), 63 deletions(-) diff --git a/js/src/ctypes/CTypes.cpp b/js/src/ctypes/CTypes.cpp index 971b22419799..ee8b1690b5ba 100644 --- a/js/src/ctypes/CTypes.cpp +++ b/js/src/ctypes/CTypes.cpp @@ -2583,7 +2583,7 @@ ImplicitConvert(JSContext* cx, // Convert into an intermediate, in case of failure. size_t elementSize = CType::GetSize(baseType); size_t arraySize = elementSize * targetLength; - AutoPtr intermediate(cx->pod_malloc(arraySize)); + auto intermediate = cx->make_pod_array(arraySize); if (!intermediate) { JS_ReportAllocationOverflow(cx); return false; @@ -2648,7 +2648,7 @@ ImplicitConvert(JSContext* cx, // Convert into an intermediate, in case of failure. size_t structSize = CType::GetSize(targetType); - AutoPtr intermediate(cx->pod_malloc(structSize)); + auto intermediate = cx->make_pod_array(structSize); if (!intermediate) { JS_ReportAllocationOverflow(cx); return false; @@ -3632,7 +3632,7 @@ CType::GetFFIType(JSContext* cx, JSObject* obj) return static_cast(slot.toPrivate()); } - AutoPtr result; + UniquePtrFFIType result; switch (CType::GetTypeCode(obj)) { case TYPE_array: result = ArrayType::BuildFFIType(cx, obj); @@ -3649,7 +3649,7 @@ CType::GetFFIType(JSContext* cx, JSObject* obj) if (!result) return nullptr; JS_SetReservedSlot(obj, SLOT_FFITYPE, PRIVATE_TO_JSVAL(result.get())); - return result.forget(); + return result.release(); } JSString* @@ -4496,7 +4496,7 @@ ArrayType::GetLength(JSObject* obj) return Convert(length.toDouble()); } -ffi_type* +UniquePtrFFIType ArrayType::BuildFFIType(JSContext* cx, JSObject* obj) { MOZ_ASSERT(CType::IsCType(obj)); @@ -4517,7 +4517,7 @@ ArrayType::BuildFFIType(JSContext* cx, JSObject* obj) // values. It would be nice to not do all the work of setting up 'elements', // but some libffi platforms currently require that it be meaningful. I'm // looking at you, x86_64. - AutoPtr ffiType(cx->new_()); + auto ffiType = cx->make_unique(); if (!ffiType) { JS_ReportOutOfMemory(cx); return nullptr; @@ -4536,7 +4536,7 @@ ArrayType::BuildFFIType(JSContext* cx, JSObject* obj) ffiType->elements[i] = ffiBaseType; ffiType->elements[length] = nullptr; - return ffiType.forget(); + return Move(ffiType); } bool @@ -4880,7 +4880,7 @@ StructType::DefineInternal(JSContext* cx, JSObject* typeObj_, JSObject* fieldsOb // its constituents. (We cannot simply stash the hash in a reserved slot now // to get GC safety for free, since if anything in this function fails we // do not want to mutate 'typeObj'.) - AutoPtr fields(cx->new_()); + auto fields = cx->make_unique(); if (!fields || !fields->init(len)) { JS_ReportOutOfMemory(cx); return false; @@ -4973,7 +4973,7 @@ StructType::DefineInternal(JSContext* cx, JSObject* typeObj_, JSObject* fieldsOb if (!SizeTojsval(cx, structSize, &sizeVal)) return false; - JS_SetReservedSlot(typeObj, SLOT_FIELDINFO, PRIVATE_TO_JSVAL(fields.forget())); + JS_SetReservedSlot(typeObj, SLOT_FIELDINFO, PRIVATE_TO_JSVAL(fields.release())); JS_SetReservedSlot(typeObj, SLOT_SIZE, sizeVal); JS_SetReservedSlot(typeObj, SLOT_ALIGN, INT_TO_JSVAL(structAlign)); @@ -4983,7 +4983,7 @@ StructType::DefineInternal(JSContext* cx, JSObject* typeObj_, JSObject* fieldsOb return true; } -ffi_type* +UniquePtrFFIType StructType::BuildFFIType(JSContext* cx, JSObject* obj) { MOZ_ASSERT(CType::IsCType(obj)); @@ -4996,20 +4996,21 @@ StructType::BuildFFIType(JSContext* cx, JSObject* obj) size_t structSize = CType::GetSize(obj); size_t structAlign = CType::GetAlignment(obj); - AutoPtr ffiType(cx->new_()); + auto ffiType = cx->make_unique(); if (!ffiType) { JS_ReportOutOfMemory(cx); return nullptr; } ffiType->type = FFI_TYPE_STRUCT; - AutoPtr elements; + size_t count = len != 0 ? len + 1 : 2; + auto elements = cx->make_pod_array(count); + if (!elements) { + JS_ReportOutOfMemory(cx); + return nullptr; + } + if (len != 0) { - elements = cx->pod_malloc(len + 1); - if (!elements) { - JS_ReportOutOfMemory(cx); - return nullptr; - } elements[len] = nullptr; for (FieldInfoHash::Range r = fields->all(); !r.empty(); r.popFront()) { @@ -5019,21 +5020,15 @@ StructType::BuildFFIType(JSContext* cx, JSObject* obj) return nullptr; elements[entry.value().mIndex] = fieldType; } - } else { // Represent an empty struct as having a size of 1 byte, just like C++. MOZ_ASSERT(structSize == 1); MOZ_ASSERT(structAlign == 1); - elements = cx->pod_malloc(2); - if (!elements) { - JS_ReportOutOfMemory(cx); - return nullptr; - } elements[0] = &ffi_type_uint8; elements[1] = nullptr; } - ffiType->elements = elements.get(); + ffiType->elements = elements.release(); #ifdef DEBUG // Perform a sanity check: the result of our struct size and alignment @@ -5055,8 +5050,7 @@ StructType::BuildFFIType(JSContext* cx, JSObject* obj) ffiType->alignment = structAlign; #endif - elements.forget(); - return ffiType.forget(); + return Move(ffiType); } bool @@ -6090,7 +6084,7 @@ CClosure::Create(JSContext* cx, MOZ_ASSERT(!fninfo->mIsVariadic); MOZ_ASSERT(GetABICode(fninfo->mABI) != ABI_WINAPI); - AutoPtr cinfo(cx->new_(JS_GetRuntime(cx))); + auto cinfo = cx->make_unique(JS_GetRuntime(cx)); if (!cinfo) { JS_ReportOutOfMemory(cx); return nullptr; @@ -6160,7 +6154,7 @@ CClosure::Create(JSContext* cx, } // Stash the ClosureInfo struct on our new object. - JS_SetReservedSlot(result, SLOT_CLOSUREINFO, PRIVATE_TO_JSVAL(cinfo.forget())); + JS_SetReservedSlot(result, SLOT_CLOSUREINFO, PRIVATE_TO_JSVAL(cinfo.release())); // Casting between void* and a function pointer is forbidden in C and C++. // Do it via an integral type. diff --git a/js/src/ctypes/CTypes.h b/js/src/ctypes/CTypes.h index dee4a5cd1099..a530c6889aa1 100644 --- a/js/src/ctypes/CTypes.h +++ b/js/src/ctypes/CTypes.h @@ -6,6 +6,8 @@ #ifndef ctypes_CTypes_h #define ctypes_CTypes_h +#include "mozilla/UniquePtr.h" + #include "ffi.h" #include "jsalloc.h" #include "prlink.h" @@ -22,39 +24,6 @@ namespace ctypes { ** Utility classes *******************************************************************************/ -// Class that takes ownership of a pointer T*, and calls cx->delete_() or -// cx->array_delete() upon destruction. -template -class AutoPtr { -private: - typedef AutoPtr self_type; - -public: - AutoPtr() : mPtr(nullptr) { } - explicit AutoPtr(T* ptr) : mPtr(ptr) { } - ~AutoPtr() { js_delete(mPtr); } - - T* operator->() { return mPtr; } - bool operator!() { return mPtr == nullptr; } - T& operator[](size_t i) { return *(mPtr + i); } - // Note: we cannot safely provide an 'operator T*()', since this would allow - // the compiler to perform implicit conversion from one AutoPtr to another - // via the constructor AutoPtr(T*). - - T* get() { return mPtr; } - void set(T* other) { MOZ_ASSERT(mPtr == nullptr); mPtr = other; } - T* forget() { T* result = mPtr; mPtr = nullptr; return result; } - - self_type& operator=(T* rhs) { mPtr = rhs; return *this; } - -private: - // Do not allow copy construction or assignment from another AutoPtr. - AutoPtr(AutoPtr&); - self_type& operator=(AutoPtr& rhs); - - T* mPtr; -}; - // Container class for Vector, using SystemAllocPolicy. template class Array : public Vector @@ -472,6 +441,8 @@ namespace PointerType { JSObject* GetBaseType(JSObject* obj); } +typedef mozilla::UniquePtr> UniquePtrFFIType; + namespace ArrayType { JSObject* CreateInternal(JSContext* cx, HandleObject baseType, size_t length, bool lengthDefined); @@ -479,7 +450,7 @@ namespace ArrayType { JSObject* GetBaseType(JSObject* obj); size_t GetLength(JSObject* obj); bool GetSafeLength(JSObject* obj, size_t* result); - ffi_type* BuildFFIType(JSContext* cx, JSObject* obj); + UniquePtrFFIType BuildFFIType(JSContext* cx, JSObject* obj); } namespace StructType { @@ -488,7 +459,7 @@ namespace StructType { const FieldInfoHash* GetFieldInfo(JSObject* obj); const FieldInfo* LookupField(JSContext* cx, JSObject* obj, JSFlatString *name); JSObject* BuildFieldsArray(JSContext* cx, JSObject* obj); - ffi_type* BuildFFIType(JSContext* cx, JSObject* obj); + UniquePtrFFIType BuildFFIType(JSContext* cx, JSObject* obj); } namespace FunctionType {