Blender V5.3
array_store_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include "MEM_guardedalloc.h"
11
12#include "BLI_array_store.hh"
13#include "BLI_array_store_utils.hh" /* own include */
14
15#include "BLI_math_base_c.hh"
16
17namespace blender {
18
20 const int stride,
21 const int chunk_size)
22{
23 if (bs_stride->stride_table_len < stride) {
24 bs_stride->stride_table_len = stride;
25 bs_stride->stride_table = static_cast<BArrayStore **>(
26 MEM_realloc_zeroed(bs_stride->stride_table, sizeof(*bs_stride->stride_table) * stride));
27 }
28 BArrayStore **bs_p = &bs_stride->stride_table[stride - 1];
29
30 if ((*bs_p) == nullptr) {
31 /* calculate best chunk-count to fit a power of two */
32 uint chunk_count = chunk_size;
33 {
34 uint size = chunk_count * stride;
35 size = power_of_2_max_u(size);
36 size = MEM_SIZE_OPTIMAL(size);
37 chunk_count = size / stride;
38 }
39
40 (*bs_p) = BLI_array_store_create(stride, chunk_count);
41 }
42 return *bs_p;
43}
44
46{
47 BLI_assert(stride > 0 && stride <= bs_stride->stride_table_len);
48 return bs_stride->stride_table[stride - 1];
49}
50
52{
53 for (int i = 0; i < bs_stride->stride_table_len; i += 1) {
54 if (bs_stride->stride_table[i]) {
56 }
57 }
58
59 /* It's possible this table was never used. */
60 MEM_SAFE_DELETE(bs_stride->stride_table);
61 bs_stride->stride_table_len = 0;
62}
63
65 size_t *r_size_expanded,
66 size_t *r_size_compacted)
67{
68 size_t size_compacted = 0;
69 size_t size_expanded = 0;
70 for (int i = 0; i < bs_stride->stride_table_len; i++) {
71 const BArrayStore *bs = bs_stride->stride_table[i];
72 if (bs) {
73 size_compacted += BLI_array_store_calc_size_compacted_get(bs);
74 size_expanded += BLI_array_store_calc_size_expanded_get(bs);
75 }
76 }
77
78 *r_size_expanded = size_expanded;
79 *r_size_compacted = size_compacted;
80}
81
82} // namespace blender
Efficient in-memory storage of multiple similar arrays.
Read Blender memory allocation and freeing.
#define MEM_realloc_zeroed(vmemh, len)
#define MEM_SIZE_OPTIMAL(size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct BArrayStore * BLI_array_store_at_size_get(struct BArrayStore_AtSize *bs_stride, int stride)
unsigned int uint
Definition BKE_text.h:55
struct BArrayStore * BLI_array_store_at_size_ensure(struct BArrayStore_AtSize *bs_stride, int stride, int chunk_size)
MINLINE unsigned int power_of_2_max_u(unsigned int x)
void BLI_array_store_at_size_calc_memory_usage(const struct BArrayStore_AtSize *bs_stride, size_t *r_size_expanded, size_t *r_size_compacted)
BArrayStore * BLI_array_store_create(unsigned int stride, unsigned int chunk_count)
size_t BLI_array_store_calc_size_expanded_get(const BArrayStore *bs)
void BLI_array_store_at_size_clear(struct BArrayStore_AtSize *bs_stride)
size_t BLI_array_store_calc_size_compacted_get(const BArrayStore *bs)
void BLI_array_store_destroy(BArrayStore *bs)