alloc.cpp 2.2 KB

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