void* Allocator::reallocate(void* object, size_t newSize) { if (!m_isBmallocEnabled) return realloc(object, newSize); size_t oldSize = 0; switch (objectType(object)) { case ObjectType::Small: { BASSERT(objectType(nullptr) == ObjectType::Small); if (!object) break; size_t sizeClass = Object(object).page()->sizeClass(); oldSize = objectSize(sizeClass); break; } case ObjectType::Large: { std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex()); oldSize = PerProcess<Heap>::getFastCase()->largeSize(lock, object); if (newSize < oldSize && newSize > smallMax) { PerProcess<Heap>::getFastCase()->shrinkLarge(lock, Range(object, oldSize), newSize); return object; } break; } } void* result = allocate(newSize); size_t copySize = std::min(oldSize, newSize); memcpy(result, object, copySize); m_deallocator.deallocate(object); return result; }
Allocator::Allocator(Heap* heap, Deallocator& deallocator) : m_isBmallocEnabled(heap->environment().isBmallocEnabled()) , m_deallocator(deallocator) { for (size_t sizeClass = 0; sizeClass < sizeClassCount; ++sizeClass) m_bumpAllocators[sizeClass].init(objectSize(sizeClass)); }
extern void * realloc(void *ptr, size_t size) { pthread_mutex_lock(&mutex); increaseReallocCalls(); // Allocate new object void * newptr = allocateObject( size ); // Copy old object only if ptr != 0 if ( ptr != 0 ) { // copy only the minimum number of bytes size_t sizeToCopy = objectSize( ptr ); if ( sizeToCopy > size ) { sizeToCopy = size; } memcpy( newptr, ptr, sizeToCopy ); // Free old object freeObject( ptr ); } return newptr; }
void SimdUIntType::newAutomaticVariable (StatementNodePtr node, LContext &lcontext) const { SimdLContext &slcontext = static_cast <SimdLContext &> (lcontext); slcontext.addInst (new SimdPushPlaceholderInst (objectSize(), node->lineNumber)); }
void SimdArrayType::generateCode (const SyntaxNodePtr &node, LContext &lcontext) const { SimdLContext &slcontext = static_cast <SimdLContext &> (lcontext); VariableNodePtr var = node.cast<VariableNode>(); if( var && var->initialValue.cast<ValueNode>()) { SizeVector sizes; SizeVector offsets; coreSizes(0, sizes, offsets); slcontext.addInst (new SimdInitializeInst(sizes, offsets, node->lineNumber)); return; } else if (isAssignment(node)) // return or assignment { slcontext.addInst (new SimdAssignArrayInst (size(), elementSize(), node->lineNumber)); return; } else if ( node.cast<ArrayIndexNode>() ) { if(unknownSize() || unknownElementSize()) { slcontext.addInst (new SimdIndexVSArrayInst(elementSize(), unknownElementSize(), size(), unknownSize(), node->lineNumber)); } else { slcontext.addInst (new SimdIndexArrayInst(elementSize(), node->lineNumber, size())); } return; } else if (node.cast<SizeNode>()) { assert(size() == 0); slcontext.addInst (new SimdPushRefInst (unknownSize(), node->lineNumber)); } else if (node.cast<CallNode>()) { slcontext.addInst (new SimdPushPlaceholderInst(objectSize(), node->lineNumber)); return; } }
VehicleCloning::VehicleCloning(hkDemoEnvironment* env, hkBool createWorld) : hkDefaultPhysicsDemo(env) { m_bootstrapIterations = 200; m_track = HK_NULL; m_numWheels = 4; m_landscapeContainer = HK_NULL; setUpWorld(); if (!createWorld) { return; } m_world->lock(); const VehicleCloningVariant& variant = g_variants[m_variantId]; m_vehicles.setSize( variant.m_numVehicles ); // Create a vehicle to use as a template for cloning other vehicles hkpPhysicsSystem* vehicleSystem = createVehicle(); // Clone the vehicle and add the clones to the world hkArray<hkAabb> spawnVolumes; spawnVolumes.pushBack(hkAabb(hkVector4(-95, 2, -95), hkVector4(95, 8, 95))); AabbSpawnUtil spawnUtil( spawnVolumes ); for ( int i = 0; i < variant.m_numVehicles; ++i ) { hkpPhysicsSystem* newVehicleSystem = vehicleSystem->clone(); hkVector4 objectSize( 4, 4, 4 ); hkVector4 position; spawnUtil.getNewSpawnPosition( objectSize, position ); newVehicleSystem->getRigidBodies()[0]->setPosition(position); m_vehicles[i] = static_cast<hkpVehicleInstance*>(newVehicleSystem->getActions()[0]); m_vehicles[i]->addReference(); m_world->addPhysicsSystem( newVehicleSystem ); newVehicleSystem->removeReference(); } createWheels(variant.m_numVehicles); vehicleSystem->removeReference(); m_world->unlock(); }
void* Allocator::reallocate(void* object, size_t newSize) { if (!m_isBmallocEnabled) return realloc(object, newSize); void* result = allocate(newSize); if (!object) return result; size_t oldSize = 0; switch (objectType(object)) { case Small: { SmallPage* page = SmallPage::get(SmallLine::get(object)); oldSize = objectSize(page->sizeClass()); break; } case Medium: { MediumPage* page = MediumPage::get(MediumLine::get(object)); oldSize = objectSize(page->sizeClass()); break; } case Large: { std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex()); LargeObject largeObject(object); oldSize = largeObject.size(); break; } case XLarge: { std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex()); Range range = PerProcess<Heap>::getFastCase()->findXLarge(lock, object); RELEASE_BASSERT(range); oldSize = range.size(); break; } } size_t copySize = std::min(oldSize, newSize); memcpy(result, object, copySize); m_deallocator.deallocate(object); return result; }
AddrPtr SimdUIntType::newStaticVariable (Module *module) const { return newStaticVariableGeneric(module, objectSize()); }
ObjectsOnLandscapeDemo::ObjectsOnLandscapeDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { m_bootstrapIterations = 200; { hkpWorldCinfo info; m_world = new hkpWorld( info ); m_world->lock(); // Register ALL agents (though some may not be necessary) hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher()); } const ObjectsOnLandscapeVariant& variant = g_variants[m_variantId]; // Increase the stack size. m_oldStack = hkThreadMemory::getInstance().getStack(); m_stackBuffer = hkAllocate<char>( MY_STACK_SIZE, HK_MEMORY_CLASS_DEMO ); hkThreadMemory::getInstance().setStackArea( m_stackBuffer, MY_STACK_SIZE ); // // Create the ground using the landscape repository // { int landscapeIndex = LandscapeRepository::getInstance().getLandscapeIndexByName(variant.m_landscapeType); m_landscapeContainer = LandscapeRepository::getInstance().getLandscape(landscapeIndex); hkpRigidBodyCinfo groundInfo; groundInfo.m_motionType = hkpMotion::MOTION_FIXED; groundInfo.m_shape = m_landscapeContainer->m_shape; hkpRigidBody* groundbody = new hkpRigidBody(groundInfo); m_world->addEntity(groundbody)->removeReference(); } m_packfileData = HK_NULL; // Create a utility for generating positions for objects AabbSpawnUtil spawnUtil( m_landscapeContainer->m_spawnVolumes ); if (variant.m_numObjects > 100) { spawnUtil.m_allowOverlaps = true; } // m_env->m_window->getViewport(0)->toggleState(HKG_ENABLED_WIREFRAME); switch (variant.m_addObjectType) { case ADD_CONVEX: { for ( int i = 0; i < variant.m_numObjects; ++i ) { hkVector4 objectSize( spawnUtil.m_pseudoRandomGenerator.getRandRange(.5f, 2), spawnUtil.m_pseudoRandomGenerator.getRandRange(.5f, 2), spawnUtil.m_pseudoRandomGenerator.getRandRange(.5f, 2)); hkVector4 position; spawnUtil.getNewSpawnPosition( objectSize, position ); hkReal density = 1000; hkpRigidBody* rb = GameUtils::createRandomConvexGeometricFromBox(objectSize, objectSize(0) * objectSize(1) * objectSize(2) * density, // mass position, 30, // num verts &spawnUtil.m_pseudoRandomGenerator); m_world->addEntity(rb); rb->removeReference(); } break; } case ADD_COMPOUND: { hkString fileName = hkAssetManagementUtil::getFilePath("Resources/Physics/Compoundbodies/compoundbodies.hkx"); hkIstream infile( fileName.cString() ); HK_ASSERT( 0x215d080c, infile.isOk() ); hkPackfileReader* reader = new hkBinaryPackfileReader(); reader->loadEntireFile(infile.getStreamReader()); if( hkVersionUtil::updateToCurrentVersion( *reader, hkVersionRegistry::getInstance() ) != HK_SUCCESS ) { HK_ASSERT2(0, 0, "Couldn't update version."); } m_packfileData = reader->getPackfileData(); m_packfileData->addReference(); hkRootLevelContainer* container = static_cast<hkRootLevelContainer*>( reader->getContents( hkRootLevelContainerClass.getName() ) ); HK_ASSERT2(0xa6451543, container != HK_NULL, "Could not load root level obejct" ); hkpPhysicsData* physicsData = static_cast<hkpPhysicsData*>( container->findObjectByType( hkpPhysicsDataClass.getName() ) ); HK_ASSERT2(0xa6451544, physicsData != HK_NULL, "Could not find physics data in root level object" ); delete reader; const hkArray<hkpRigidBody*>& rigidBodies = physicsData->getPhysicsSystems()[0]->getRigidBodies(); for ( int i = 0; i < variant.m_numObjects; ++i ) { hkpRigidBody* newBody = rigidBodies[i % rigidBodies.getSize()]->clone(); hkQuaternion rotation; spawnUtil.m_pseudoRandomGenerator.getRandomRotation( rotation ); newBody->setRotation( rotation ); hkAabb aabb; newBody->getCollidable()->getShape()->getAabb(newBody->getTransform(), .05f, aabb ); hkVector4 objectSize; objectSize.setSub4( aabb.m_max, aabb.m_min ); hkVector4 position; spawnUtil.getNewSpawnPosition( objectSize, position ); newBody->setPosition( position ); m_world->addEntity( newBody ); newBody->removeReference(); } break; } case ADD_RAGDOLL: { // // Create a group filter to remove collisions between ragdoll bones // { hkpGroupFilter* filter = new hkpGroupFilter(); hkpGroupFilterSetup::setupGroupFilter( filter ); m_world->setCollisionFilter( filter ); filter->removeReference(); } const hkReal height = 2.0f; hkVector4 objectSize(height, height, height); for ( int i = 0; i < variant.m_numObjects; ++i ) { hkVector4 position; spawnUtil.getNewSpawnPosition( objectSize, position ); hkQuaternion rotation; rotation.setIdentity(); GameUtils::RagdollCinfo ragdollInfo; ragdollInfo.m_position = position; ragdollInfo.m_height = height; spawnUtil.m_pseudoRandomGenerator.getRandomRotation(ragdollInfo.m_rotation); ragdollInfo.m_systemNumber = i + 1; ragdollInfo.m_boneShapeType = GameUtils::RPT_CAPSULE; ragdollInfo.m_numVertebrae = 1; ragdollInfo.m_ragdollInterBoneCollisions = GameUtils::DISABLE_ONLY_ADJOINING_BONE_COLLISIONS; ragdollInfo.m_wantHandsAndFeet = GameUtils::WANT_HANDS_AND_FEET; hkpPhysicsSystem* ragdoll = GameUtils::createRagdoll( ragdollInfo ); const hkArray<hkpRigidBody*>& rigidbodies = ragdoll->getRigidBodies(); for( int iRB = 0; iRB < rigidbodies.getSize(); iRB++ ) { hkpRigidBody* body = rigidbodies[iRB]; body->setLinearDamping( 0.1f ); body->getMaterial().setFriction(0.31f); } m_world->addPhysicsSystem(ragdoll); ragdoll->removeReference(); } break; } } // // Setup the camera // { hkVector4 up ( 0.0f, 1.0f, 0.0f ); setupDefaultCameras( env, m_landscapeContainer->m_cameraFrom, m_landscapeContainer->m_cameraTo, up ); setupGraphics(); } m_world->unlock(); }