Example #1
0
  void DoAllocationTest() {
    uint8_t array_data[sizeof(int)];
    BaseMP mem_pool(array_data, sizeof(array_data),
                    sizeof(int), 1);

    EXPECT_EQ(0, mem_pool.Allocate());
    EXPECT_EQ(static_cast<uint32_t>(-1), mem_pool.Allocate());
  }
Example #2
0
  void DoGetIndexTest() {
    int array_data[2] = { 0 };
    BaseMP mem_pool(array_data, sizeof(array_data),
                    sizeof(array_data[0]), ARRAY_SIZE(array_data));

    EXPECT_EQ(0, mem_pool.Allocate());
    EXPECT_EQ(1, mem_pool.Allocate());

    EXPECT_EQ(0, mem_pool.GetIndex(&array_data[0]));
    EXPECT_EQ(1, mem_pool.GetIndex(&array_data[1]));
  }
Example #3
0
  void DoRemovalTest() {
    int array_data[1] = { 0 };
    BaseMP mem_pool(array_data, sizeof(array_data),
                    sizeof(array_data[0]), ARRAY_SIZE(array_data));

    EXPECT_EQ(0, mem_pool.Allocate());

    mem_pool.Remove(0);

    EXPECT_EQ(0, mem_pool.Allocate());
  }
Example #4
0
  void DoInsertionTest() {
    int array_data[1] = { 0 };
    BaseMP mem_pool(array_data, sizeof(array_data),
                    sizeof(array_data[0]), ARRAY_SIZE(array_data));

    const int insertion_data = 123;
    const int extra_data = 234;
    EXPECT_EQ(0, mem_pool.Insert(&insertion_data));
    EXPECT_EQ(static_cast<uint32_t>(-1), mem_pool.Insert(&extra_data));
    EXPECT_EQ(array_data[0], insertion_data);
  }
Example #5
0
  void DoConstructorTest() {
    uint8_t array_data[sizeof(int)];
    BaseMP mem_pool(array_data, sizeof(array_data),
                    sizeof(int), 1);

    int typed_data[1];
    TypedMP typed_array(typed_data,
                        sizeof(typed_data),
                        ARRAY_SIZE(typed_data));

    ContainedMP contained_array;
  }
Example #6
0
  void DoTypedArrayFunctions() {
    int array_data[2] = { 0 };
    TypedMP mem_pool(array_data,
                     sizeof(array_data),
                     ARRAY_SIZE(array_data));

    const int insertion_data1 = 123;
    const int insertion_data2 = 234;
    EXPECT_EQ(0, mem_pool.Insert(insertion_data1));
    EXPECT_EQ(1, mem_pool.Insert(&insertion_data2));

    EXPECT_EQ(insertion_data1, mem_pool[0]);
    EXPECT_EQ(insertion_data2, mem_pool[1]);
  }
Example #7
0
  void DoMaxUsedIndexTest() {
    int array_data[2] = { 0 };
    BaseMP mem_pool(array_data, sizeof(array_data),
                    sizeof(array_data[0]), ARRAY_SIZE(array_data));

    EXPECT_EQ(0, mem_pool.GetNumIndexesUsed());
    EXPECT_EQ(0, mem_pool.Allocate());

    EXPECT_EQ(1, mem_pool.GetNumIndexesUsed());

    mem_pool.Remove(0);

    EXPECT_EQ(1, mem_pool.GetNumIndexesUsed());
  }
int main(void)
{
	pid_t pid;
	int i;
	int n = 2;
	char buf[BUFSIZ];
	char *b;
	int size;
	char *p;

#if DEBUG_MEM_POOL == 2
	while (1) {
		b = fgets(buf, BUFSIZ, stdin);
		buf[strlen(b) - 1] = '\0';
		if (strcmp(b, "q") == 0)
			break;
		size = atoi(buf);
		
		p = mem_pool(size);
	}
	mem_pool_free();
#endif


#if DEBUG_MEM_POOL == 1								/* 匿名 内存 映射区, 用于有 父子,兄弟 关系的进程*/
	int *p, *q;

	p = (int *) memory_pool(4084);
	*p = 100;

	for (i = 0; i < n; i++)
		if ((pid = fork()) == 0)
			break;

	if (i == n) { /* parent */
		sleep(1);
		printf("*p = %d\n", *p);
	} else { /* child */
		*p = 200;
	}
	mem_pool_free();
#endif

	return 0;
}
Example #9
0
/* basic sequential allocation followed by 50 frees, then another 50 allocs */
int test_alloc_4(int argc, char **argv) {
	strategies strategy;
	int lbound = 1;
	int ubound = 4;

	if (strategyFromString(*(argv+1))>0)
		lbound=ubound=strategyFromString(*(argv+1));

	for (strategy = lbound; strategy <= ubound; strategy++)
	{
		int correct_holes = 0;
		int correct_alloc = 100;
		int correct_largest_free = 0;
		int i;

		void* lastPointer = NULL;
		initmem(strategy,100);
		for (i = 0; i < 100; i++)
		{
			void* pointer = mymalloc(1);
			if ( i > 0 && pointer != (lastPointer+1) )
			{
				printf("Allocation with %s was not sequential at %i; expected %p, actual %p\n", strategy_name(strategy), i,lastPointer+1,pointer);
				return 1;
			}
			lastPointer = pointer;
		}

		for (i = 1; i < 100; i+= 2)
		{
			myfree(mem_pool() + i);
		}

		for (i = 1; i < 100; i+=2)
		{
			void* pointer = mymalloc(1);
			if ( i > 1 && pointer != (lastPointer+2) )
			{
				printf("Second allocation with %s was not sequential at %i; expected %p, actual %p\n", strategy_name(strategy), i,lastPointer+1,pointer);
				return 1;
			}
			lastPointer = pointer;
		}

		if (mem_holes() != correct_holes)
		{
			printf("Holes not counted as %d with %s\n", correct_holes, strategy_name(strategy));
			return	1;
		}

		if (mem_allocated() != correct_alloc)
		{
			printf("Memory not reported as %d with %s\n", correct_alloc, strategy_name(strategy));
			return	1;
		}

		if (mem_largest_free() != correct_largest_free)
		{
			printf("Largest memory block free not reported as %d with %s\n", correct_largest_free, strategy_name(strategy));
			return	1;
		}

	}

	return 0;
}