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_BLZEROALLOCATOR_P_H
8#define BLEND2D_BLZEROALLOCATOR_P_H
9
10#include "./blapi-internal_p.h"
11
12//! \cond INTERNAL
13//! \addtogroup blend2d_internal
14//! \{
15
16// ============================================================================
17// [BLZeroMem]
18// ============================================================================
19
20BL_HIDDEN void* blZeroAllocatorAlloc(size_t size, size_t* allocatedSize) noexcept;
21BL_HIDDEN void* blZeroAllocatorResize(void* prevPtr, size_t prevSize, size_t size, size_t* allocatedSize) noexcept;
22BL_HIDDEN void blZeroAllocatorRelease(void* ptr, size_t size) noexcept;
23
24// ============================================================================
25// [BLZeroBuffer]
26// ============================================================================
27
28//! Memory buffer that is initially zeroed and that must be zeroed upon release.
29class BLZeroBuffer {
30public:
31 BL_NONCOPYABLE(BLZeroBuffer)
32
33 //! Zero allocated data.
34 uint8_t* data;
35 //! Size of the buffer.
36 size_t size;
37
38 // --------------------------------------------------------------------------
39 // [Construction / Destruction]
40 // --------------------------------------------------------------------------
41
42 BL_INLINE BLZeroBuffer() noexcept
43 : data(nullptr),
44 size(0) {}
45
46 BL_INLINE BLZeroBuffer(BLZeroBuffer&& other) noexcept
47 : data(other.data),
48 size(other.size) {
49 other.data = nullptr;
50 other.size = 0;
51 }
52
53 BL_INLINE ~BLZeroBuffer() noexcept {
54 if (data)
55 blZeroAllocatorRelease(data, size);
56 }
57
58 // --------------------------------------------------------------------------
59 // [Allocation]
60 // --------------------------------------------------------------------------
61
62 BL_INLINE BLResult ensure(size_t minimumSize) noexcept {
63 if (minimumSize <= size)
64 return BL_SUCCESS;
65
66 data = static_cast<uint8_t*>(blZeroAllocatorResize(data, size, minimumSize, &size));
67 return data ? BL_SUCCESS : blTraceError(BL_ERROR_OUT_OF_MEMORY);
68 }
69
70 BL_INLINE void release() noexcept {
71 if (data) {
72 blZeroAllocatorRelease(data, size);
73 data = nullptr;
74 size = 0;
75 }
76 }
77};
78
79//! \}
80//! \endcond
81
82#endif // BLEND2D_BLZEROALLOCATOR_P_H
83