alloc.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "alloc.h"
  4. #include "../../common/sys/thread.h"
  5. namespace embree
  6. {
  7. __thread FastAllocator::ThreadLocal2* FastAllocator::thread_local_allocator2 = nullptr;
  8. SpinLock FastAllocator::s_thread_local_allocators_lock;
  9. std::vector<std::unique_ptr<FastAllocator::ThreadLocal2>> FastAllocator::s_thread_local_allocators;
  10. struct fast_allocator_regression_test : public RegressionTest
  11. {
  12. BarrierSys barrier;
  13. std::atomic<size_t> numFailed;
  14. std::unique_ptr<FastAllocator> alloc;
  15. fast_allocator_regression_test()
  16. : RegressionTest("fast_allocator_regression_test"), numFailed(0)
  17. {
  18. registerRegressionTest(this);
  19. }
  20. static void thread_alloc(fast_allocator_regression_test* This)
  21. {
  22. FastAllocator::CachedAllocator threadalloc = This->alloc->getCachedAllocator();
  23. size_t* ptrs[1000];
  24. for (size_t j=0; j<1000; j++)
  25. {
  26. This->barrier.wait();
  27. for (size_t i=0; i<1000; i++) {
  28. ptrs[i] = (size_t*) threadalloc.malloc0(sizeof(size_t)+(i%32));
  29. *ptrs[i] = size_t(threadalloc.talloc0) + i;
  30. }
  31. for (size_t i=0; i<1000; i++) {
  32. if (*ptrs[i] != size_t(threadalloc.talloc0) + i)
  33. This->numFailed++;
  34. }
  35. This->barrier.wait();
  36. }
  37. }
  38. bool run ()
  39. {
  40. alloc = make_unique(new FastAllocator(nullptr,false));
  41. numFailed.store(0);
  42. size_t numThreads = getNumberOfLogicalThreads();
  43. barrier.init(numThreads+1);
  44. /* create threads */
  45. std::vector<thread_t> threads;
  46. for (size_t i=0; i<numThreads; i++)
  47. threads.push_back(createThread((thread_func)thread_alloc,this));
  48. /* run test */
  49. for (size_t i=0; i<1000; i++)
  50. {
  51. alloc->reset();
  52. barrier.wait();
  53. barrier.wait();
  54. }
  55. /* destroy threads */
  56. for (size_t i=0; i<numThreads; i++)
  57. join(threads[i]);
  58. alloc = nullptr;
  59. return numFailed == 0;
  60. }
  61. };
  62. fast_allocator_regression_test fast_allocator_regression;
  63. }