Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
SampleFolderIndex Class Reference

#include <sample_folder_index.h>

Classes

class  Space
struct  Entry
struct  Folder
 One indexed folder. Its files are listed in its chain of Blocks, in the order they're in the folder. More...
class  Block

Public Member Functions

std::optional< FilePointer > find (char const *filePath)
void clear ()
 Forgets every folder, and frees the memory.

Static Public Member Functions

static constexpr bool canLookUp (char const *fileName)

Static Public Attributes

static constexpr size_t kMaxFolders = 4
static constexpr size_t kMaxPathLength = 256
static constexpr uint32_t kMaxEntriesToRead = 4096
static constexpr size_t kMaxMemoryPerFolder = 64 * 1024
static constexpr uint32_t kHitsToPayOff = 2
static constexpr uint32_t kMinLookupsBeforeIndexing = 2
static constexpr uint32_t kMaxLookupsBeforeIndexing = 64

Private Member Functions

Folder * findFolder (char const *path, size_t pathLength)
Folder * indexFolder (char const *path)
void forget (size_t slot)
void blockStolen (Block *block)
void folderLost (Folder const &folder)
 A folder's index is being thrown away, before it would have been otherwise.
void indexingDidNotPay ()

Private Attributes

std::array< Folder *, kMaxFolders > folders_ {}
char lastFolderLookedUp_ [kMaxPathLength] = {}
uint32_t lookupsInARow_ = 0
 Of files in lastFolderLookedUp_.
uint32_t lookupsBeforeIndexing_ = kMinLookupsBeforeIndexing
uint32_t useCounter_ = 0

Detailed Description

Finds sample files quickly when many are loaded from one folder, as for a multisample preset with a hundred zones.

FatFs finds a file by searching its folder from the start, a sector at a time, and for such a preset that search is most of the loading time. This reads the folder through once instead, and remembers where each file starts and how big it is.

It's only ever a shortcut: when find() returns nothing, the caller opens the file the normal way. It's only used to find sample files, never for writing. And it forgets everything whenever anything is written to the card or the card is removed or inserted (sdCardChangeCount, in diskio.c), so it can't point to where a file used to be.

Nothing ever needs freeing by hand. An indexed folder's files are listed in a chain of blocks of stealable memory, and the memory allocator takes any of them back whenever it needs the space - like it does with sample data. Losing one block loses that folder's index. That's safe at any time between calls, because nothing outside this class ever points into a block: find() returns a copy. Each block is exactly as big as a Cluster of sample data, so taking one back makes room for exactly one Cluster - a smaller block wouldn't, and taking it would mean taking its neighbours too. How big a Cluster is depends on how the card was formatted, so a folder takes as many blocks as it needs.

Member Function Documentation

◆ blockStolen()

void SampleFolderIndex::blockStolen ( Block * block)
private

The allocator is taking a Block back. Its folder's other Blocks are freed the next time anything is looked up - not now, from inside the allocator.

◆ canLookUp()

constexpr bool SampleFolderIndex::canLookUp ( char const * fileName)
inlinestaticconstexpr

Whether find() can look up this file name. It only does for names it can match just the way FatFs does: printable ASCII characters that FatFs allows in names, not ending in a dot or space - nearly every sample's name. FatFs matches other names in ways not copied here, like ignoring the case of accented letters or a dot at the end, so those are always left to it.

◆ find()

std::optional< FilePointer > SampleFolderIndex::find ( char const * filePath)

Where the file at filePath starts and how big it is, if its folder has been indexed. The folder gets indexed when files keep being looked up in it - reading a whole folder isn't worth it for one file.

Member Data Documentation

◆ kHitsToPayOff

uint32_t SampleFolderIndex::kHitsToPayOff = 2
staticconstexpr

Reading a whole folder costs about as much as finding two of its files the normal way, so an index has paid for itself once it's found two files. Until indexes start getting thrown away before that - because the card keeps getting written to, or memory is short - a folder is indexed on the second lookup in a row. Each index thrown away too early doubles the lookups in a row needed for the next, up to the maximum, and one that pays for itself brings it back down. That way indexing never costs much more than it saves.

◆ kMaxEntriesToRead

uint32_t SampleFolderIndex::kMaxEntriesToRead = 4096
staticconstexpr

A folder is read up to this many directory entries (32 bytes each, a long name taking several), however few files they hold, and only indexed that far

◆ kMaxMemoryPerFolder

size_t SampleFolderIndex::kMaxMemoryPerFolder = 64 * 1024
staticconstexpr

A folder takes as many blocks as it needs, up to the fewest that make at least this much memory, and is indexed as far as that goes. That's over a thousand files with typical sample names, whatever the size of a Cluster.


The documentation for this class was generated from the following files: