vsg: include/vsg/core/MemorySlots.h Source File

vsg 1.1.8 VulkanSceneGraph library

Loading...
Searching...
No Matches
MemorySlots.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Export.h>
16
17#include <list>
18#include <map>
19#include <ostream>
20#include <vector>
21
22namespace vsg
23{
24
26 enum MemoryTracking
27 {
28 MEMORY_TRACKING_NO_CHECKS = 0,
29 MEMORY_TRACKING_REPORT_ACTIONS = 1,
30 MEMORY_TRACKING_CHECK_ACTIONS = 2,
31 MEMORY_TRACKING_DEFAULT = MEMORY_TRACKING_NO_CHECKS
32 };
33
35 class VSG_DECLSPEC MemorySlots
36 {
37 public:
38 explicit MemorySlots(size_t availableMemorySize, int in_memoryTracking = MEMORY_TRACKING_DEFAULT);
39 ~MemorySlots();
40
41 using OptionalOffset = std::pair<bool, size_t>;
42 OptionalOffset reserve(size_t size, size_t alignment);
43
44 bool release(size_t offset, size_t size);
45
46 bool full() const { return _availableMemory.empty(); }
47 bool empty() const { return totalAvailableSize() == totalMemorySize(); }
48
49 size_t maximumAvailableSpace() const { return _availableMemory.empty() ? 0 : _availableMemory.rbegin()->first; }
50 size_t totalAvailableSize() const;
51 size_t totalReservedSize() const;
52 size_t totalMemorySize() const { return _totalMemorySize; }
53
54 // debug facilities
55 void report(std::ostream& out) const;
56 bool check() const;
57
58 mutable int memoryTracking = MEMORY_TRACKING_DEFAULT;
59
60 protected:
61 std::multimap<size_t, size_t> _availableMemory;
62 std::map<size_t, size_t> _offsetSizes;
63 std::map<size_t, size_t> _reservedMemory;
64
65 void insertAvailableSlot(size_t offset, size_t size);
66 void removeAvailableSlot(size_t offset, size_t size);
67
68 size_t _totalMemorySize;
69 };
70
71} // namespace vsg
vsg::MemorySlots
Definition MemorySlots.h:36