tstRTMemPool.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * IPRT Testcase - MemPool.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2009 Oracle Corporation
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * available from http://www.virtualbox.org. This file is free software;
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * General Public License (GPL) as published by the Free Software
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * The contents of this file may alternatively be used under the terms
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * of the Common Development and Distribution License Version 1.0
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * CDDL are applicable instead of those of the GPL.
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * You may elect to license modified versions of this file under the
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * terms and conditions of either the GPL or the CDDL or both.
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync/*******************************************************************************
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync* Header Files *
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync*******************************************************************************/
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync/*******************************************************************************
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync* Global Variables *
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync*******************************************************************************/
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync/** The test handle */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync/** Memory pool for tst4. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Basic API checks.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * We'll return if any of these fails.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* Normal alloc. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, 1));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, 0));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* Zeroed allocation. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolAllocZ(hMemPool, 1024));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(ASMMemIsAllU32(pv, 1024, 0) == NULL);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolAllocZ(hMemPool, 0));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* Duped allocation. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolDup(hMemPool, szTest, sizeof(szTest)));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(memcmp(pv, szTest, sizeof(szTest)) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolDupEx(hMemPool, szTest, sizeof(szTest), cb));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(memcmp(pv, szTest, sizeof(szTest)) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(ASMMemIsAll8((uint8_t *)pv + sizeof(szTest), cb, 0) == NULL);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* Reallocation */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, NULL, 1));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, pv, 2));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, 42));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, pv, 32));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, NULL, 128));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, pv, 256));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRealloc(hMemPool, pv, 0) == NULL);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* Free (a bit hard to test) */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTMemPoolFree(hMemPool, RTMemPoolAlloc(hMemPool, 42));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* Memory referencing. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, i));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(RTMemPoolRelease(hMemPool, pv) == 3);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(RTMemPoolRelease(NIL_RTMEMPOOL, pv) == 5);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(RTMemPoolRelease(NIL_RTMEMPOOL, pv) == 4);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(RTMemPoolRelease(hMemPool, pv) == cRefs);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x cRefs=%d\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv, cRefs));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(pv2 = RTMemPoolAlloc(hMemPool, i));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK(RTMemPoolRelease(hMemPool, pv2) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemIsAll8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x cRefs=%d\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv, cRefs));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Test automatic cleanup upon destruction.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsyncstatic void tst3(void)
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Nothing freed.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_RETV(RTMemPoolCreate(&hMemPool, "test 3a"), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_RETV(RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Pseudo random freeing to test list maintaince.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_RETV(RTMemPoolCreate(&hMemPool, "test 3b"), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync for (j = 0; j < RT_ELEMENTS(apvHistory) - i * 7; j++)
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(apvHistory[j] = RTMemPoolAlloc(hMemPool, j));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, apvHistory[iFree]) == 0);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_RETV(RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTestIPrintf(RTTESTLVL_INFO, "cBlocks=%u j=%u\n", cBlocks, j);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync/** Thread function for tst4. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsyncstatic DECLCALLBACK(int) tst4Thread(RTTHREAD hSelf, void *pvArg)
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync// uint32_t iThread = (uint32_t)(uintptr_t)pvArg;
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* setup. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* wait for the kick-off */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* do the work. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync for (j = 0; j < RT_ELEMENTS(apvHistory) - (i % 200); j++)
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RET(apvHistory[j] = RTMemPoolAlloc(hMemPool, (i & 15) + (j & 63)), VERR_NO_MEMORY);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RET(RTMemPoolRelease(hMemPool, apvHistory[k]) == 0, VERR_INTERNAL_ERROR);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync while (j-- > 0)
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RET(RTMemPoolRelease(hMemPool, apvHistory[j]) == 0, VERR_INTERNAL_ERROR);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync/** sub test */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTestISubF("Serialization - %u threads", cThreads);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_RETV(RTMemPoolCreate(&hMemPool, "test 2a"), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync PRTTHREAD pahThreads = (PRTTHREAD)RTMemPoolAlloc(hMemPool, cThreads * sizeof(RTTHREAD));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* start them. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync int rc = RTThreadCreateF(&pahThreads[i], tst4Thread, (void *)(uintptr_t)i, 0,
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tst4-%u/%u", i, cThreads);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* kick them off. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC_OK(RTThreadUserSignal(pahThreads[i]));
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync /* wait for them. */
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync int rc = RTThreadWait(pahThreads[i], 2*60*1000, NULL);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Starts a bunch of threads beating on a pool to test serialization.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsyncstatic void tst4(void)
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Test it with a few different thread counts.
176991ff77f632fbe0e92e3aa17cf249367a309bvboxsync int rc = RTTestInitAndCreate("tstRTMemPool", &hTest);
5ff501b3f5f8ba5172a6d8ad1a2a99b103a5d058vboxsync * Smoke tests using first the default and then a custom pool.
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolCreate(&hMemPool, "test 2a"), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(NIL_RTMEMPOOL), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(RTMEMPOOL_DEFAULT), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(RTMEMPOOL_DEFAULT), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolCreate(&hMemPool, "test 2b"), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
8ca5eef802d34e34f85919c5d39b510eeab3de3bvboxsync * Further tests.