Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
sample_folder_index.h
1/*
2 * Copyright © 2026 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 <array>
21#include <cstddef>
22#include <cstdint>
23#include <optional>
24
25extern "C" {
26#include "fatfs/ff.h"
27}
28
46public:
49 std::optional<FilePointer> find(char const* filePath);
50
52 void clear();
53
58 static constexpr bool canLookUp(char const* fileName) {
59 char const* c = fileName;
60 for (; *c; c++) {
61 unsigned char ch = *c;
62 if (ch < ' ' || ch > '~') {
63 return false; // Control characters, and anything not ASCII
64 }
65 switch (ch) {
66 case '\\': // FatFs takes this as a folder separator
67 case '*':
68 case ':':
69 case '<':
70 case '>':
71 case '|':
72 case '"':
73 case '?': // Also what f_readdir() gives for a name it can't read
74 return false;
75 default:
76 break;
77 }
78 }
79 return c != fileName && c[-1] != '.' && c[-1] != ' ';
80 }
81
82 static constexpr size_t kMaxFolders = 4;
83 static constexpr size_t kMaxPathLength = 256;
86 static constexpr uint32_t kMaxEntriesToRead = 4096;
89 static constexpr size_t kMaxMemoryPerFolder = 64 * 1024;
90
96 static constexpr uint32_t kHitsToPayOff = 2;
97 static constexpr uint32_t kMinLookupsBeforeIndexing = 2;
98 static constexpr uint32_t kMaxLookupsBeforeIndexing = 64;
99
102 class Space {
103 public:
105 constexpr Space(size_t start, size_t blockSize)
106 : entriesEnd_{start}, namesStart_{blockSize < start ? start : blockSize}, blockSize_{namesStart_} {}
107
110 constexpr std::optional<uint16_t> take(size_t entrySize, size_t namesSize) {
111 if (namesStart_ - entriesEnd_ < entrySize + namesSize
112 || blockSize_ - namesStart_ + namesSize > UINT16_MAX) {
113 return std::nullopt;
114 }
115 entriesEnd_ += entrySize;
116 namesStart_ -= namesSize;
117 return blockSize_ - namesStart_;
118 }
119
120 private:
121 size_t entriesEnd_;
122 size_t namesStart_;
123 size_t blockSize_;
124 };
125
126private:
127 struct Entry {
128 FilePointer filePointer;
129 uint16_t longName;
130 uint16_t shortName;
131 };
132 struct Folder;
133 class Block;
134
135 Folder* findFolder(char const* path, size_t pathLength);
136 Folder* indexFolder(char const* path);
137 void forget(size_t slot);
138 void blockStolen(Block* block);
139 void folderLost(Folder const& folder);
140 void indexingDidNotPay();
141
142 std::array<Folder*, kMaxFolders> folders_{};
143 char lastFolderLookedUp_[kMaxPathLength] = {};
144 uint32_t lookupsInARow_ = 0;
145 uint32_t lookupsBeforeIndexing_ = kMinLookupsBeforeIndexing;
146 uint32_t useCounter_ = 0;
147};
148
149extern SampleFolderIndex sampleFolderIndex;
Definition sample_folder_index.cpp:51
constexpr Space(size_t start, size_t blockSize)
For a block of blockSize bytes whose first start bytes are taken.
Definition sample_folder_index.h:105
constexpr std::optional< uint16_t > take(size_t entrySize, size_t namesSize)
Definition sample_folder_index.h:110
Definition sample_folder_index.h:45
void blockStolen(Block *block)
Definition sample_folder_index.cpp:273
static constexpr uint32_t kMaxEntriesToRead
Definition sample_folder_index.h:86
void clear()
Forgets every folder, and frees the memory.
Definition sample_folder_index.cpp:298
uint32_t lookupsInARow_
Of files in lastFolderLookedUp_.
Definition sample_folder_index.h:144
std::optional< FilePointer > find(char const *filePath)
Definition sample_folder_index.cpp:89
void folderLost(Folder const &folder)
A folder's index is being thrown away, before it would have been otherwise.
Definition sample_folder_index.cpp:288
static constexpr bool canLookUp(char const *fileName)
Definition sample_folder_index.h:58
static constexpr size_t kMaxMemoryPerFolder
Definition sample_folder_index.h:89
static constexpr uint32_t kHitsToPayOff
Definition sample_folder_index.h:96
Definition sample_folder_index.h:127
uint16_t longName
Distances back from the end of the Entry's block to the names.
Definition sample_folder_index.h:129
One indexed folder. Its files are listed in its chain of Blocks, in the order they're in the folder.
Definition sample_folder_index.cpp:36