alloc.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "alloc.h"
  17. #include "../common/sys/thread.h"
  18. namespace embree
  19. {
  20. struct fast_allocator_regression_test : public RegressionTest
  21. {
  22. BarrierSys barrier;
  23. std::atomic<size_t> numFailed;
  24. std::unique_ptr<FastAllocator> alloc;
  25. fast_allocator_regression_test()
  26. : RegressionTest("fast_allocator_regression_test"), numFailed(0)
  27. {
  28. registerRegressionTest(this);
  29. }
  30. static void thread_alloc(fast_allocator_regression_test* This)
  31. {
  32. FastAllocator::ThreadLocal2* threadalloc = This->alloc->threadLocal2();
  33. size_t* ptrs[1000];
  34. for (size_t j=0; j<1000; j++)
  35. {
  36. This->barrier.wait();
  37. for (size_t i=0; i<1000; i++) {
  38. ptrs[i] = (size_t*) threadalloc->alloc0->malloc(sizeof(size_t)+(i%32));
  39. *ptrs[i] = size_t(threadalloc) + i;
  40. }
  41. for (size_t i=0; i<1000; i++) {
  42. if (*ptrs[i] != size_t(threadalloc) + i)
  43. This->numFailed++;
  44. }
  45. This->barrier.wait();
  46. }
  47. }
  48. bool run ()
  49. {
  50. alloc = make_unique(new FastAllocator(nullptr,false));
  51. numFailed.store(0);
  52. size_t numThreads = getNumberOfLogicalThreads();
  53. barrier.init(numThreads+1);
  54. /* create threads */
  55. std::vector<thread_t> threads;
  56. for (size_t i=0; i<numThreads; i++)
  57. threads.push_back(createThread((thread_func)thread_alloc,this));
  58. /* run test */
  59. for (size_t i=0; i<1000; i++)
  60. {
  61. alloc->reset();
  62. barrier.wait();
  63. barrier.wait();
  64. }
  65. /* destroy threads */
  66. for (size_t i=0; i<numThreads; i++)
  67. join(threads[i]);
  68. alloc = nullptr;
  69. return numFailed == 0;
  70. }
  71. };
  72. fast_allocator_regression_test fast_allocator_regression;
  73. }