/src/libreoffice/vcl/inc/graphic/MemoryManaged.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | */ |
9 | | |
10 | | #pragma once |
11 | | |
12 | | #include <sal/types.h> |
13 | | #include <rtl/strbuf.hxx> |
14 | | #include <vcl/timer.hxx> |
15 | | |
16 | | #include <graphic/Manager.hxx> |
17 | | |
18 | | #include <memory> |
19 | | #include <mutex> |
20 | | #include <chrono> |
21 | | #include <o3tl/sorted_vector.hxx> |
22 | | |
23 | | namespace vcl::graphic |
24 | | { |
25 | | class VCL_DLLPUBLIC MemoryManaged |
26 | | { |
27 | | private: |
28 | | sal_Int64 mnCurrentSizeBytes = 0; |
29 | | bool mbIsRegistered = false; |
30 | | |
31 | | protected: |
32 | | // Copy |
33 | | MemoryManaged(MemoryManaged const& rMemoryManaged) |
34 | 124 | : mbIsRegistered(rMemoryManaged.mbIsRegistered) |
35 | 124 | { |
36 | | // if the original is registered we need to register as well |
37 | 124 | if (mbIsRegistered) |
38 | 124 | registerIntoManager(); |
39 | 124 | } |
40 | | |
41 | | // Move |
42 | | MemoryManaged(MemoryManaged&& rMemoryManaged) |
43 | | : mbIsRegistered(rMemoryManaged.mbIsRegistered) |
44 | 0 | { |
45 | 0 | // if the original is registered we need to register as well |
46 | 0 | if (mbIsRegistered) |
47 | 0 | registerIntoManager(); |
48 | 0 | } |
49 | | |
50 | | MemoryManaged(bool bRegister) |
51 | 1.11M | { |
52 | 1.11M | if (bRegister) |
53 | 144k | registerIntoManager(); |
54 | 1.11M | } |
55 | | |
56 | 1.11M | virtual ~MemoryManaged() { unregisterFromManager(); } |
57 | | |
58 | | void updateCurrentSizeInBytes(sal_Int64 nNewSize) |
59 | 12.7k | { |
60 | 12.7k | if (mbIsRegistered) |
61 | 12.7k | { |
62 | 12.7k | auto& rManager = vcl::graphic::MemoryManager::get(); |
63 | 12.7k | rManager.changeExisting(this, nNewSize); |
64 | 12.7k | } |
65 | 0 | else |
66 | 0 | { |
67 | 0 | mnCurrentSizeBytes = nNewSize; |
68 | 0 | } |
69 | 12.7k | } |
70 | | |
71 | | void changeExisting(sal_Int64 nNewSize) |
72 | 160k | { |
73 | 160k | if (mbIsRegistered) |
74 | 156k | { |
75 | 156k | auto& rManager = vcl::graphic::MemoryManager::get(); |
76 | 156k | rManager.changeExisting(this, nNewSize); |
77 | 156k | } |
78 | 3.31k | else |
79 | 3.31k | { |
80 | 3.31k | mnCurrentSizeBytes = nNewSize; |
81 | 3.31k | } |
82 | 160k | } |
83 | | |
84 | | void swappedIn(sal_Int64 nNewSize) |
85 | 12.8k | { |
86 | 12.8k | if (mbIsRegistered) |
87 | 12.8k | { |
88 | 12.8k | auto& rManager = vcl::graphic::MemoryManager::get(); |
89 | 12.8k | rManager.swappedIn(this, nNewSize); |
90 | 12.8k | } |
91 | 0 | else |
92 | 0 | { |
93 | 0 | mnCurrentSizeBytes = nNewSize; |
94 | 0 | } |
95 | 12.8k | } |
96 | | |
97 | | void swappedOut(sal_Int64 nNewSize) |
98 | 0 | { |
99 | 0 | if (mbIsRegistered) |
100 | 0 | { |
101 | 0 | auto& rManager = vcl::graphic::MemoryManager::get(); |
102 | 0 | rManager.swappedOut(this, nNewSize); |
103 | 0 | } |
104 | 0 | else |
105 | 0 | { |
106 | 0 | mnCurrentSizeBytes = nNewSize; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | public: |
111 | 497k | sal_Int64 getCurrentSizeInBytes() const { return mnCurrentSizeBytes; } |
112 | | |
113 | 182k | void setCurrentSizeInBytes(sal_Int64 nSize) { mnCurrentSizeBytes = nSize; } |
114 | | |
115 | | protected: |
116 | | void registerIntoManager() |
117 | 160k | { |
118 | 160k | if (!mbIsRegistered) |
119 | 157k | { |
120 | 157k | auto& rManager = vcl::graphic::MemoryManager::get(); |
121 | 157k | rManager.registerObject(this); |
122 | 157k | mbIsRegistered = true; |
123 | 157k | } |
124 | 160k | } |
125 | | |
126 | | void unregisterFromManager() |
127 | 1.11M | { |
128 | 1.11M | if (mbIsRegistered) |
129 | 157k | { |
130 | 157k | auto& rManager = vcl::graphic::MemoryManager::get(); |
131 | 157k | rManager.unregisterObject(this); |
132 | 157k | mbIsRegistered = false; |
133 | 157k | } |
134 | 1.11M | } |
135 | | |
136 | | public: |
137 | | virtual bool canReduceMemory() const = 0; |
138 | | virtual bool reduceMemory() = 0; |
139 | | virtual std::chrono::high_resolution_clock::time_point getLastUsed() const = 0; |
140 | | virtual void dumpState(rtl::OStringBuffer& rState) = 0; |
141 | | }; |
142 | | |
143 | | } // end namespace vcl::graphic |
144 | | |
145 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |