Skip to content

Commit

Permalink
Use frunk better (#3353)
Browse files Browse the repository at this point in the history
* export some linker variables for the frunk and make sections more granular

* use the frunk for the internal small object region

* move note ons back to internal since they're pretty hot

* add mock define for memory tests

* add no ling

* tidy
  • Loading branch information
m-m-adams authored Feb 10, 2025
1 parent 4249e82 commit ab579db
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
14 changes: 11 additions & 3 deletions linker_script_rz_a1l.ld
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ SECTIONS
/* section 9.4 First level address translation */
.ttb_mmu1 INTERNAL_RAM_START : ALIGN(0x4000)
{
PROVIDE(__frunk_slack_end = .);
ttb_mmu1_base = .;
. += TTB_SIZE;
. = ALIGN(0x4);
Expand Down Expand Up @@ -172,18 +173,25 @@ SECTIONS
*ttb_init.S.obj (.text)
*ttb_init.S.obj (.rodata)
*ttb_init.S.obj (.data)
address_end_reset = .;

. = ALIGN(0x4);
} > RAM012L

.text : ALIGN(0x4) {
_stext = .;

*(.text)
*(.text*)
*(.text.startup)

_etext = .;
address_end_reset = .;

PROVIDE(program_code_end = .);
} > RAM012L

.exceptions : ALIGN(0x4) {
*(.ARM.extab)
*(.ARM.extab.*)
PROVIDE(program_code_end = .);
} > RAM012L

.rodata :
Expand Down
2 changes: 1 addition & 1 deletion src/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ extern void freezeWithError(char const* errmsg);

#define PLACE_SDRAM_BSS __attribute__((__section__(".sdram_bss")))
#define PLACE_SDRAM_DATA __attribute__((__section__(".sdram_data")))

#define PLACE_SDRAM_RODATA __attribute__((__section__(".sdram_rodata")))
// #define PLACE_SDRAM_TEXT __attribute__((__section__(".sdram_text"))) // Paul: I had problems with execution from
// SDRAM, maybe timing?
67 changes: 37 additions & 30 deletions src/deluge/memory/general_memory_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,68 @@
*/

#include "memory/general_memory_allocator.h"

#include "definitions_cxx.hpp"
#include "io/debug/log.h"
#include "memory/stealable.h"
#include "processing/engines/audio_engine.h"

// these are never used directly, they're just reserving raw memory for use in the allocator and clang tidy is unhappy
// NOLINTBEGIN
// TODO: Check if these have the right size
PLACE_SDRAM_BSS char emptySpacesMemory[sizeof(EmptySpaceRecord) * 512];
PLACE_SDRAM_BSS char emptySpacesMemoryInternal[sizeof(EmptySpaceRecord) * 1024];
PLACE_SDRAM_BSS char emptySpacesMemoryInternalSmall[sizeof(EmptySpaceRecord) * 256];
PLACE_SDRAM_BSS char emptySpacesMemoryGeneral[sizeof(EmptySpaceRecord) * 256];
PLACE_SDRAM_BSS char emptySpacesMemoryGeneralSmall[sizeof(EmptySpaceRecord) * 256];
PLACE_INTERNAL_FRUNK char emptySpacesMemory[sizeof(EmptySpaceRecord) * 512];
PLACE_INTERNAL_FRUNK char emptySpacesMemoryInternal[sizeof(EmptySpaceRecord) * 1024];
PLACE_INTERNAL_FRUNK char emptySpacesMemoryInternalSmall[sizeof(EmptySpaceRecord) * 256];
PLACE_INTERNAL_FRUNK char emptySpacesMemoryGeneral[sizeof(EmptySpaceRecord) * 256];
PLACE_INTERNAL_FRUNK char emptySpacesMemoryGeneralSmall[sizeof(EmptySpaceRecord) * 256];
extern uint32_t __frunk_bss_end;
extern uint32_t __frunk_slack_end;
extern uint32_t __sdram_bss_start;
extern uint32_t __sdram_bss_end;
extern uint32_t __heap_start;
extern uint32_t __heap_end;
extern uint32_t program_stack_start;
extern uint32_t program_stack_end;

GeneralMemoryAllocator::GeneralMemoryAllocator() {
uint32_t externalSmallEnd = EXTERNAL_MEMORY_END;
uint32_t externalSmallStart = externalSmallEnd - RESERVED_EXTERNAL_SMALL_ALLOCATOR;
uint32_t externalEnd = externalSmallStart;
uint32_t externalStart = externalSmallStart - RESERVED_EXTERNAL_ALLOCATOR;
uint32_t stealableEnd = externalStart;
uint32_t stealableStart = (uint32_t)&__sdram_bss_end;

uint32_t internalSmallEnd = (uint32_t)&program_stack_start;
uint32_t internalSmallStart = internalSmallEnd - RESERVED_INTERNAL_SMALL;
uint32_t internalEnd = internalSmallStart;
uint32_t internalStart = (uint32_t)&__heap_start;

lock = false;
// NOLINTEND
GeneralMemoryAllocator::GeneralMemoryAllocator() : lock(false) {
uint32_t external_small_end = EXTERNAL_MEMORY_END;
uint32_t external_small_start = external_small_end - RESERVED_EXTERNAL_SMALL_ALLOCATOR;
uint32_t external_end = external_small_start;
uint32_t external_start = external_small_start - RESERVED_EXTERNAL_ALLOCATOR;
uint32_t stealable_end = external_start;
// NOLINTBEGIN
// clang tidy hates both reinterpret and c style casts but linker output is only meaningful when taking address
auto stealable_start = (uint32_t)&__sdram_bss_end;

auto internal_small_start = (uint32_t)&__frunk_bss_end;
auto internal_small_end = (uint32_t)&__frunk_slack_end;
auto internal_start = (uint32_t)&__heap_start;
auto internal_end = (uint32_t)&program_stack_start;
// NOLINTEND
regions[MEMORY_REGION_STEALABLE].name = "stealable";
regions[MEMORY_REGION_INTERNAL].name = "internal";
regions[MEMORY_REGION_EXTERNAL].name = "external";
regions[MEMORY_REGION_EXTERNAL_SMALL].name = "small external";
regions[MEMORY_REGION_INTERNAL_SMALL].name = "small internal";

regions[MEMORY_REGION_STEALABLE].setup(emptySpacesMemory, sizeof(emptySpacesMemory), stealableStart, stealableEnd,
regions[MEMORY_REGION_STEALABLE].setup(emptySpacesMemory, sizeof(emptySpacesMemory), stealable_start, stealable_end,
&cacheManager);
regions[MEMORY_REGION_EXTERNAL].setup(emptySpacesMemoryGeneral, sizeof(emptySpacesMemoryGeneral), externalStart,
externalEnd, nullptr);
regions[MEMORY_REGION_EXTERNAL].setup(emptySpacesMemoryGeneral, sizeof(emptySpacesMemoryGeneral), external_start,
external_end, nullptr);
regions[MEMORY_REGION_EXTERNAL_SMALL].setup(emptySpacesMemoryGeneralSmall, sizeof(emptySpacesMemoryGeneralSmall),
externalSmallStart, externalSmallEnd, nullptr);
external_small_start, external_small_end, nullptr);
regions[MEMORY_REGION_EXTERNAL_SMALL].minAlign_ = 16;
regions[MEMORY_REGION_EXTERNAL_SMALL].pivot_ = 64;
regions[MEMORY_REGION_INTERNAL].setup(emptySpacesMemoryInternal, sizeof(emptySpacesMemoryInternal), internalStart,
internalEnd, nullptr);
regions[MEMORY_REGION_INTERNAL].setup(emptySpacesMemoryInternal, sizeof(emptySpacesMemoryInternal), internal_start,
internal_end, nullptr);

regions[MEMORY_REGION_INTERNAL_SMALL].setup(emptySpacesMemoryInternalSmall, sizeof(emptySpacesMemoryInternalSmall),
internalSmallStart, internalSmallEnd, nullptr);
internal_small_start, internal_small_end, nullptr);
regions[MEMORY_REGION_INTERNAL_SMALL].minAlign_ = 16;
regions[MEMORY_REGION_INTERNAL_SMALL].pivot_ = 64;
}

constexpr size_t kInternalSwitchSize = 128;
constexpr size_t kExternalSwitchSize = 128;
int32_t closestDistance = 2147483647;

void GeneralMemoryAllocator::checkStack(char const* caller) {
Expand Down Expand Up @@ -115,7 +122,7 @@ void* GeneralMemoryAllocator::allocExternal(uint32_t requiredSize) {

lock = true;
void* address = nullptr;
if (requiredSize < 128) {
if (requiredSize < kExternalSwitchSize) {
address = regions[MEMORY_REGION_EXTERNAL_SMALL].alloc(requiredSize, false, NULL);
}
// if it's a large object or the small object allocator was full stick it in the big one
Expand All @@ -139,7 +146,7 @@ void* GeneralMemoryAllocator::allocInternal(uint32_t requiredSize) {

lock = true;
void* address = nullptr;
if (requiredSize < 128) {
if (requiredSize < kInternalSwitchSize) {
address = regions[MEMORY_REGION_INTERNAL_SMALL].alloc(requiredSize, false, NULL);
}
// if it's a large object or the small object allocator was full stick it in the big one
Expand Down
5 changes: 2 additions & 3 deletions src/deluge/model/clip/instrument_clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,8 @@ void InstrumentClip::processCurrentPos(ModelStackWithTimelineCounter* modelStack
ticksTilNextNoteRowEvent = loopLength - lastProcessedPos;
}

PLACE_SDRAM_BSS static PendingNoteOnList
pendingNoteOnList; // Making this static, which it really should have always been,
// actually didn't help max stack usage at all somehow...
static PendingNoteOnList pendingNoteOnList; // Making this static, which it really should have always been,
// actually didn't help max stack usage at all somehow...
pendingNoteOnList.count = 0;

for (int32_t i = 0; i < noteRows.getNumElements(); i++) {
Expand Down
10 changes: 6 additions & 4 deletions tests/32bit_unit_tests/mocks/mock_defines.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <cstdint>
int32_t __sdram_bss_end = 0;
int32_t program_stack_start = 0;
int32_t program_stack_end = 0;
int32_t __heap_start = 0;
uint32_t __sdram_bss_end = 0;
uint32_t __frunk_bss_end = 0;
uint32_t __frunk_slack_end = 0;
uint32_t program_stack_start = 0;
uint32_t program_stack_end = 0;
uint32_t __heap_start = 0;
uint32_t currentUIMode = 0;

#define NUM_ENCODERS 6
Expand Down

0 comments on commit ab579db

Please sign in to comment.