Path: blob/master/thirdparty/embree/kernels/common/alloc.cpp
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#include "alloc.h"4#include "../../common/sys/thread.h"5#if defined(APPLE) && defined(__aarch64__)6#include "../../common/sys/barrier.h"7#endif89namespace embree10{11__thread FastAllocator::ThreadLocal2* FastAllocator::thread_local_allocator2 = nullptr;12MutexSys FastAllocator::s_thread_local_allocators_lock;13std::vector<std::unique_ptr<FastAllocator::ThreadLocal2>> FastAllocator::s_thread_local_allocators;1415struct fast_allocator_regression_test : public RegressionTest16{17BarrierSys barrier;18std::atomic<size_t> numFailed;19std::unique_ptr<FastAllocator> alloc;2021fast_allocator_regression_test()22: RegressionTest("fast_allocator_regression_test"), numFailed(0)23{24registerRegressionTest(this);25}2627static void thread_alloc(fast_allocator_regression_test* This)28{29FastAllocator::CachedAllocator threadalloc = This->alloc->getCachedAllocator();3031size_t* ptrs[1000];32for (size_t j=0; j<1000; j++)33{34This->barrier.wait();35for (size_t i=0; i<1000; i++) {36ptrs[i] = (size_t*) threadalloc.malloc0(sizeof(size_t)+(i%32));37*ptrs[i] = size_t(threadalloc.talloc0) + i;38}39for (size_t i=0; i<1000; i++) {40if (*ptrs[i] != size_t(threadalloc.talloc0) + i)41This->numFailed++;42}43This->barrier.wait();44}45}4647bool run ()48{49alloc = make_unique(new FastAllocator(nullptr,false));50numFailed.store(0);5152size_t numThreads = getNumberOfLogicalThreads();53barrier.init(numThreads+1);5455/* create threads */56std::vector<thread_t> threads;57for (size_t i=0; i<numThreads; i++)58threads.push_back(createThread((thread_func)thread_alloc,this));5960/* run test */61for (size_t i=0; i<1000; i++)62{63alloc->reset();64barrier.wait();65barrier.wait();66}6768/* destroy threads */69for (size_t i=0; i<numThreads; i++)70join(threads[i]);7172alloc = nullptr;7374return numFailed == 0;75}76};7778fast_allocator_regression_test fast_allocator_regression;79}8081828384