Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
general_memory_allocator.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 "definitions_cxx.hpp"
21#include "memory/memory_region.h"
22#include "memory/stealable.h"
23#include <concepts>
24
25#define MEMORY_REGION_STEALABLE 0
26#define MEMORY_REGION_INTERNAL 1
27#define MEMORY_REGION_EXTERNAL 2
28#define MEMORY_REGION_EXTERNAL_SMALL 3
29#define MEMORY_REGION_INTERNAL_SMALL 4
30#define NUM_MEMORY_REGIONS 5
31constexpr uint32_t RESERVED_EXTERNAL_ALLOCATOR = 0x00200000; // 2 MiB - at least. See GeneralMemoryAllocator()
32constexpr uint32_t RESERVED_EXTERNAL_SMALL_ALLOCATOR = 0x00020000; // 200k
33constexpr uint32_t RESERVED_INTERNAL_SMALL = 0x00010000; // 200k
34class Stealable;
35
36/*
37 * ======================= MEMORY ALLOCATION ========================
38 *
39 * The Deluge codebase uses a custom memory allocation system, largely necessitated by the fact that
40 * the Deluge’s CPU has 3MB ram, plus the Deluge has an external 64MB SDRAM IC, and both of these
41 * need to have dynamic memory allocation as part of the same system.
42 *
43 * The internal RAM on the CPU is a bit faster, so is allocated first when available.
44 * But huge blocks of data like cached Clusters of audio data from the SD card are always
45 * placed on the external RAM IC because they would overwhelm the internal RAM too quickly,
46 * preventing potentially thousands of small objects which need to be accessed all the time
47 * from being placed in that fast internal RAM.
48 *
49 * Various objects or pieces of data remain loaded (cached) in RAM even when they are no longer necessarily needed.
50 * The main example of this is audio data in Clusters, discussed above. The base class for all such
51 * objects is Stealable, and as the name suggests, their memory may usually be “stolen” when needed.
52 *
53 * Most Stealables store a “numReasonsToBeLoaded”, which counts how many “things” are requiring
54 * that object to be retained in RAM. E.g. a Cluster of audio data would have a “reason” to
55 * remain loaded in RAM if it is currently being played back. If that numReasons goes down to 0,
56 * then that Stealable object is usually free to have its memory stolen.
57 *
58 * Stealables which in fact are eligible to be stolen at a given moment are stored in a queue which
59 * prioritises stealing of the audio data which is less likely to be needed, e.g. if it belongs to a
60 * Song that’s no longer loaded. But, to avoid overcomplication, this queue is not adhered to in the
61 * case where a neighbouring region of memory is chosen for allocation (or itself being stolen) when
62 * the allocation requires that the object in question have its memory stolen too in order to make
63 * up a large enough allocation.
64 */
65
66class GeneralMemoryAllocator {
67public:
68 GeneralMemoryAllocator();
69 [[gnu::always_inline]] void* allocMaxSpeed(uint32_t requiredSize, void* thingNotToStealFrom = nullptr) {
70 return alloc(requiredSize, true, false, thingNotToStealFrom);
71 }
72
73 [[gnu::always_inline]] void* allocLowSpeed(uint32_t requiredSize, void* thingNotToStealFrom = nullptr) {
74 return alloc(requiredSize, false, false, thingNotToStealFrom);
75 }
76
80 template <std::derived_from<Stealable> T>
81 [[gnu::always_inline]] void* allocStealable(uint32_t requiredSize = sizeof(T),
82 void* thingNotToStealFrom = nullptr) {
83 return alloc(requiredSize, false, true, thingNotToStealFrom);
84 }
85
86 void* alloc(uint32_t requiredSize, bool mayUseOnChipRam, bool makeStealable, void* thingNotToStealFrom);
87 void dealloc(void* address);
88 void* allocExternal(uint32_t requiredSize);
89 void* allocInternal(uint32_t requiredSize);
90 void deallocExternal(void* address);
91 uint32_t shortenRight(void* address, uint32_t newSize);
92 uint32_t shortenLeft(void* address, uint32_t amountToShorten, uint32_t numBytesToMoveRightIfSuccessful = 0);
93 void extend(void* address, uint32_t minAmountToExtend, uint32_t idealAmountToExtend,
94 uint32_t* getAmountExtendedLeft, uint32_t* getAmountExtendedRight, void* thingNotToStealFrom = nullptr);
95 uint32_t extendRightAsMuchAsEasilyPossible(void* address);
96 void test();
97 uint32_t getAllocatedSize(void* address);
98 void checkStack(char const* caller);
99 void testShorten(int32_t i);
100 int32_t getRegion(void* address);
101 void testMemoryDeallocated(void* address);
102
103 void putStealableInQueue(Stealable* stealable, StealableQueue q);
104 void putStealableInAppropriateQueue(Stealable* stealable);
105
106 MemoryRegion regions[NUM_MEMORY_REGIONS];
110 uint32_t stealableSectionsEnd = 0;
111 // only used for managing stealables (audio files that we could deallocate and re load from sd later if needed)
112 CacheManager cacheManager;
113 bool lock;
114
115 static GeneralMemoryAllocator& get() {
116 static GeneralMemoryAllocator generalMemoryAllocator;
117 return generalMemoryAllocator;
118 }
119
120private:
121 void checkEverythingOk(char const* errorString);
122};
123
124extern "C" {
125void* delugeAlloc(unsigned int requiredSize, bool mayUseOnChipRam = true);
126void delugeDealloc(void* address);
127}
Definition cache_manager.h:12
uint32_t stealableSectionsStart
Definition general_memory_allocator.h:109
void * allocStealable(uint32_t requiredSize=sizeof(T), void *thingNotToStealFrom=nullptr)
Definition general_memory_allocator.h:81
Definition memory_region.h:44
Definition stealable.h:25