Blender  V2.93
BLI_index_range.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include <atomic>
18 #include <mutex>
19 
20 #include "BLI_array.hh"
21 #include "BLI_index_range.hh"
22 #include "BLI_span.hh"
23 #include "BLI_vector.hh"
24 
25 namespace blender {
26 
29 static int64_t *current_array = nullptr;
31 
33 {
34  int64_t min_required_size = start_ + size_;
35 
36  if (min_required_size <= current_array_size) {
37  return Span<int64_t>(current_array + start_, size_);
38  }
39 
40  std::lock_guard<std::mutex> lock(current_array_mutex);
41 
42  if (min_required_size <= current_array_size) {
43  return Span<int64_t>(current_array + start_, size_);
44  }
45 
46  int64_t new_size = std::max<int64_t>(1000, power_of_2_max_u(min_required_size));
47  RawArray<int64_t, 0> new_array(new_size);
48  for (int64_t i = 0; i < new_size; i++) {
49  new_array[i] = i;
50  }
51  arrays.append(std::move(new_array));
52 
53  current_array = arrays.last().data();
54  std::atomic_thread_fence(std::memory_order_seq_cst);
55  current_array_size = new_size;
56 
57  return Span<int64_t>(current_array + start_, size_);
58 }
59 
60 } // namespace blender
MINLINE unsigned int power_of_2_max_u(unsigned int x)
ThreadMutex mutex
Span< int64_t > as_span() const
static int64_t * current_array
static RawVector< RawArray< int64_t, 0 > > arrays
static std::mutex current_array_mutex
static int64_t current_array_size
__int64 int64_t
Definition: stdint.h:92