You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

allocator_tests.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2012-2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "util.h"
  5. #include "support/allocators/secure.h"
  6. #include "test/test_bitcoin.h"
  7. #include <boost/test/unit_test.hpp>
  8. BOOST_FIXTURE_TEST_SUITE(allocator_tests, BasicTestingSetup)
  9. // Dummy memory page locker for platform independent tests
  10. static const void *last_lock_addr, *last_unlock_addr;
  11. static size_t last_lock_len, last_unlock_len;
  12. class TestLocker
  13. {
  14. public:
  15. bool Lock(const void *addr, size_t len)
  16. {
  17. last_lock_addr = addr;
  18. last_lock_len = len;
  19. return true;
  20. }
  21. bool Unlock(const void *addr, size_t len)
  22. {
  23. last_unlock_addr = addr;
  24. last_unlock_len = len;
  25. return true;
  26. }
  27. };
  28. BOOST_AUTO_TEST_CASE(test_LockedPageManagerBase)
  29. {
  30. const size_t test_page_size = 4096;
  31. LockedPageManagerBase<TestLocker> lpm(test_page_size);
  32. size_t addr;
  33. last_lock_addr = last_unlock_addr = 0;
  34. last_lock_len = last_unlock_len = 0;
  35. /* Try large number of small objects */
  36. addr = 0;
  37. for(int i=0; i<1000; ++i)
  38. {
  39. lpm.LockRange(reinterpret_cast<void*>(addr), 33);
  40. addr += 33;
  41. }
  42. /* Try small number of page-sized objects, straddling two pages */
  43. addr = test_page_size*100 + 53;
  44. for(int i=0; i<100; ++i)
  45. {
  46. lpm.LockRange(reinterpret_cast<void*>(addr), test_page_size);
  47. addr += test_page_size;
  48. }
  49. /* Try small number of page-sized objects aligned to exactly one page */
  50. addr = test_page_size*300;
  51. for(int i=0; i<100; ++i)
  52. {
  53. lpm.LockRange(reinterpret_cast<void*>(addr), test_page_size);
  54. addr += test_page_size;
  55. }
  56. /* one very large object, straddling pages */
  57. lpm.LockRange(reinterpret_cast<void*>(test_page_size*600+1), test_page_size*500);
  58. BOOST_CHECK(last_lock_addr == reinterpret_cast<void*>(test_page_size*(600+500)));
  59. /* one very large object, page aligned */
  60. lpm.LockRange(reinterpret_cast<void*>(test_page_size*1200), test_page_size*500-1);
  61. BOOST_CHECK(last_lock_addr == reinterpret_cast<void*>(test_page_size*(1200+500-1)));
  62. BOOST_CHECK(lpm.GetLockedPageCount() == (
  63. (1000*33+test_page_size-1)/test_page_size + // small objects
  64. 101 + 100 + // page-sized objects
  65. 501 + 500)); // large objects
  66. BOOST_CHECK((last_lock_len & (test_page_size-1)) == 0); // always lock entire pages
  67. BOOST_CHECK(last_unlock_len == 0); // nothing unlocked yet
  68. /* And unlock again */
  69. addr = 0;
  70. for(int i=0; i<1000; ++i)
  71. {
  72. lpm.UnlockRange(reinterpret_cast<void*>(addr), 33);
  73. addr += 33;
  74. }
  75. addr = test_page_size*100 + 53;
  76. for(int i=0; i<100; ++i)
  77. {
  78. lpm.UnlockRange(reinterpret_cast<void*>(addr), test_page_size);
  79. addr += test_page_size;
  80. }
  81. addr = test_page_size*300;
  82. for(int i=0; i<100; ++i)
  83. {
  84. lpm.UnlockRange(reinterpret_cast<void*>(addr), test_page_size);
  85. addr += test_page_size;
  86. }
  87. lpm.UnlockRange(reinterpret_cast<void*>(test_page_size*600+1), test_page_size*500);
  88. lpm.UnlockRange(reinterpret_cast<void*>(test_page_size*1200), test_page_size*500-1);
  89. /* Check that everything is released */
  90. BOOST_CHECK(lpm.GetLockedPageCount() == 0);
  91. /* A few and unlocks of size zero (should have no effect) */
  92. addr = 0;
  93. for(int i=0; i<1000; ++i)
  94. {
  95. lpm.LockRange(reinterpret_cast<void*>(addr), 0);
  96. addr += 1;
  97. }
  98. BOOST_CHECK(lpm.GetLockedPageCount() == 0);
  99. addr = 0;
  100. for(int i=0; i<1000; ++i)
  101. {
  102. lpm.UnlockRange(reinterpret_cast<void*>(addr), 0);
  103. addr += 1;
  104. }
  105. BOOST_CHECK(lpm.GetLockedPageCount() == 0);
  106. BOOST_CHECK((last_unlock_len & (test_page_size-1)) == 0); // always unlock entire pages
  107. }
  108. BOOST_AUTO_TEST_SUITE_END()