Exemple #1
0
void PathfindQuit() {
	DestroyMemoryPool(g_PathDataPool);
	DestroyMemoryPool(g_PathPool);
	DestroyMemoryPool(g_PathScorePool);
	for(int i = 0; i < PATHTABLE_SIZE; ++i)
		free(g_PathStack.Stack[i]);
	SDL_DestroyMutex(g_PathStack.Lock);
	SDL_DestroySemaphore(g_PathStack.Sem);
}
/**
 * @brief Tester for FULMemoryPool, allocate, random free and random allocate again and again.
 * @note  This test is just for function test, test function works well in every condition,time test
 * is meaningless.
 */
int FUBMemoryPoolRandomTester()
{
	// To compute used time.
	struct timeval startTime, endTime;
	unsigned long long costTime = 0ULL;

	// To store available memory address got from system or pool.
	char **pStrings = (char **)malloc(sizeof(char *) * TEST_MALLOC_TIMES);
	for (int i=0; i<TEST_MALLOC_TIMES; ++i)
	{
		pStrings[i] = NULL;
	}
	// Generate variable length of string.
	int aRandom[TEST_MALLOC_TIMES];
	srand((unsigned int)time(NULL) + rand());
	for (int i=0; i<TEST_MALLOC_TIMES; ++i)
	{
		aRandom[i] = rand() % MALLOC_MAX_LEN;
		srand(aRandom[i]);
	}

	// Memory pool Malloc/Free test.
	PrintLog("Now testing memory pool Malloc/Free, FUB memory pool.");
	PrintLog("Note: This test is just for function test, time test is meaningless.");
	gettimeofday(&startTime, NULL);

	MemoryPool_t *pPool = CreateMemoryPool(MALLOC_MAX_LEN, FIRST_CHUNK_BLOCKS, GROW_CHUNK_BLOCKS);
	for (int i=0; i<TEST_RETRY_TIMES; ++i)
	{
		for (int j=0; j<TEST_MALLOC_TIMES; ++j)
		{
			if (aRandom[j] % 2)
			{
				pStrings[j] = (char *)Malloc(pPool);
				*pStrings[j] = '\0';
			}
		}
		for (int j=0; j<TEST_MALLOC_TIMES; ++j)
		{
			if ((aRandom[j] % 3) && pStrings[j])
			{
				Free(pPool, pStrings[j]);
				pStrings[j] = NULL;
			}
		}
	}
	DestroyMemoryPool(&pPool);

	gettimeofday(&endTime, NULL);
	costTime = 1000 * 1000 * (endTime.tv_sec - startTime.tv_sec) + endTime.tv_usec - startTime.tv_usec;
	printf("Memory pool Malloc/Free tested, malloc and free %d strings for %d times, cost %llu us.\n",
			TEST_MALLOC_TIMES, TEST_RETRY_TIMES, costTime);

	free(pStrings);
	return 0;
}
/**
 * @brief Tester for VALMemoryPool.
 */
