Skip to content

Instantly share code, notes, and snippets.

@mbolt35
Created April 24, 2012 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbolt35/2475458 to your computer and use it in GitHub Desktop.
Save mbolt35/2475458 to your computer and use it in GitHub Desktop.
Vector Dynamic Memory Allocation
// Allocation for Array
ArrayObject* ArrayClass::newarray(Atom* argv, int argc)
{
ArrayObject *inst = newArray(argc);
for (uint32 i=0; i<uint32(argc); i++) {
inst->setUintProperty(i, argv[i]);
}
return inst;
}
ArrayObject* ArrayClass::newArray(uint32 capacity)
{
VTable* ivtable = this->ivtable();
ArrayObject *a = new (core()->GetGC(), ivtable->getExtraSize())
ArrayObject(ivtable, prototype, capacity);
return a;
}
ArrayObject* ArrayClass::createInstance(VTable *ivtable, ScriptObject* prototype)
{
return new (core()->GetGC(), ivtable->getExtraSize()) ArrayObject(ivtable, prototype, 0);
}
// Vector.<T> creation
// There are actually 4 of these template implementations for Vector...
// -- For int, uint, Number, and "Object"
IntVectorClass::IntVectorClass(VTable *vtable)
: ClassClosure(vtable)
{
toplevel()->intVectorClass = this;
prototype = toplevel()->objectClass->construct();
}
ScriptObject* IntVectorClass::createInstance(VTable *ivtable,
ScriptObject *prototype)
{
return new (core()->GetGC(), ivtable->getExtraSize()) IntVectorObject(ivtable, prototype);
}
Atom IntVectorClass::call(int argc, Atom* argv)
{
if (argc != 1)
{
toplevel()->throwArgumentError(kCoerceArgumentCountError, toplevel()->core()->toErrorString(argc));
}
if( AvmCore::istype(argv[1], ivtable()->traits ) )
return argv[1];
IntVectorObject* v = (IntVectorObject*)createInstance(ivtable(), prototype);
v->initWithObj(argv[1]);
return v->atom();
}
IntVectorObject* IntVectorClass::newVector(uint32 length)
{
VTable* ivtable = this->ivtable();
IntVectorObject *v = new (core()->GetGC(), ivtable->getExtraSize())
IntVectorObject(ivtable, prototype);
v->set_length(length);
return v;
}
VectorBaseObject* IntVectorObject::newVector(uint32 length)
{
return toplevel()->intVectorClass->newVector(length);
}
// This is the dynamic memory allocation method for the Vector.<T> class
virtual void grow(uint32 newCapacity, bool exact = false)
{
if (newCapacity > m_capacity)
{
if( !exact )
newCapacity = newCapacity + (newCapacity >>2);
//newCapacity = ((newCapacity+kGrowthIncr)/kGrowthIncr)*kGrowthIncr;
T *newArray = mmfx_new_array(T, newCapacity);
if (!newArray)
{
toplevel()->throwError(kOutOfMemoryError);
}
if (m_array)
{
VMPI_memcpy(newArray, m_array, m_length * sizeof(T));
mmfx_delete_array((T*)m_array);
}
VMPI_memset(newArray+m_length, 0, (newCapacity-m_capacity) * sizeof(T));
m_array = newArray;
m_capacity = newCapacity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment