summaryrefslogtreecommitdiff
path: root/vcl/inc/graphic/Manager.hxx
blob: 0a4ee3fbc84d1ff48cb7977ef1c4938f5a4717b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <sal/types.h>
#include <rtl/strbuf.hxx>
#include <vcl/timer.hxx>

#include <memory>
#include <mutex>
#include <chrono>
#include <o3tl/sorted_vector.hxx>

namespace vcl::graphic
{
class MemoryManaged;

class VCL_DLLPUBLIC MemoryManager final
{
private:
    o3tl::sorted_vector<MemoryManaged*> maObjectList;
    sal_Int64 mnTotalSize = 0;
    std::mutex maMutex; // instead of SolarMutex because graphics can live past vcl main

    std::chrono::seconds mnAllowedIdleTime = std::chrono::seconds(1);
    bool mbSwapEnabled = true;
    bool mbReducingGraphicMemory = false;
    sal_Int64 mnMemoryLimit = 10'000'000;
    Timer maSwapOutTimer;
    sal_Int32 mnTimeout = 1'000;
    sal_Int64 mnSmallFrySize = 100'000;

    DECL_LINK(ReduceMemoryTimerHandler, Timer*, void);

public:
    MemoryManager();
    void registerObject(MemoryManaged* pObject);
    void unregisterObject(MemoryManaged* pObject);
    void changeExisting(MemoryManaged* pObject, sal_Int64 nNewSize);

    void swappedIn(MemoryManaged* pObject, sal_Int64 nNewSize);
    void swappedOut(MemoryManaged* pObject, sal_Int64 nNewSize);

    static MemoryManager& get();
    o3tl::sorted_vector<MemoryManaged*> const& getManagedObjects() { return maObjectList; }
    sal_Int64 getTotalSize() { return mnTotalSize; }

    void reduceMemory(std::unique_lock<std::mutex>& rGuard, bool bDropAll = false);
    void loopAndReduceMemory(std::unique_lock<std::mutex>& rGuard, bool bDropAll = false);
    void reduceAllAndNow();
    void dumpState(rtl::OStringBuffer& rState);
};

} // end namespace vcl::graphic

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */