Blender V4.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
10#include "MEM_guardedalloc.h"
11
12#include "BLI_array_store.h"
13#include "BLI_array_store_utils.h" /* own include */
14
15#include "BLI_math_base.h"
16
18 const int stride,
19 const int chunk_size)
20{
21 if (bs_stride->stride_table_len < stride) {
23 bs_stride->stride_table = static_cast<BArrayStore **>(
25 }
26 BArrayStore **bs_p = &bs_stride->stride_table[stride - 1];
27
28 if ((*bs_p) == nullptr) {
29 /* calculate best chunk-count to fit a power of two */
30 uint chunk_count = chunk_size;
31 {
32 uint size = chunk_count * stride;
33 size = power_of_2_max_u(size);
34 size = MEM_SIZE_OPTIMAL(size);
35 chunk_count = size / stride;
36 }
37
38 (*bs_p) = BLI_array_store_create(stride, chunk_count);
39 }
40 return *bs_p;
41}
42
44{
45 BLI_assert(stride > 0 && stride <= bs_stride->stride_table_len);
46 return bs_stride->stride_table[stride - 1];
47}
48
50{
51 for (int i = 0; i < bs_stride->stride_table_len; i += 1) {
52 if (bs_stride->stride_table[i]) {
54 }
55 }
56
57 /* It's possible this table was never used. */
60}
61
63 size_t *r_size_expanded,
64 size_t *r_size_compacted)
65{
66 size_t size_compacted = 0;
67 size_t size_expanded = 0;
68 for (int i = 0; i < bs_stride->stride_table_len; i++) {
69 const BArrayStore *bs = bs_stride->stride_table[i];
70 if (bs) {
71 size_compacted += BLI_array_store_calc_size_compacted_get(bs);
72 size_expanded += BLI_array_store_calc_size_expanded_get(bs);
73 }
74 }
75
76 *r_size_expanded = size_expanded;
77 *r_size_compacted = size_compacted;
78}
Efficient in-memory storage of multiple similar arrays.
size_t BLI_array_store_calc_size_expanded_get(const BArrayStore *bs)
void BLI_array_store_destroy(BArrayStore *bs)
size_t BLI_array_store_calc_size_compacted_get(const BArrayStore *bs)
BArrayStore * BLI_array_store_create(unsigned int stride, unsigned int chunk_count)
#define BLI_assert(a)
Definition BLI_assert.h:50
MINLINE unsigned int power_of_2_max_u(unsigned int x)
unsigned int uint
Read Guarded memory(de)allocation.
#define MEM_recallocN(vmemh, len)
#define MEM_SAFE_FREE(v)
#define MEM_SIZE_OPTIMAL(size)
void BLI_array_store_at_size_calc_memory_usage(const BArrayStore_AtSize *bs_stride, size_t *r_size_expanded, size_t *r_size_compacted)
BArrayStore * BLI_array_store_at_size_get(BArrayStore_AtSize *bs_stride, const int stride)
BArrayStore * BLI_array_store_at_size_ensure(BArrayStore_AtSize *bs_stride, const int stride, const int chunk_size)
void BLI_array_store_at_size_clear(BArrayStore_AtSize *bs_stride)
BArrayStore_AtSize bs_stride
struct BArrayStore ** stride_table