2019-09-12 14:23:21 +02:00
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-09-12 17:03:03 +02:00
|
|
|
#include <atomic>
|
2019-09-12 14:23:21 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2020-04-21 17:31:56 +02:00
|
|
|
#include "BLI_array.hh"
|
|
|
|
|
#include "BLI_index_range.hh"
|
2020-06-09 11:58:47 +02:00
|
|
|
#include "BLI_span.hh"
|
2020-04-21 17:31:56 +02:00
|
|
|
#include "BLI_vector.hh"
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
namespace blender {
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-07-20 16:00:20 +02:00
|
|
|
static RawVector<RawArray<int64_t, 0>> arrays;
|
2020-07-20 12:16:20 +02:00
|
|
|
static int64_t current_array_size = 0;
|
|
|
|
|
static int64_t *current_array = nullptr;
|
2019-09-12 14:23:21 +02:00
|
|
|
static std::mutex current_array_mutex;
|
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
Span<int64_t> IndexRange::as_span() const
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t min_required_size = start_ + size_;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
|
if (min_required_size <= current_array_size) {
|
2020-07-20 12:16:20 +02:00
|
|
|
return Span<int64_t>(current_array + start_, size_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(current_array_mutex);
|
|
|
|
|
|
|
|
|
|
if (min_required_size <= current_array_size) {
|
2020-07-20 12:16:20 +02:00
|
|
|
return Span<int64_t>(current_array + start_, size_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t new_size = std::max<int64_t>(1000, power_of_2_max_u(min_required_size));
|
2020-07-20 16:00:20 +02:00
|
|
|
RawArray<int64_t, 0> new_array(new_size);
|
2020-07-20 12:16:20 +02:00
|
|
|
for (int64_t i = 0; i < new_size; i++) {
|
2019-09-12 14:23:21 +02:00
|
|
|
new_array[i] = i;
|
|
|
|
|
}
|
|
|
|
|
arrays.append(std::move(new_array));
|
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
current_array = arrays.last().data();
|
2019-09-12 14:23:21 +02:00
|
|
|
std::atomic_thread_fence(std::memory_order_seq_cst);
|
|
|
|
|
current_array_size = new_size;
|
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
return Span<int64_t>(current_array + start_, size_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
} // namespace blender
|