1// [Blend2D]
2// 2D Vector Graphics Powered by a JIT Compiler.
3//
4// [License]
5// Zlib - See LICENSE.md file in the package.
6
7#ifndef BLEND2D_BLARRAY_P_H
8#define BLEND2D_BLARRAY_P_H
9
10#include "./blapi-internal_p.h"
11#include "./blarray.h"
12#include "./blsupport_p.h"
13
14//! \cond INTERNAL
15//! \addtogroup blend2d_internal
16//! \{
17
18// ============================================================================
19// [BLArray - Internal]
20// ============================================================================
21
22BL_HIDDEN BLResult blArrayImplDelete(BLArrayImpl* impl) noexcept;
23
24static BL_INLINE BLResult blArrayImplRelease(BLArrayImpl* impl) noexcept {
25 if (blImplDecRefAndTest(impl))
26 return blArrayImplDelete(impl);
27 return BL_SUCCESS;
28}
29
30// ============================================================================
31// [BLArray - Utilities]
32// ============================================================================
33
34namespace {
35
36constexpr size_t blContainerSizeOf(size_t baseSize, size_t itemSize, size_t n) noexcept {
37 return baseSize + n * itemSize;
38}
39
40constexpr size_t blContainerCapacityOf(size_t baseSize, size_t itemSize, size_t implSize) noexcept {
41 return (implSize - baseSize) / itemSize;
42}
43
44//! Calculates the maximum theoretical capacity of items a container can hold.
45//! This is really a theoretical capacity that will never be reached in practice
46//! is it would mean that all addressable memory will be used by the data and
47//! mapped into a single continuous region, which is impossible.
48constexpr size_t blContainerMaximumCapacity(size_t baseSize, size_t itemSize) noexcept {
49 return blContainerCapacityOf(baseSize, itemSize, SIZE_MAX);
50}
51
52BL_INLINE size_t blContainerFittingCapacity(size_t baseSize, size_t itemSize, size_t n) noexcept {
53 size_t nInBytes = blAlignUp(baseSize + n * itemSize, 32);
54 size_t capacity = (nInBytes - baseSize) / itemSize;
55
56 BL_ASSERT(capacity >= n);
57 return capacity;
58}
59
60BL_INLINE size_t blContainerGrowingCapacity(size_t baseSize, size_t itemSize, size_t n, size_t minSizeInBytes) noexcept {
61 size_t nInBytes = baseSize + n * itemSize;
62 size_t optInBytes;
63
64 if (nInBytes < BL_ALLOC_GROW_LIMIT) {
65 optInBytes = blMax<size_t>(minSizeInBytes, blAlignUpPowerOf2(nInBytes + (nInBytes >> 1)));
66 }
67 else {
68 optInBytes = blMax<size_t>(nInBytes, blAlignUp(nInBytes, BL_ALLOC_GROW_LIMIT));
69 }
70
71 size_t capacity = (optInBytes - baseSize) / itemSize;
72 BL_ASSERT(capacity >= n);
73
74 return capacity;
75}
76
77} // {anonymous}
78
79//! \}
80//! \endcond
81
82#endif // BLEND2D_BLARRAY_P_H
83