int VALMemoryPoolTester()
{
	// To compute used time.
	struct timeval startTime, endTime;
	unsigned long long costTime = 0ULL;

	// To store available memory address got from system or pool.
	char **pStrings = (char **)malloc(sizeof(char *) * TEST_MALLOC_TIMES);
	// Generate variable length of string.
	int aStrLen[TEST_MALLOC_TIMES];
	srand((unsigned int)time(NULL) + rand());
	for (int i=0; i<TEST_MALLOC_TIMES; ++i)
	{
		aStrLen[i] = rand() % MALLOC_MAX_LEN;
		srand(aStrLen[i]);
	}

	// Memory pool Malloc/Free test.
	PrintLog("Now testing memory pool Malloc/Free, VAL memory pool.");
	gettimeofday(&startTime, NULL);

	MemoryPool_t *pPool = CreateMemoryPool(MALLOC_MAX_LEN);
	for (int i=0; i<TEST_RETRY_TIMES; ++i)
	{
		for (int j=0; j<TEST_MALLOC_TIMES; ++j)
		{
			pStrings[j] = Malloc(pPool, sizeof(char) * (aStrLen[j] + 1));
			//GenerateRandStr(pStrings[j], aStrLen[j]);
			*pStrings[j] = '\0';
			Free(pPool, pStrings[j]);
		}
	}
	/*
	 * The following code tests when wants to allocate a big block memory than pool can do, then deliver
	 * action to system, and record this memory, when destroy memory pool, all allocated big block will
	 * be release.
	 */
	/*for (int i=0; i<TEST_MALLOC_TIMES; ++i)
	{
		pStrings[i] = Malloc(pPool, sizeof(char) * (aStrLen[i] + MALLOC_MAX_LEN));
	}*/
	DestroyMemoryPool(&pPool);

	gettimeofday(&endTime, NULL);
	costTime = 1000 * 1000 * (endTime.tv_sec - startTime.tv_sec) + endTime.tv_usec - startTime.tv_usec;
	printf("Memory pool Malloc/Free tested, malloc and free %d strings for %d times, cost %llu us.\n",
			TEST_MALLOC_TIMES, TEST_RETRY_TIMES, costTime);

	free(pStrings);
	return 0;
}
S32 main()
{
    S32 ret;
    U32 length;
    U8  *keySalt;
    U8  *base64KeySalt;
    U8  index;

    /* Create a memory pool */
    pool = CreateMemoryPool(MEMORY_POOL_SIZE);
    if (pool == NULL)
    {
		DP("Memory pool failed to be created.\n");
        return RFAILED;
    }

    length = ((KEY_SALT_ORIG_LEN + 2) / 3) * 4 + 1;
    base64KeySalt = (U8 *)Calloc(pool, length);
    keySalt = (U8 *)Calloc(pool, KEY_SALT_ORIG_LEN);

    /* create master key and salt */
    DP("1. Start to create master key and salt...\n\n");
    ret = CreateCryptoKeySalt(base64KeySalt);
    if (ret != ROK)
    {
        DP("Master key and salt could not be created.\n");
        return ret;
    }
    DP("Base64 master key and salt[%d]:\n\"%s\"\n\n", strlen(base64KeySalt), base64KeySalt);

    /* base64 decoding */
    DP("2. Start to base64 decode master key and salt...\n\n");
    ret = Base64Decode(base64KeySalt, length - 1, keySalt);
    if (ret != ROK)
    {
        DP("Master key and salt could not be decoded.\n");
        return ret;
    }

    DP("Master key and salt:\n");
    for (index = 0; index < KEY_SALT_ORIG_LEN; index++)
    {
        DP("0x%02x ", keySalt[index]);
    }
    DP("\n\n");

    /* Destroy a memory pool */
    DestroyMemoryPool(pool);
    return ROK;
}
/**
 * @brief Tester for FULMemoryPool, as the order, allocate and free, repeat this progress.
 */
int FUBMemoryPoolOrderingTester()
{
	// To compute used time.
	struct timeval startTime, endTime;
	unsigned long long costTime = 0ULL;

	// To store available memory address got from system or pool.
	char **pStrings = (char **)malloc(sizeof(char *) * TEST_MALLOC_TIMES);

	// Memory pool Malloc/Free test.
	PrintLog("Now testing memory pool Malloc/Free, FUB memory pool.");
	gettimeofday(&startTime, NULL);

	MemoryPool_t *pPool = CreateMemoryPool(MALLOC_MAX_LEN, FIRST_CHUNK_BLOCKS, GROW_CHUNK_BLOCKS);
	for (int i=0; i<TEST_RETRY_TIMES; ++i)
	{
		for (int j=0; j<TEST_MALLOC_TIMES; ++j)
		{
			pStrings[j] = (char *)Malloc(pPool);
			*pStrings[j] = '\0';
			//GenerateRandStr(pStrings[j], MALLOC_MAX_LEN-1);
			Free(pPool, pStrings[j]);
		}
		/*for (int j=0; j<TEST_MALLOC_TIMES; ++j)
		{
			Free(pPool, pStrings[j]);
		}*/
	}
	DestroyMemoryPool(&pPool);

	gettimeofday(&endTime, NULL);
	costTime = 1000 * 1000 * (endTime.tv_sec - startTime.tv_sec) + endTime.tv_usec - startTime.tv_usec;
	printf("Memory pool Malloc/Free tested, malloc and free %d strings for %d times, cost %llu us.\n",
			TEST_MALLOC_TIMES, TEST_RETRY_TIMES, costTime);

	free(pStrings);
	return 0;
}