Deluge Firmware 1.3.0
Build date: 2025.04.16
Loading...
Searching...
No Matches
memory_region.h
1/*
2 * Copyright © 2015-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "memory/cache_manager.h"
21#include "util/container/array/ordered_resizeable_array_with_multi_word_key.h"
22
23#include <util/exceptions.h>
24
26 uint32_t length;
27 uint32_t address;
28};
29
31 uint32_t address; // 0 means didn't grab / not found.
32 int32_t amountsExtended[2];
33 uint32_t longestRunFound; // Only valid if didn't return some space.
34};
35#define SPACE_HEADER_EMPTY 0
36#define SPACE_HEADER_STEALABLE 0x40000000
37#define SPACE_HEADER_ALLOCATED 0x80000000
38
39#define SPACE_TYPE_MASK 0xC0000000u
40#define SPACE_SIZE_MASK 0x3FFFFFFFu
41constexpr size_t max_align_big = 1 << 12;
42constexpr size_t min_align_big = 64;
43constexpr size_t pivot_big = 512;
44class MemoryRegion {
45public:
46 MemoryRegion();
47 void setup(void* emptySpacesMemory, int32_t emptySpacesMemorySize, uint32_t regionBegin, uint32_t regionEnd,
48 CacheManager* cacheManager);
49 void* alloc(uint32_t requiredSize, bool makeStealable, void* thingNotToStealFrom);
50 size_t nallocx(size_t size) { return padSize(size); }
51 uint32_t shortenRight(void* address, uint32_t newSize);
52 uint32_t shortenLeft(void* address, uint32_t amountToShorten, uint32_t numBytesToMoveRightIfSuccessful = 0);
53 void extend(void* address, uint32_t minAmountToExtend, uint32_t idealAmountToExtend,
54 uint32_t* getAmountExtendedLeft, uint32_t* getAmountExtendedRight, void* thingNotToStealFrom);
55 uint32_t extendRightAsMuchAsEasilyPossible(void* spaceAddress);
56 void dealloc(void* address);
57 void verifyMemoryNotFree(void* address, uint32_t spaceSize);
58
59 uint32_t start;
60 uint32_t end;
61
62 CacheManager& cache_manager() {
63 if (cache_manager_) {
64 return *cache_manager_;
65 }
66 throw deluge::exception::NO_CACHE_FOR_REGION;
67 }
68
69#if ALPHA_OR_BETA_VERSION
70 char const* name; // For debugging messages only.
71#endif
73
74private:
75 friend class CacheManager;
76 friend class GeneralMemoryAllocator;
77 // manages "stealables" for a memory region, only used in external stealable region
78 CacheManager* cache_manager_;
79 uint32_t numAllocations_{0};
80 uint32_t pivot_{pivot_big}; // items smaller than pivot allocate to left, larger to right
81 size_t maxAlign_ = max_align_big;
82 // not size_t, it needs to be compared to results of pointer subtractions that can be negative
83 ptrdiff_t minAlign_ = min_align_big;
84 void markSpaceAsEmpty(uint32_t spaceStart, uint32_t spaceSize, bool mayLookLeft = true, bool mayLookRight = true);
86 attemptToGrabNeighbouringMemory(void* originalSpaceAddress, int32_t originalSpaceSize, int32_t minAmountToExtend,
87 int32_t idealAmountToExtend, void* thingNotToStealFrom,
88 uint32_t markWithTraversalNo = 0, bool originalSpaceNeedsStealing = false);
89
90 void writeTempHeadersBeforeASteal(uint32_t newStartAddress, uint32_t newSize);
91 void sanityCheck();
92 uint32_t padSize(uint32_t requiredSize);
93};
Definition ordered_resizeable_array_with_multi_word_key.h:25
Definition memory_region.h:25
Definition memory_region.h:30