Bug 1124195 - Replace use of AutoPtr with mozilla::UniquePtr r=sfink

This commit is contained in:
Jon Coppeard 2015-01-22 14:11:12 +00:00
Родитель 08f8094f11
Коммит 16e6be5593
2 изменённых файлов: 26 добавлений и 61 удалений

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

@ -2583,7 +2583,7 @@ ImplicitConvert(JSContext* cx,
// Convert into an intermediate, in case of failure. // Convert into an intermediate, in case of failure.
size_t elementSize = CType::GetSize(baseType); size_t elementSize = CType::GetSize(baseType);
size_t arraySize = elementSize * targetLength; size_t arraySize = elementSize * targetLength;
AutoPtr<char> intermediate(cx->pod_malloc<char>(arraySize)); auto intermediate = cx->make_pod_array<char>(arraySize);
if (!intermediate) { if (!intermediate) {
JS_ReportAllocationOverflow(cx); JS_ReportAllocationOverflow(cx);
return false; return false;
@ -2648,7 +2648,7 @@ ImplicitConvert(JSContext* cx,
// Convert into an intermediate, in case of failure. // Convert into an intermediate, in case of failure.
size_t structSize = CType::GetSize(targetType); size_t structSize = CType::GetSize(targetType);
AutoPtr<char> intermediate(cx->pod_malloc<char>(structSize)); auto intermediate = cx->make_pod_array<char>(structSize);
if (!intermediate) { if (!intermediate) {
JS_ReportAllocationOverflow(cx); JS_ReportAllocationOverflow(cx);
return false; return false;
@ -3632,7 +3632,7 @@ CType::GetFFIType(JSContext* cx, JSObject* obj)
return static_cast<ffi_type*>(slot.toPrivate()); return static_cast<ffi_type*>(slot.toPrivate());
} }
AutoPtr<ffi_type> result; UniquePtrFFIType result;
switch (CType::GetTypeCode(obj)) { switch (CType::GetTypeCode(obj)) {
case TYPE_array: case TYPE_array:
result = ArrayType::BuildFFIType(cx, obj); result = ArrayType::BuildFFIType(cx, obj);
@ -3649,7 +3649,7 @@ CType::GetFFIType(JSContext* cx, JSObject* obj)
if (!result) if (!result)
return nullptr; return nullptr;
JS_SetReservedSlot(obj, SLOT_FFITYPE, PRIVATE_TO_JSVAL(result.get())); JS_SetReservedSlot(obj, SLOT_FFITYPE, PRIVATE_TO_JSVAL(result.get()));
return result.forget(); return result.release();
} }
JSString* JSString*
@ -4496,7 +4496,7 @@ ArrayType::GetLength(JSObject* obj)
return Convert<size_t>(length.toDouble()); return Convert<size_t>(length.toDouble());
} }
ffi_type* UniquePtrFFIType
ArrayType::BuildFFIType(JSContext* cx, JSObject* obj) ArrayType::BuildFFIType(JSContext* cx, JSObject* obj)
{ {
MOZ_ASSERT(CType::IsCType(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', // 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 // but some libffi platforms currently require that it be meaningful. I'm
// looking at you, x86_64. // looking at you, x86_64.
AutoPtr<ffi_type> ffiType(cx->new_<ffi_type>()); auto ffiType = cx->make_unique<ffi_type>();
if (!ffiType) { if (!ffiType) {
JS_ReportOutOfMemory(cx); JS_ReportOutOfMemory(cx);
return nullptr; return nullptr;
@ -4536,7 +4536,7 @@ ArrayType::BuildFFIType(JSContext* cx, JSObject* obj)
ffiType->elements[i] = ffiBaseType; ffiType->elements[i] = ffiBaseType;
ffiType->elements[length] = nullptr; ffiType->elements[length] = nullptr;
return ffiType.forget(); return Move(ffiType);
} }
bool 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 // 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 // to get GC safety for free, since if anything in this function fails we
// do not want to mutate 'typeObj'.) // do not want to mutate 'typeObj'.)
AutoPtr<FieldInfoHash> fields(cx->new_<FieldInfoHash>()); auto fields = cx->make_unique<FieldInfoHash>();
if (!fields || !fields->init(len)) { if (!fields || !fields->init(len)) {
JS_ReportOutOfMemory(cx); JS_ReportOutOfMemory(cx);
return false; return false;
@ -4973,7 +4973,7 @@ StructType::DefineInternal(JSContext* cx, JSObject* typeObj_, JSObject* fieldsOb
if (!SizeTojsval(cx, structSize, &sizeVal)) if (!SizeTojsval(cx, structSize, &sizeVal))
return false; 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_SIZE, sizeVal);
JS_SetReservedSlot(typeObj, SLOT_ALIGN, INT_TO_JSVAL(structAlign)); JS_SetReservedSlot(typeObj, SLOT_ALIGN, INT_TO_JSVAL(structAlign));
@ -4983,7 +4983,7 @@ StructType::DefineInternal(JSContext* cx, JSObject* typeObj_, JSObject* fieldsOb
return true; return true;
} }
ffi_type* UniquePtrFFIType
StructType::BuildFFIType(JSContext* cx, JSObject* obj) StructType::BuildFFIType(JSContext* cx, JSObject* obj)
{ {
MOZ_ASSERT(CType::IsCType(obj)); MOZ_ASSERT(CType::IsCType(obj));
@ -4996,20 +4996,21 @@ StructType::BuildFFIType(JSContext* cx, JSObject* obj)
size_t structSize = CType::GetSize(obj); size_t structSize = CType::GetSize(obj);
size_t structAlign = CType::GetAlignment(obj); size_t structAlign = CType::GetAlignment(obj);
AutoPtr<ffi_type> ffiType(cx->new_<ffi_type>()); auto ffiType = cx->make_unique<ffi_type>();
if (!ffiType) { if (!ffiType) {
JS_ReportOutOfMemory(cx); JS_ReportOutOfMemory(cx);
return nullptr; return nullptr;
} }
ffiType->type = FFI_TYPE_STRUCT; 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) { if (len != 0) {
elements = cx->pod_malloc<ffi_type*>(len + 1);
if (!elements) {
JS_ReportOutOfMemory(cx);
return nullptr;
}
elements[len] = nullptr; elements[len] = nullptr;
for (FieldInfoHash::Range r = fields->all(); !r.empty(); r.popFront()) { for (FieldInfoHash::Range r = fields->all(); !r.empty(); r.popFront()) {
@ -5019,21 +5020,15 @@ StructType::BuildFFIType(JSContext* cx, JSObject* obj)
return nullptr; return nullptr;
elements[entry.value().mIndex] = fieldType; elements[entry.value().mIndex] = fieldType;
} }
} else { } else {
// Represent an empty struct as having a size of 1 byte, just like C++. // Represent an empty struct as having a size of 1 byte, just like C++.
MOZ_ASSERT(structSize == 1); MOZ_ASSERT(structSize == 1);
MOZ_ASSERT(structAlign == 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[0] = &ffi_type_uint8;
elements[1] = nullptr; elements[1] = nullptr;
} }
ffiType->elements = elements.get(); ffiType->elements = elements.release();
#ifdef DEBUG #ifdef DEBUG
// Perform a sanity check: the result of our struct size and alignment // 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; ffiType->alignment = structAlign;
#endif #endif
elements.forget(); return Move(ffiType);
return ffiType.forget();
} }
bool bool

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

@ -6,6 +6,8 @@
#ifndef ctypes_CTypes_h #ifndef ctypes_CTypes_h
#define ctypes_CTypes_h #define ctypes_CTypes_h
#include "mozilla/UniquePtr.h"
#include "ffi.h" #include "ffi.h"
#include "jsalloc.h" #include "jsalloc.h"
#include "prlink.h" #include "prlink.h"
@ -22,39 +24,6 @@ namespace ctypes {
** Utility classes ** 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. // Container class for Vector, using SystemAllocPolicy.
template<class T, size_t N = 0> template<class T, size_t N = 0>
class Array : public Vector<T, N, SystemAllocPolicy> class Array : public Vector<T, N, SystemAllocPolicy>
@ -472,6 +441,8 @@ namespace PointerType {
JSObject* GetBaseType(JSObject* obj); JSObject* GetBaseType(JSObject* obj);
} }
typedef mozilla::UniquePtr<ffi_type, JS::DeletePolicy<ffi_type>> UniquePtrFFIType;
namespace ArrayType { namespace ArrayType {
JSObject* CreateInternal(JSContext* cx, HandleObject baseType, size_t length, JSObject* CreateInternal(JSContext* cx, HandleObject baseType, size_t length,
bool lengthDefined); bool lengthDefined);
@ -479,7 +450,7 @@ namespace ArrayType {
JSObject* GetBaseType(JSObject* obj); JSObject* GetBaseType(JSObject* obj);
size_t GetLength(JSObject* obj); size_t GetLength(JSObject* obj);
bool GetSafeLength(JSObject* obj, size_t* result); bool GetSafeLength(JSObject* obj, size_t* result);
ffi_type* BuildFFIType(JSContext* cx, JSObject* obj); UniquePtrFFIType BuildFFIType(JSContext* cx, JSObject* obj);
} }
namespace StructType { namespace StructType {
@ -488,7 +459,7 @@ namespace StructType {
const FieldInfoHash* GetFieldInfo(JSObject* obj); const FieldInfoHash* GetFieldInfo(JSObject* obj);
const FieldInfo* LookupField(JSContext* cx, JSObject* obj, JSFlatString *name); const FieldInfo* LookupField(JSContext* cx, JSObject* obj, JSFlatString *name);
JSObject* BuildFieldsArray(JSContext* cx, JSObject* obj); JSObject* BuildFieldsArray(JSContext* cx, JSObject* obj);
ffi_type* BuildFFIType(JSContext* cx, JSObject* obj); UniquePtrFFIType BuildFFIType(JSContext* cx, JSObject* obj);
} }
namespace FunctionType { namespace FunctionType {