ObjectAllocationProfile.h [plain text]
#ifndef ObjectAllocationProfile_h
#define ObjectAllocationProfile_h
#include "VM.h"
#include "JSGlobalObject.h"
#include "ObjectPrototype.h"
#include "SlotVisitor.h"
#include "WriteBarrier.h"
namespace JSC {
class ObjectAllocationProfile {
friend class LLIntOffsetsExtractor;
public:
static ptrdiff_t offsetOfAllocator() { return OBJECT_OFFSETOF(ObjectAllocationProfile, m_allocator); }
static ptrdiff_t offsetOfStructure() { return OBJECT_OFFSETOF(ObjectAllocationProfile, m_structure); }
ObjectAllocationProfile()
: m_allocator(0)
{
}
bool isNull() { return !m_allocator; }
void initialize(VM& vm, JSCell* owner, JSObject* prototype, unsigned inferredInlineCapacity)
{
ASSERT(!m_allocator);
ASSERT(!m_structure);
unsigned inlineCapacity = 0;
if (inferredInlineCapacity < JSFinalObject::defaultInlineCapacity()) {
inferredInlineCapacity += possibleDefaultPropertyCount(vm, prototype);
if (!inferredInlineCapacity) {
inferredInlineCapacity = JSFinalObject::defaultInlineCapacity();
} else if (inferredInlineCapacity > JSFinalObject::defaultInlineCapacity()) {
inferredInlineCapacity = JSFinalObject::defaultInlineCapacity();
}
inlineCapacity = inferredInlineCapacity;
ASSERT(inlineCapacity < JSFinalObject::maxInlineCapacity());
} else {
inlineCapacity = inferredInlineCapacity;
if (inlineCapacity > JSFinalObject::maxInlineCapacity())
inlineCapacity = JSFinalObject::maxInlineCapacity();
}
ASSERT(inlineCapacity > 0);
ASSERT(inlineCapacity <= JSFinalObject::maxInlineCapacity());
size_t allocationSize = JSFinalObject::allocationSize(inlineCapacity);
MarkedAllocator* allocator = &vm.heap.allocatorForObjectWithoutDestructor(allocationSize);
ASSERT(allocator->cellSize());
size_t slop = (allocator->cellSize() - allocationSize) / sizeof(WriteBarrier<Unknown>);
inlineCapacity += slop;
if (inlineCapacity > JSFinalObject::maxInlineCapacity())
inlineCapacity = JSFinalObject::maxInlineCapacity();
m_allocator = allocator;
m_structure.set(vm, owner,
vm.prototypeMap.emptyObjectStructureForPrototype(prototype, inlineCapacity));
}
Structure* structure() { return m_structure.get(); }
unsigned inlineCapacity() { return m_structure->inlineCapacity(); }
void clear()
{
m_allocator = 0;
m_structure.clear();
ASSERT(isNull());
}
void visitAggregate(SlotVisitor& visitor)
{
visitor.append(&m_structure);
}
private:
unsigned possibleDefaultPropertyCount(VM& vm, JSObject* prototype)
{
if (prototype == prototype->globalObject()->objectPrototype())
return 0;
size_t count = 0;
PropertyNameArray propertyNameArray(&vm);
prototype->structure()->getPropertyNamesFromStructure(vm, propertyNameArray, ExcludeDontEnumProperties);
PropertyNameArrayData::PropertyNameVector& propertyNameVector = propertyNameArray.data()->propertyNameVector();
for (size_t i = 0; i < propertyNameVector.size(); ++i) {
JSValue value = prototype->getDirect(vm, propertyNameVector[i]);
if (jsDynamicCast<JSFunction*>(value))
continue;
++count;
}
return count;
}
MarkedAllocator* m_allocator; WriteBarrier<Structure> m_structure;
};
}
#endif // ObjectAllocationProfile_h