-
Notifications
You must be signed in to change notification settings - Fork 56
Memory pool #1283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
n1LS
wants to merge
9
commits into
xiphonics:master
Choose a base branch
from
n1LS:memory-pool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Memory pool #1283
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
095353f
adds memory pool with 1k buffer and file index list
n1LS 844a60b
views now use the shared file index list
n1LS a5fedf5
moving fileIndexLists from stack to use the global buffer as well
n1LS 24a7811
merge
n1LS e4ae1cd
adds getter for currentView
n1LS 8724732
removes ui from memoryPool, adds assert
n1LS 48e49d2
adds scoped locks to memory pool
n1LS b73d4de
added 2nd factor to scoped locks (owner ref)
n1LS 24f2678
merges upstream/master
n1LS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * | ||
| * Copyright (c) 2026 xiphonics, inc. | ||
| * | ||
| * This file is part of the picoTracker firmware | ||
| */ | ||
|
|
||
| #include "MemoryPool.h" | ||
|
|
||
| #ifdef ADV | ||
| #include "Adapters/adv/mutex/advMutex.h" | ||
| using Mutex = advMutex; | ||
| #else | ||
| #include "Adapters/picoTracker/mutex/picoTrackerMutex.h" | ||
| using Mutex = picoTrackerMutex; | ||
| #endif | ||
|
|
||
| // globally available 1k scratch buffer for temporary use, e.g. for file | ||
| // loading, string rendering, file sorting | ||
| char MemoryPool::buffer_[MEMORYPOOL_SCRATCH_SIZE]; | ||
| static Mutex s_scratchBufferMutex; | ||
| SysMutex *MemoryPool::scratchBufferMutex_ = &s_scratchBufferMutex; | ||
|
|
||
| // globally available list of file indexes for the current directory | ||
| etl::vector<int, MAX_FILE_INDEX_SIZE> MemoryPool::fileIndexList_; | ||
| static Mutex s_fileIndexListMutex; | ||
| SysMutex *MemoryPool::fileIndexListMutex_ = &s_fileIndexListMutex; | ||
| const void *MemoryPool::fileIndexCurrentKey_ = nullptr; | ||
| int MemoryPool::fileIndexLockDepth_ = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * | ||
| * Copyright (c) 2026 xiphonics, inc. | ||
| * | ||
| * This file is part of the picoTracker firmware | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "System/FileSystem/FileSystem.h" | ||
|
|
||
| #include "ScopedLock.h" | ||
|
|
||
| constexpr size_t MEMORYPOOL_SCRATCH_SIZE = 1024; | ||
|
|
||
| class MemoryPool { | ||
| public: | ||
| template <typename T> class ScopedRef { | ||
| public: | ||
| ScopedRef(T &resource, SysMutex &mutex) | ||
| : resource_(resource), lock_(mutex) {} | ||
| T *operator->() { return &resource_; } | ||
| T &operator*() { return resource_; } | ||
| ScopedRef(const ScopedRef &) = delete; | ||
| ScopedRef &operator=(const ScopedRef &) = delete; | ||
|
|
||
| private: | ||
| T &resource_; | ||
| ScopedLock lock_; | ||
| }; | ||
|
|
||
| // Scoped accessor for the raw scratch buffer. | ||
| // | ||
| // CORRECT: | ||
| // auto buf = MemoryPool::getBuffer(); | ||
| // char *p = buf.data(); // lock held until 'buf' goes out of scope | ||
| // | ||
| // WRONG - lock released immediately, p is unprotected: | ||
| // char *p = MemoryPool::getBuffer().data(); | ||
| class ScopedBuffer { | ||
| public: | ||
| ScopedBuffer(char *buf, SysMutex &m) : buf_(buf), lock_(m) {} | ||
| char *data() { return buf_; } | ||
| ScopedBuffer(const ScopedBuffer &) = delete; | ||
| ScopedBuffer &operator=(const ScopedBuffer &) = delete; | ||
|
|
||
| private: | ||
| char *buf_; | ||
| ScopedLock lock_; | ||
| }; | ||
|
|
||
| // Scoped accessor for the file index list with key-based recursive locking. | ||
| // | ||
| // Pass 'this' as the key so the same object can re-enter without deadlocking: | ||
| // auto list = MemoryPool::getFileIndexList(this); | ||
| // | ||
| // A different key (or the first caller) acquires the mutex; the same key | ||
| // simply increments a depth counter and skips re-locking. The mutex is | ||
| // released only when the depth reaches zero (outermost scope exits). | ||
| class KeyedScopedRef { | ||
| public: | ||
| using VecType = etl::vector<int, MAX_FILE_INDEX_SIZE>; | ||
|
|
||
| KeyedScopedRef(VecType &resource, SysMutex &mutex) | ||
| : resource_(resource), mutex_(mutex) {} | ||
|
|
||
| ~KeyedScopedRef() { | ||
| if (--fileIndexLockDepth_ == 0) { | ||
| fileIndexCurrentKey_ = nullptr; | ||
| mutex_.Unlock(); | ||
| } | ||
| } | ||
|
|
||
| VecType *operator->() { return &resource_; } | ||
| VecType &operator*() { return resource_; } | ||
| KeyedScopedRef(const KeyedScopedRef &) = delete; | ||
| KeyedScopedRef &operator=(const KeyedScopedRef &) = delete; | ||
|
|
||
| private: | ||
| VecType &resource_; | ||
| SysMutex &mutex_; | ||
| }; | ||
|
|
||
| [[nodiscard]] static KeyedScopedRef getFileIndexList(const void *key) { | ||
| if (fileIndexCurrentKey_ != key) { | ||
| fileIndexListMutex_->Lock(); | ||
| fileIndexCurrentKey_ = key; | ||
| } | ||
| ++fileIndexLockDepth_; | ||
| return {fileIndexList_, *fileIndexListMutex_}; | ||
| } | ||
|
|
||
| [[nodiscard]] static ScopedBuffer getBuffer() { | ||
| return {buffer_, *scratchBufferMutex_}; | ||
| } | ||
|
|
||
| private: | ||
| static char buffer_[MEMORYPOOL_SCRATCH_SIZE]; | ||
| static etl::vector<int, MAX_FILE_INDEX_SIZE> fileIndexList_; | ||
| static SysMutex *fileIndexListMutex_; | ||
| static SysMutex *scratchBufferMutex_; | ||
| static const void *fileIndexCurrentKey_; | ||
| static int fileIndexLockDepth_; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * | ||
| * Copyright (c) 2026 xiphonics, inc. | ||
| * | ||
| * This file is part of the picoTracker firmware | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "System/Process/SysMutex.h" | ||
|
|
||
| // minimal RAII lock guard using the SysMutex interface ************************ | ||
|
|
||
| class ScopedLock { | ||
| public: | ||
| explicit ScopedLock(SysMutex &m) : m_(m) { m_.Lock(); } | ||
| ~ScopedLock() { m_.Unlock(); } | ||
| ScopedLock(const ScopedLock &) = delete; | ||
| ScopedLock &operator=(const ScopedLock &) = delete; | ||
|
|
||
| private: | ||
| SysMutex &m_; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.