зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1124195 - Replace use of AutoPtr with mozilla::UniquePtr r=sfink
This commit is contained in:
Родитель
08f8094f11
Коммит
16e6be5593
|
@ -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<char> intermediate(cx->pod_malloc<char>(arraySize));
|
||||
auto intermediate = cx->make_pod_array<char>(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<char> intermediate(cx->pod_malloc<char>(structSize));
|
||||
auto intermediate = cx->make_pod_array<char>(structSize);
|
||||
if (!intermediate) {
|
||||
JS_ReportAllocationOverflow(cx);
|
||||
return false;
|
||||
|
@ -3632,7 +3632,7 @@ CType::GetFFIType(JSContext* cx, JSObject* obj)
|
|||
return static_cast<ffi_type*>(slot.toPrivate());
|
||||
}
|
||||
|
||||
AutoPtr<ffi_type> 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<size_t>(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<ffi_type> ffiType(cx->new_<ffi_type>());
|
||||
auto ffiType = cx->make_unique<ffi_type>();
|
||||
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<FieldInfoHash> fields(cx->new_<FieldInfoHash>());
|
||||
auto fields = cx->make_unique<FieldInfoHash>();
|
||||
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<ffi_type> ffiType(cx->new_<ffi_type>());
|
||||
auto ffiType = cx->make_unique<ffi_type>();
|
||||
if (!ffiType) {
|
||||
JS_ReportOutOfMemory(cx);
|
||||
return nullptr;
|
||||
}
|
||||
ffiType->type = FFI_TYPE_STRUCT;
|
||||
|
||||
AutoPtr<ffi_type*> elements;
|
||||
size_t count = len != 0 ? len + 1 : 2;
|
||||
auto elements = cx->make_pod_array<ffi_type*>(count);
|
||||
if (!elements) {
|
||||
JS_ReportOutOfMemory(cx);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (len != 0) {
|
||||
elements = cx->pod_malloc<ffi_type*>(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<ffi_type*>(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
|
||||
|
|
|
@ -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 T>
|
||||
class AutoPtr {
|
||||
private:
|
||||
typedef AutoPtr<T> 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<T>&);
|
||||
self_type& operator=(AutoPtr<T>& rhs);
|
||||
|
||||
T* mPtr;
|
||||
};
|
||||
|
||||
// Container class for Vector, using SystemAllocPolicy.
|
||||
template<class T, size_t N = 0>
|
||||
class Array : public Vector<T, N, SystemAllocPolicy>
|
||||
|
@ -472,6 +441,8 @@ namespace PointerType {
|
|||
JSObject* GetBaseType(JSObject* obj);
|
||||
}
|
||||
|
||||
typedef mozilla::UniquePtr<ffi_type, JS::DeletePolicy<ffi_type>> 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 {
|
||||
|
|
Загрузка…
Ссылка в новой задаче