void PathfindInit() { g_PathDataPool = CreateMemoryPool(sizeof(struct PathData), PATH_PATHPOOLSZ); g_PathPool = CreateMemoryPool(sizeof(struct Path), PATH_PATHPOOLSZ); g_PathScorePool = CreateMemoryPool(sizeof(struct PathNodeScore), PATH_PATHSCORESZ); for(int i = 0; i < PATHTABLE_SIZE; ++i) g_PathStack.Stack[i] = calloc(PATHFIND_OPENSIZE, sizeof(struct PathNodeScore*)); g_PathStack.Lock = SDL_CreateMutex(); g_PathStack.Top = 0; g_PathStack.Sem = SDL_CreateSemaphore(PATHTABLE_SIZE); }
int main (void) { BaseType_t rc; int i; ThreadParameters_t params[5] = { {1, 1}, {2, 3}, {3, 5}, {4, 7}, {5, 11}, }; printf("Testing mem_pools\n"); pool_1 = CreateMemoryPool(1, 10, 8); pool_2 = CreateMemoryPool(2, 10, 4); pool_3 = CreateMemoryPool(3, 10, 2); pool_4 = CreateMemoryPool(4, 10, 1); pool_5 = CreateMemoryPool(5, 10, 2); pool_6 = CreateMemoryPool(6, 10, 4); pool_7 = CreateMemoryPool(7, 10, 8); pool_8 = CreateMemoryPool(8, 10, 16); pool_9 = CreateMemoryPool(9, 10, 4); for (i = 0; i < 5; i++) { rc = xTaskCreate( TestThread, "test", 1000, (void *)¶ms[i], 3, NULL); /** * Make sure out task was created. */ configASSERT(rc == pdPASS); } /** * Start FreeRTOS here. */ vTaskStartScheduler(); /* * We shouldn't ever get here unless someone calls * vTaskEndScheduler(). Note that there appears to be a * bug in the Linux FreeRTOS simulator that crashes when * this is called. */ printf("Scheduler ended!\n"); return 0; }
/** * @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; }