示例#1
0
static void test_chunkalloc(skiatest::Reporter* reporter) {
    static const size_t kMin = 1024;
    SkChunkAlloc alloc(kMin);

    //------------------------------------------------------------------------
    // check empty
    check_alloc(reporter, alloc, 0, 0, 0);
    REPORTER_ASSERT(reporter, !alloc.contains(nullptr));
    REPORTER_ASSERT(reporter, !alloc.contains(reporter));

    // reset on empty allocator
    alloc.reset();
    check_alloc(reporter, alloc, 0, 0, 0);

    // rewind on empty allocator
    alloc.rewind();
    check_alloc(reporter, alloc, 0, 0, 0);

    //------------------------------------------------------------------------
    // test reset when something is allocated
    size_t size = kMin >> 1;
    void* ptr = simple_alloc(reporter, &alloc, size);

    alloc.reset();
    check_alloc(reporter, alloc, 0, 0, 0);
    REPORTER_ASSERT(reporter, !alloc.contains(ptr));

    //------------------------------------------------------------------------
    // test rewind when something is allocated
    ptr = simple_alloc(reporter, &alloc, size);

    alloc.rewind();
    check_alloc(reporter, alloc, size, 0, 1);
    REPORTER_ASSERT(reporter, !alloc.contains(ptr));

    // use the available block
    ptr = simple_alloc(reporter, &alloc, size);
    alloc.reset();

    //------------------------------------------------------------------------
    // test out allocating a second block
    ptr = simple_alloc(reporter, &alloc, size);

    ptr = alloc.allocThrow(kMin);
    check_alloc(reporter, alloc, 2*kMin, size+kMin, 2);
    REPORTER_ASSERT(reporter, alloc.contains(ptr));

    //------------------------------------------------------------------------
    // test out unalloc
    size_t freed = alloc.unalloc(ptr);
    REPORTER_ASSERT(reporter, freed == kMin);
    check_alloc(reporter, alloc, 2*kMin, size, 2);
    REPORTER_ASSERT(reporter, !alloc.contains(ptr));
}
示例#2
0
int main(){
	printf("test simple allocator \n");

	simple_allocator_init();
	printf("test simple allocator 1 \n");

	char * test = (char*)simple_alloc(512);
	test[0] = 1;

	printf("test simple allocator 2 \n");
	char * test2 = (char*)simple_alloc(2*1024*1024);
	test2[0] = 1;


	printf("test simple allocator end \n");
	return 1;
}
			/**
			 * Performs allocation and necessary conversions.
			 */
			void allocate(const labust::simulation::vector& tauIn,
					labust::simulation::vector& tauOut)
			{
				Eigen::VectorXd vi(dofs.size());
				for (int i=0; i<dofs.size(); ++i) vi(i)=tauIn(dofs[i]);

				tdes = Binv*vi;

				//ROS_ERROR("Before tdes: %f %f %f %f",tdes(0), tdes(1), tdes(2), tdes(3));

				switch (type)
				{
				case NoAlloc:
					//noalloc
					break;
				case SimpleAlloc:
					simple_alloc();
					break;
				case ScaleAlloc:
					scale_alloc();
					break;
				default:
					throw std::runtime_error("ThrustAllocator: Undefined allocation type.");
				}

				//ROS_ERROR("Forces %f %f %f",tdes(0), tdes(1), tdes(2), tdes(3));
				vi = B*tdes;

				for (int i=0; i<dofs.size(); ++i) tauOut(dofs[i])=vi(i);

				bool scaling = false;

				//Determine coercion for windup
				switch (type)
				{
				case NoAlloc:
					//noalloc
					break;
				case SimpleAlloc:
					//It is safe to compare these
					for (int i=0; i<dofs.size(); ++i)
					{
						coercion(dofs[i]) = 0;
						if (tauIn(dofs[i])>tauOut(dofs[i]))	coercion(dofs[i])=1;
						else if (tauIn(dofs[i])<tauOut(dofs[i]))	coercion(dofs[i])=-1;
					}
					break;
				case ScaleAlloc:
//						for (int i=0; i<group_scales.size(); ++i) if ((scaling = (group_scales[i]>1))) break;
//
//						if (scaling)
//						{
//							for (int i=0; i<dofs.size(); ++i)
//							{
//								if (tauIn(dofs[i])>=tauOut(dofs[i]))	coercion(dofs[i])=1;
//								else if (tauIn(dofs[i])<=tauOut(dofs[i]))	coercion(dofs[i])=-1;
//							}
//						}

					//Alternative to scaling inspection
					for (int i=0; i<dofs.size(); ++i)
					{
						coercion(dofs[i]) = 0;
						//If there is deviation between input and output we assume
						if (fabs(tauIn(dofs[i]) - tauOut(dofs[i])) > 0.01)
						{
							if (tauIn(dofs[i])>=tauOut(dofs[i]))	coercion(dofs[i])=1;
							else if (tauIn(dofs[i])<=tauOut(dofs[i]))	coercion(dofs[i])=-1;
						}
					}
					break;
				default:
					break;
				}
			}